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

5 comments:

  1. Hi,

    Retriving SPList is giving me error
    Object reference not set to an instance of an object.

    Stack Trace : at Microsoft.SharePoint.Library.SPRequest.SetVar(String bstrUrl, String bstrName, String bstrValue)
    at Microsoft.SharePoint.SPListItemCollection.EnsureListItemsData()
    at Microsoft.SharePoint.SPListItemCollection.get_Count()
    at DHA.Web.TopMenu.GetList()
    at DHA.Web.TopMenu.CreateChildControls()


    can you please tell me where the problem can be? Caching is working fine with SPListItemCollection it means no issue with code. Why it is not working with SPList.

    ReplyDelete
  2. at which line u got error?
    can you pls explain it more?? did u tried to debugged it?

    ReplyDelete
  3. Getting an error after retriving the list in cache and when i try to query it .
    Error-Object Reference Not set to instance

    ReplyDelete
  4. Very nice article, you can utilize out of the box list view web part and filter them by current logged-in user.

    ReplyDelete
  5. it seems that you have shared your ideas very clearly and it was really useful. custom essay writing service is here to ask you doubt on academic essay writing.

    ReplyDelete