Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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";
            this.Controls.Add(pvwp);
        }
share|improve this question

3 Answers 3

up vote 4 down vote accepted

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))
{
    ...
} 

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spcontext_members.aspx

Edit

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

share|improve this answer
    
getting this error after i use the above code Value does not fall within the expected range. –  Abu Hamzah 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.

share|improve this answer
    
can you show me how to use –  Abu Hamzah Sep 1 '11 at 16:33
    
An example: wictorwilen.se/Post/… –  Anita Boerboom Sep 2 '11 at 7:38

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://";
        }
        else
        {
            basepath = "http://";
        }
        basepath += context.Request.ServerVariables["SERVER_NAME"].ToString();
        if (!context.Request.ServerVariables["SERVER_PORT"].ToString().Equals("80") &&
            !context.Request.ServerVariables["SERVER_PORT"].ToString().Equals("443"))
        {
            basepath += ":" + context.Request.ServerVariables["SERVER_PORT"].ToString();
        }
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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