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();


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);


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

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

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

  • what about server side? no code for it? – Happy Dec 4 '15 at 18:27
  • Nice little function. Didn't know about it. – Nadeem Yousuf Dec 5 '15 at 3:49

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.