Monday, November 16, 2009

Accessing SPList-cross web application, sitecollection and site

There might be some scenario where we might have to access list across site collection. We might have a situation that we have to search for a list across a site collection. We may have to check whether the list is present in any of the site collection or not and if present we may have to do some operation on the list. Below is the code snippet that may be useful in this scenario-
private string GetListvalue()
{
string strVal=string.Empty;
SPSecurity.RunWithElevatedPrivileges(delegate()
{
Uri uri = new Uri(SPContext.Current.Web.Url.ToString());
SPWebApplication webApp = SPWebApplication.Lookup(new Uri("http://" + uri.Host));
if (webApp != null && webApp.Sites != null && webApp.Sites.Count > 0)
{
using (SPWeb web = SPContext.Current.Site.RootWeb)
{
foreach (SPSite site in webApp.Sites)
{
foreach (SPWeb web in site.AllWebs)
{
if (IfListExists(web, "List Name"))
{
SPList lstStore;
SPListItemCollection items;
lstStore = web.GetList(list path);
SPQuery query = new SPQuery();
query.Query = "CAML query";
items = lstStore.GetItems(query);
strVal = items[0]["Column name"].ToString();
}
}
}
}
}
}
});
return strVal;
}
Here I am accessing a sigle column from the first row that will be returned by the query.
The IfListExists() method will use Linq to check whether the list exists or not. Sharepoint don't have any method or properties to check the existance of the list. Another alternative is to use the Extension Methods. Below is the definition of the IfListExists() method-
private static bool IfListExists(SPWeb web, string listName)
{
return web.Lists.Cast().Any(list => string.Equals(list.Title, listName));
}

Thats it.
Hopes it will help. :)

7 comments:

  1. Any idea of how to retreive items of the current logged in user from various doc libraris of different web applications. Also we need in normal list view format i.e. with context menu. Any product available for such requirement.
    Awaiting for your reply...

    ReplyDelete
  2. hey
    u can use out of the box list view webpart and filter them by current logged-in user.
    there is a column 'created by' in each list/library. u can match the same with current loggedin user for the filtering.

    ReplyDelete
  3. Thanks of your reply..
    I am asking about retreive items of the current logged in user from various doc libraris of different web applications or virtual server.

    ReplyDelete
  4. to access multiple doc library u will have to use SPSitedataQuery class instead of SPQuery. This also have some limitation. check out my below blog-

    http://deewaker-blogs.blogspot.com/2009/07/accessing-multiple-lists-across.html

    To check whether the item is created by current logged in user or not, u need to check whether web.CurrentUser is same as the 'CreatedBy' column name for a particular row.

    ReplyDelete
  5. Thanks for your reply..
    We have instance where we need retreive document library items from multiple site collections which lies under multiple web application.
    Is this possible by SP Object model?
    Kindly advice how we can acheive this?

    ReplyDelete
  6. Important site. Great to see some unique content being published here unlike thousands of fluffy blogs out there. Like custom essay writing service this will be a great writing help for students and for research fellows.

    ReplyDelete
  7. You have explained the content in a good manner. It seems like useful information for people who deals with web applications. Best essay writing service reviews is here to ask your doubts related to academic paper.

    ReplyDelete