we have a user control on the master page (this masterpage is applied to all sites in the web application).

User control code:

 SPSite spsite = new SPSite(SPContext.Current.Web.Url);
                SPWeb spWeb = spsite.RootWeb;
                SPList spList = spWeb.Lists.TryGetList("MyMenu");
                if (spList != null)
                    SPQuery qry = new SPQuery();
                    qry.Query =
                    @"   <OrderBy>
      <FieldRef Name='Title' />
                    qry.ViewFields = @"<FieldRef Name='Title' /><FieldRef Name='URL' />";
                    SPListItemCollection listItems = spList.GetItems(qry);

The MyMenu list is present in the ROOT site collection's (the 1st site collection which we create after creating webapplication) ROOT site.

How do I write code that will allow me to access this list from any place in the web application? At present if we open a other site collection (attach debugger to the w3p) then the list is returned as null.

3 Answers 3


We normally use a setting in for example the property bag of the web application or farm for storing the "root" site collection URL or GUID.

Then you can read this in your user control and from that open the correct site


The problem with your code is that SPContext.Current.Web will obviously return the current web, not the one in the "Root" site collection. Suppose you have two collections

  • http//sharepointFarm:777/
  • http//sharepointFarm:777/sites/myCollection

Now, if you browse a website under http_//sharepointFarm:777/sites/myCollection the context will point to that website -> this also means that the next line

SPWeb spWeb = spsite.RootWeb;

will point to the root web of the http//sharepointFarm:777/sites/myCollection collection which is "http//sharepointFarm:777/sites/myCollection".

Since your list is on the other collection your code will fail to find it.

To avoid the problem you could use the web application url which in most situations is also the url of the root site collection.

string webAppUrl = 
   SPContext.Current.Site.WebApplication.AlternateUrls.First(x => x.UrlZone == this.Site.Zone).IncomingUrl;

The basic idea is to get the url you need from the collection of alternate urls of your web application. The linq filter is needed to ensure that you chose the url that match the zone of your request. After that, you simply use .IncommingUrl to get the base url of your web application. So, to reasume, your code would look like this:

string webAppUrl = 
   SPContext.Current.Site.WebApplication.AlternateUrls.First(x => x.UrlZone == this.Site.Zone).IncomingUrl;
SPSite spsite = new SPSite(webAppUrl);
SPWeb spWeb = spsite.RootWeb;
SPList spList = spWeb.Lists.TryGetList("MyMenu");
if (spList != null)

Notice: you will probably still need to use RunWithElevatedPrivileges to bypass any security related problem the above code may trigger.

  • is it safe to use this?
    – variable
    Commented Apr 2, 2014 at 9:14
  • what do you mean by safe, @variable? As I said, the main problem you may have is user permissions preventing your access to the other site, but you should have that no mater how you get your root site collection url. Do you have any specific concern? I had used the above instruction in a web service to recover the url of the root site collection with no problem (other than security).
    – SPArcheon
    Commented Apr 2, 2014 at 9:25
  • Yes, I would like to know if I have to use something like runwithelevated priviledges for this code?
    – variable
    Commented Apr 2, 2014 at 9:47
  • As I said, it really depends on where the above code will run. An normal user may not have the required permissions to perform the required operations (for example, it may not have access to the root site collection). In that case you may need to wrap the code in a SPSecurity.RunWithElevatedPrivileges block to elevate the code to be run under the AppPool identity: that should give every permission you may need.
    – SPArcheon
    Commented Apr 8, 2014 at 13:44

SPContext.Current.Site.WebApplication.Sites[0] is the root site collection of the current web application. (See e.g. here for an MSDN tutorial using it.)

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.