Search code examples
c#visual-studiovisual-studio-2010sharepointsharepoint-2010

How do I find the root site URL in a Share Point 2010 project?


Is there a way to get the root url of the current server the Share Point application is hosted on? For example, if I want to load information from a site I'm currently typing:

SPSite site = new SPSite(http://dev3);

But when I move the development code onto the production server I have to manually replace the site URLs with the new server URLs:

SPSite site = new SPSite(http://sp2010);

I'm using C# in Visual Studio 2010.


Solution

  • If you want to get the hostname of the current machine, it's this:

    System.Net.Dns.GetHostName()
    

    If you're looking for something using the SharePoint Object Model, try this:

    new SPSite(SPServer.Local.Address.ToString())