sympmarc / SPServices

SPServices is a jQuery library which abstracts SharePoint's Web Services and makes them easier to use. It also includes functions which use the various Web Service operations to provide more useful (and cool) capabilities. It works entirely client side and requires no server install.
MIT License
208 stars 61 forks source link

SPRedirectWithID not working #71

Open rob-windsor opened 6 years ago

rob-windsor commented 6 years ago

I'm trying to redirect a user to the edit form after they submit an item in a new form. Here's the code I'm embedding into the new form:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.SPServices/2014.02/jquery.SPServices.js"></script>

<script type="text/javascript">
    (function () {
        jQuery(function () {
            $().SPServices.SPRedirectWithID({
                redirectUrl: "EditForm.aspx",
                qsParamName: "ID"
            });
        });

        window.PreSaveAction = function () {
            var aspnetForm = jQuery("#aspnetForm");
            alert(aspnetForm.attr("action"));

            return true;
        }
    })();
</script>

In SharePoint Online the alert in the PreSaveAction function shows that the action attribute in the form has been updated but I still get redirected to the list view after the save.

image

In SharePoint 2013 (with MDS disabled) the alert in the PreSaveAction function shows that the action attribute in the form has not been updated so, as expected, I get redirected to the list view after the save.

image

I think I'm using SPRedirectWithID as directed in the documentation.

sympmarc commented 6 years ago

This function was always a bit of a hack. Basically, the page refreshes until the script sees that the item has been created (there's a new item since the Save button was pushed), and then the aspnewForm action is changed to redirect to the the target with the newly discovered ID.

What you're seeing in your first screenshot is that interim state: the Source is set to the current page and RealSource holds the Source value SharePoint had set originally.

I haven't ever tested this on SharePoint Online, to be honest. In theory it should work in a "classic" list's NewForm, but it's possible something has changed to make it not work. Let me see if I can replicate.

rob-windsor commented 6 years ago

OK, thanks. I just wanted to confirm that I wasn't using the API incorrectly.

sympmarc commented 6 years ago

I've reproduced, and it looks to me like the action attribute is ignored for some reason. It may be that they saw this as a vulnerability and plugged that hole. I'll do a little more debugging...

sympmarc commented 6 years ago

Yeah, from what I can see, action is now ignored, unless I'm missing something. The script to do the postback is more byzantine than ever, but I traced it pretty deep. All the right set up occurs when the NewForm loads, but the redirect I put into the action never happens.

rob-windsor commented 6 years ago

Thanks for investigating this.

sympmarc commented 6 years ago

Sorry I don't have a better answer.

hcarvajal commented 5 years ago

I want to help on this project.

MiracelVip commented 4 years ago

is there any progress on this bug? I got the same problem that the redirection is not working at all

with this script i could change the redirection behaving but i don't have the ID of the record which is much more important `$(document).ready(function () {

// for Save button var button = $('input[accesskey="O"]');

button.removeAttr("onclick");

button.click(function() {

         var elementName = $(this).attr("name"); 
         var aspForm = $("form[id=aspnetForm]"); 
         var oldPostbackUrl = aspForm.get(0).action; 
         var currentSourceValue = GetUrlKeyValue("Source", true, oldPostbackUrl); 
         var newPostbackUrl = oldPostbackUrl.replace(currentSourceValue, "https://departments.brueckner.com/shared/pro/NBD_BOPE/Lists/BOPE_interested_parties_contact/NewForm.aspx");

if (!PreSaveItem()) return false; WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(elementName, "", true, "", newPostbackUrl, false, true)); }); });`

hcarvajal commented 4 years ago

What version of sharepoint is this. You can easily get the url using a api call to the service instead. You can also do an Update add  or delete using the same logic of code with a post in JQUERY   $.ajax({    url: taskUrl,    type: "POST",    contentType: "application/json;odata=verbose",    data: JSON.stringify(item),    headers: {              "Accept": "application/json;odata=verbose",               "X-RequestDigest": $("#__REQUESTDIGEST").val(),      },      success: function (data) {                            /  var ItemId = data.d.Id;               var parts = document.getElementById("txtAttachment").value.split("\")               var filename = parts[parts.length - 1];               var   file = document.getElementById("txtAttachment").files[0];                             // uploadFile(ItemId,filename,file);                    /             window.location.href = "{url}";                       },        error:function(data, status, xhr)       {          console.log(data);          alert('task did not save');      }              });  }); 

On Friday, November 22, 2019, 06:11:50 AM EST, MiracelVip <notifications@github.com> wrote:  

is there any progress on this bug? I got the same problem that the redirection is not working at all

with this script i could change the redirection behaving but i don't have the ID of the record which is much more important `$(document).ready(function () {

// for Save button var button = $('input[accesskey="O"]');

button.removeAttr("onclick");

button.click(function() { var elementName = $(this).attr("name"); var aspForm = $("form[id=aspnetForm]"); var oldPostbackUrl = aspForm.get(0).action; var currentSourceValue = GetUrlKeyValue("Source", true, oldPostbackUrl); var newPostbackUrl = oldPostbackUrl.replace(currentSourceValue, "https://departments.brueckner.com/shared/pro/NBD_BOPE/Lists/BOPE_interested_parties_contact/NewForm.aspx");

if (!PreSaveItem()) return false; WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(elementName, "", true, "", newPostbackUrl, false, true)); }); });`

— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or unsubscribe.

sympmarc commented 4 years ago

This function was always a bit of a hack, and I haven't tried to maintain it over the years. Obviously, the version of SharePoint matters, but patches to the versions may have rendered this unusable.

w3rafu commented 2 years ago

I get XHRPOSThttps://sharepoint.womicklawfirm.com/test3/_vti_bin/Lists.asmx [HTTP/1.1 500 Internal Server Error 236ms] when I try to use SPRedirectWithID