I want to check my url to have a source and redirect to it.

I have a button for add a ListItem, i want where doesn't have a source in URL the page refresh, where have a source in URL --> redirect me to the source..

This is my URL :


code redirect me to


I want to redirect me to Source=http%3A%2F%2Fsf%2Dspsdev02%2FTestSite%2FLists%2FlinkBi%2FAllItems%2Easpx

  • Please add some of more details as it is unclear what are you trying which methodology you want to apply. – Aakash Maurya May 31 '16 at 7:32
  • I added more details. – Goshky May 31 '16 at 7:40

To go more precise I would like to code it in following manner.

if (Request.QueryString.Count > 0) {

    string urlSource = Request.QueryString["Source"];

    if (!String.IsNullOrEmpty(urlSource))
        int pos = urlSource.LastIndexOf(@"/");
        String newUrl = urlSource.Substring(0, pos + 1) + "/AllItems.aspx";
        SPUtility.Redirect(newUrl, SPRedirectFlags.Default, HttpContext.Current);

If I understood you correctly, then following is your requirement.

  1. Checking whether Source exists in query string or not
  2. If it exists, then redirect to that URL


string source = Request.QueryString["Source"];

if (source != "")
   SPUtility.Redirect(source, SPRedirectFlags.Default, HttpContext.Current);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.