Blog about Microsoft technologies, Office System, SharePoint, MOSS, WSS, Search and "The new world of work"
in

How to: Access a SPList cross Web Application, Site Collection and Site

When creating large Portal solutions with SharePoint you need to think about using more then one Site Collection. By using more then one Site Collection you can isolate information within the Portal and give it its own security settings, content databases, masterpages and more. But in some cases you will need to access a SPList from another Site Collection. For example when you store global data in a listing in one place needed through out the whole Portal. The following code finds the SPList object based on its url even accross Web Application, Site Collection and Site. We have been using this in some of our own projects.

public SPList GetList(string url)
{
  Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(delegate()
  {
    Uri uri= new Uri(url);

    SPWebApplication wa = SPWebApplication.Lookup(new Uri("http://" + uri.Host));

    if (wa != null && wa.Sites != null && wa.Sites.Count > 0)
    {
      foreach (SPSite site in wa.Sites)
      {
        foreach (SPWeb web in site.AllWebs)
        {
          SPList list = web.GetList(url);

          if (list != null)
          {
            return list;
          }
        }
      }
    }
  });

  return null;
}

It checks based on the url of the SPList object which Web Application it needs to access. In their it loops through all available Site Collections and Sites looking for the SPList object. Because users accessing the Portal can have minimal rights, you will need to use the method Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges to access other Web Applications, Site Collections and Sites. This will not use the current context of the user but in stead the System user having more rights.

» Trackbacks & Pingbacks

    No trackbacks yet.
Trackback url for this post:
http://www.bloggix.com/blogs/microsoft/trackback.ashx?PostID=393

» Comments

    No comments yet. Be the first to comment!

» Leave a Comment

(required) 
(optional)
(required) 

Submit

Subscriptions

News

SlideShare.net
I'm using SlideShare.net for sharing my presentation material. You can find it here

Dutch SharePoint User Group
The Dutch SharePoint User Group is an independent social network for people to collaborate and share information around SharePoint Technology and products. By learning from others you can improve your skills and expertise, around areas, which are then again a benefit to the social network. This platform is a community platform for SharePoint developers, architects and designer who want to share information, tricks, code and content. Its also a good starting point for people without SharePoint knowledge who are interested in SharePoint Technology. Read more

Oktober 2008 i'm working for a new company called Sparked. The new world of work causes organizations to re-think about their ways of communication and interaction with their customers.

Increasing usage of web technology and the new generation workers brings changes into these organizations.

Sparked helps these organizations to accelerate adoption of this technology platform to effectively use it around content management, content exposure and collaboration.


April 2008 i have been to the first SharePoint Conference in Dubai. Dubai is an overwhelming town with a lot of passionate people. I had a great time. Thank you my friends!



You can also find my on Facebook

Tags

Alexander Meijers

Previous Blog about SharePoint