Passing a Source Parameter on the Query String with Multiple Other Parameters

I’ve posted on similar tricks to this in the past, but this little trick is really helpful and worth calling out on its own.  Say that you’d like to pass a Source parameter on the query String, as SharePoint often does for you:

http://servername/sites/sitename/Lists/MyList/EditForm.aspx?ID=275&Source=http://servername/sites/sitename/default.aspx

Simple, and you see it all the time as you navigate around in SharePoint. In many cases, you’d also like to pass additional parameters that will “travel along” with the Source parameter.  The trick for this is to simply encode the ampersand (&) character so that it won’t be treated as the start of a new Query String parameter, but instead just a part of the Source parameter. The ASCII value for the ampersand is 26 in hex, so you encode it as %26 in the URL:

http://servername/sites/sitename/Lists/MyList/EditForm.aspx?ID=275&Source=/sites/sitename/default.aspx?ProjectID=123%26Boolean=0%26EmployeeID=345

Note that I’m passing in the relative URL for the default.aspx page.  Get into the habit of using relative URLs all the time and you’ll be in much better shape in general.

With the above URL, when you are finished with the EditForm.aspx page, whether by clicking OK or Cancel, you’ll be redirected to:

http://servername/sites/sitename/default.aspx?ProjectID=123&Boolean=0&EmployeeID=345

Similar Posts

37 Comments

  1. Marc,

    It looks like all Sharepoint roads lead to you. Everytime I google a problem I end up in your backyard. I do appreciate how much you have published on Shareopoint, it has helped me a lot.

    I do have a question, what if I am passing my URL like so…

    onclick=”window.location=’NewItemForm.aspx?source={$PageUrl}’

    I started doing this the other day based on a MSDN posting (which I think that you were involved in) about XSLT filtering. I am using a single page with querystring to filter it multiple ways. This is keeping me from having to have 16 different pages for different views and is much easier to maintain.

    My problem is, even though I get to the page by escaping (%amp;) properly in the original link, in the browser it reverts back to just ‘&’ and thus the above code stopped working. It redirects back with the first querystring variable and as expected drops everything after the ‘&’

    Any thoughts?

    Thanks and keep up the good work,
    Steve

  2. The first optoin worked like a charm. I was in the middle of working out a quick js functoin to take care of replacements. This is easier (i.e. less code).

    I have only recently dug into Sharepoint and I have found out the following: Despite what everyone beleives, that Sharepoint is a no code solution to all your business process needs, I have been writing a lot of what looks suspiciously like code :)

    Here’s the source page URL

    http://secpoint:8889/Lists/YBR/YBRItems.aspx?ShowWon=0&ShowLost=0&ShowDied=0&ParkFilter=0

    Here’s what gets passed

    http://secpoint:8889/Lists/YBR/NewItemForm.aspx?source=http%3a%2f%2fsecpoint%3a8889%2fLists%2fYBR%2fYBRItems.aspx%3fShowWon%3d0%26ShowLost%3d0%26ShowDied%3d0%26ParkFilter%3d0

    Steve

    1. Excellent! The code didn’t come through for your button. Send it to me using the Contact page above and I’ll paste it into your comment for you.

      M.

  3. Marc,
    I am trying to call a list edit form from an SSRS report that displays the list items (eventually would like to popup the dialog, but we’ll start with baby steps). For some reason, the EditForm.aspx on our SP2010 seems to ignore any url that isn’t a relative source on the site itself. So the SSRS is on a different server, it calls the edit form, but on save or cancel goes back to the all items sp view rather than the Source parameter with the report location. To test, I’ve tried just passing in “&Source=http://www.google.com” to the EditForm.aspx and on Save or Cancel it goes back to the All Items list. Is there a higher level SPsetting that limits what the “Source” parameter will accept?
    Thanks!
    Rob

    1. Rob:

      I’ve run into this before. It seems that the Source redirect won’t work across Site Collections. What I think I’ve done in the past is send a new Query String parameter as part of the URL (Source2 maybe?) and then in the EditForm, I do the redirect in my code. Messy, but it makes it work.

      M.

  4. Marc,

    I have this link:

    “…/it/GOHP/Lists/QW Project Deliverables/NewForm_Cust.aspx?PID={$PID}&Source=/it/GOHP/QWins/QWProject.aspx?ID={$PID}%26ProjectName={$ProjectName}”

    By using your %26 in the URL I get the ProjectName= in the URL just not the data. The data was read from a query string into the $ProjectName parameter. After entering in a new record I want the user redirected to this page with the ID and ProjectName populated. Any thoughts.

    Thanks,
    Jayamin

  5. I am pretty much trying to accomplish what you described above. I have a dispForm with a SPgridview on it as a web part. In the SPGridView I have an edit button that takes me to editForm.aspx page for a different list.

    For example the dispForm is for a specific Company and the gridview contains all the contacts for that company.

    My gridview, on click event of the edit button redirects to the contact list editform.aspx. I pass in on the query string of my redirect the Company ID from the dispForm so I know which record to go back to along with other useful information.

    On save of the contact I want to redirect back to company dispForm for the company I was just on.

    In the edit form I added a CEWP to read in the query string variables and save them to a var in JavaScript /jquery using $(document).ready(function() var goTo= ‘Source=list/listname/dispForm.aspx?ID=CompanyID%26Source=previousSource’.

    On save $(document).ready calls function setOnSubmitRedir().
    In the setOnSubmitRedir I do the following.
    function setOnSubmitRedir (redirURL){
    var action = $(“#aspnetForm”).attr(‘action’);
    var end = action.indexOf(‘&’);
    if(action.indexOf(‘&’)<0){
    newAction = action + "?Source=" + redirURL;
    }else{
    newAction = action.substring(0,end) + "&Source=" + redirURL;
    }

    $("#aspnetForm").attr('action',newAction );

    On save takes me back to the dispForm but it does not give me any query string variables as I would expect. Any help would be appreciated.
    Thank you,
    Karen

      1. Marc,
        Thanks for your reply. I figured it out. I needed to encode the URL in the web part grid view, not in the CEWP. In fact, once I encoded the &Source=’With my return data’ in my web part I found I didn’t even need the CEWP to change anything onSave. It worked like a charm.

        Thanks,
        Karen

  6. Thanks for that, another question, can you specify different addresses: one for clicking OK and one for clicking Cancel? currently it redirect me to one address no matter what I clicked.

    1. Shiri:

      Not using the Source mechanism; it’s a one-destination thing. You can customize the form or add script to override the default behavior.

      M.

  7. I have connected two web parts a list and document library. I filter my document library web part by the pin that is in my list web part. Now what I want to do is when users click on the add document link that is on my display.asp page, pass the pin that I used to filter that web part, to the document library edit form.aspx page. Does anyone know how I can achieve that?

    1. Rovonda:

      It sounds like you are using a Web Part Connection. I avoid those, as they don’t give you much control.

      You should be able to get at the value, though, from the parameters and build your own link to the EditForm, passing the value.

      M.

  8. Hi Marc, great post it helped me a lot in JS but Im having this same issue when Im setting the editformurl property of a contenttype programmatically, its so rare Im just trying this code on a feature activation for a contenttype

    MyContentType.EditFormUrl = “_layouts/MyEditForm.aspx?strategy=edit&IsPQR=True”;
    MyContentType.Update();

    But when I click on the item created in the list, sharepoint redirect me to this url

    http://myServer/_layouts/MyEditForm.aspx?strategy=edit&amp;;ampIsPQR=True”;

    Did you had this problem ? thanks in advance

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.