I have been using this code to retrieve the URL of the top level site;

string result = String.Empty;
using (var service = GetSitesWebService())
    result = service.GetSite(mySubSiteURL);

if (!String.IsNullOrWhiteSpace(result))
    var doc = XDocument.Parse(result);
    rootUrl = (string)doc.Element("Site").Attribute("Url");

This works fine if the user has certain permissions (not sure what they are), but if the user only has "Contributor" access then the above call to GetSite fails with a 401 - Unauthorized.

Is there any alternative approach to getting the top-level site URL, given some arbitrary site URL? I would strongly prefer to be able to do this via the Web Services, but the Client Object Model might have to do if web services don't support this for low-privilege users.

3 Answers 3


excuse me for response with a question. But, how do you to connect to web services? You need the url to connect to WS, and then you have a url. In that case you can instantiate a SPSite class with the url. Yes you can get the site collection from any subsite. url = "server/sitecollection1/subsite1"

using(SPSite site = new SPSite(url))
  SPWeb web = site.RootWeb;  

In your sample, the users have acces to Site Collection?

Remember that you can to elevate privileges with

  • Hi Aortiz, I'm accessing the web services just via a service reference to "/_vti_bin/Sites.asmx". Your example code was using the Server Object Model though, so I won't be able to use it unfortunately. Jan 29, 2013 at 13:52

So if you are inside of Sharepoint Context (web part, timer job, web service etc..) then simply use:

var topLevelSiteURL = Microsoft.SharePoint.SPContext.Current.Site.Url
  • Hi Fox, unfortunately I need to access this information over the web services, not in the server object model. Jan 29, 2013 at 14:08
  • Where is the web services deployed? On same box as Sharepoint?
    – Fox
    Jan 29, 2013 at 14:15
  • I just mean the out-of-the-box SharePoint web services (Lists, Webs, Sites etc.) It's not a custom web service or anything like that. Jan 29, 2013 at 14:17
  • So what I'm asking is, where will your calling code be deployed?
    – Fox
    Jan 29, 2013 at 14:36
  • It's a client application, on the user's desktop. Jan 29, 2013 at 14:40

The client app it's web? in this case http://antoniolanaro.blogspot.com/2011/03/ecmascript-get-site-collection-server.html

  • Please could you put the substance of the story here, rather than just a link, which may be of no use to visitors at some point in the future. Thanks.
    – SPDoctor
    Jan 29, 2013 at 16:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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