Take the 2-minute tour ×

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.

share|improve this question

3 Answers 3

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

share|improve this answer
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 '13 at 16:29

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

share|improve this answer
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. –  C.McAtackney Jan 29 '13 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
share|improve this answer
Hi Fox, unfortunately I need to access this information over the web services, not in the server object model. –  C.McAtackney Jan 29 '13 at 14:08
Where is the web services deployed? On same box as Sharepoint? –  Fox Jan 29 '13 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. –  C.McAtackney Jan 29 '13 at 14:17
So what I'm asking is, where will your calling code be deployed? –  Fox Jan 29 '13 at 14:36
It's a client application, on the user's desktop. –  C.McAtackney Jan 29 '13 at 14:40

Your Answer


