Tuesday, November 17, 2009

Caching with SPList-Sharepoint

In big sharepoint projects, it may be possible that performance of our application get reduce because of large amount of data stored in sharepoint list.In such scenario, calling Sharepoint list through sharepoint object model will harm the performance. caching can be useful in such scenario.
To implement caching we can use PortalSiteMapProvider class. The GetCachedListItemsByQuery() method of PortalSiteMapProvider Class to get the cached data.Below is the code snippet-
string strVal;
SPWeb web=SPControl.GetContextWeb(context);
SPQuery query = new SPQuery();
query.Query = "CAML Query";
PortalSiteMapProvider provider = PortalSiteMapProvider.CurrentNavSiteMapProviderNoEncodePortalWebSiteMapNode node = provider.FindSiteMapNode(web.ServerRelativeUrl.ToString()) as PortalWebSiteMapNode;
SiteMapNodeCollection items = provider.GetCachedListItemsByQuery(node, "List Name", query, web);
if (items != null)
{
foreach (PortalListItemSiteMapNode item in items) {
strVal = item["column name"].ToString();
}
}
Now strVal will get the cached data.
Alternatevely we can use HttpRunTime.cache, provided by ASP.Net, to implement caching in following way-
SPList lstStore;
SPListItemCollection items;
if (HttpRuntime.Cache["configstorelist"] == null)
{
lstStore = web.GetList(strlistPath);
HttpRuntime.Cache.Add("configstorelist", lstStore, null, DateTime.MaxValue, TimeSpan.FromMinutes(10),System.Web.Caching.CacheItemPriority.Default, null);
}
else
{
lstStore = (SPList)HttpRuntime.Cache["configstorelist"];
}
Here we will check if the Cache object is null or not. if it is null, it will call getList() method, else it will take the data from Cache object.One more point is to use GetList() method instead of SPList.Lists["name"] as SPList.Lists iterate through all the list so it may reduce the performance in large projects. So it is advisable to use GetList().

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. :)