i am looking for a way to easily interchangeable the SPSite name and currently i have hard-coded as you can see below... so what is the best way to deal with this issue, i have DEV/QA/PROD box and i should be to change the url without compiling or deploying the SP solution.

using (SPSite site = new SPSite("http://devserver/sites/test/")) //<<<SPSite name...
            PageViewerWebPart pvwp = new PageViewerWebPart();
            pvwp.Title = "My Page Viewer Web Part";
            pvwp.ContentLink = "http://www.cnn.com";

Is this a web part that you are re-using across multiple pages and sites? Is this a custom Web Part?

If you are placing this web part on a page in SharePoint, you can use SPContext.Current.Site

using (SPSite mySite = new SPSite(SPContext.Current.Site.Url))



A note about the web.config: Every time you update your web.config file, the application pool gets recycled.

  • getting this error after i use the above code Value does not fall within the expected range. – Nick Kahn Aug 31 '11 at 16:24
  • 1
    Where are you running this code? Take a look at my questions in the beginning of my response – Tim Gabrhel Aug 31 '11 at 17:26

If you are developing a custom webpart you can create a webpart property to store the site url which you can use in your code.


I usually do some string manipulation from the HttpContext to get my basepath which I then combine with relative paths in my code.

        if (context.Request.ServerVariables["HTTPS"].ToString().ToUpper().Equals("ON"))
            basepath = "https://";
            basepath = "http://";
        basepath += context.Request.ServerVariables["SERVER_NAME"].ToString();
        if (!context.Request.ServerVariables["SERVER_PORT"].ToString().Equals("80") &&
            basepath += ":" + context.Request.ServerVariables["SERVER_PORT"].ToString();

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.