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.