0

I wish to add Query string to current url of site when i click on button. I dont know how its done in SharePoint but know how to do in asp.net like below

string url = HttpContext.Current.Request.Url.AbsoluteUri;
        var uriBuilder = new UriBuilder(url);
        var query = HttpUtility.ParseQueryString(uriBuilder.Query);
        query["Myparam"] = "test";
        uriBuilder.Query = query.ToString();
        url = uriBuilder.ToString();
        Response.Redirect(url);

Update1

I tried below code in selectedIndexChange event of option button

  protected void RadioButtonList_SelectedIndexChanged(object sender, EventArgs e)
    {

        Page.ClientScript.RegisterStartupScript(GetType(), "test",
            "<script type=\"text/javascript\">AddQueryString('" +
            RadioButtonList.SelectedValue).ToString() + "');</script>)");
    }

Then in .ascx file i wrote below

<script type="text/javascript">
function AddQueryString(querystring) {

           SP.SOD.executeFunc("SP.js", "SP.ClientContext", function()    
    {    
        SP.Utilities.UrlBuilder.replaceOrAddQueryString(window.location.href, "param1", querystring);

   });
}</script>

But its not updating url With Query string. Why? I have checked that there are no errors in console and code executes sucessfully.

1
  • the function returns you a string. after you need to update the current url. Ex : var newUrl = AddQueryString("test"); window.location.href = newUrl; Commented Dec 7, 2015 at 13:33

1 Answer 1

3

In JavaScript, there is a native SharePoint function called replaceOrAddQueryString(url, key, value) and returns the url.

Ex: SP.Utilities.UrlBuilder.replaceOrAddQueryString("http://mysite", "level", "2")

will return : http://mysite?level=2

SP.Utilities.UrlBuilder.replaceOrAddQueryString("http://mysite?level=2", "sublevel", "1")

will return : http://mysite?level=2&sublevel=1

2
  • what about server side? no code for it? Commented Dec 4, 2015 at 18:27
  • Nice little function. Didn't know about it. Commented Dec 5, 2015 at 3:49

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.