Performance is one of the key factors for the success of SharePoint implementations and when there is custom code involved to access SharePoint list data, we need to ensure that the data is cached (if it does not get modified too frequently but is used for reference a lot). One of the ways to do so is to use the PortalSiteMapProvider.
The following code snippet will help you develop web parts and page layouts that can take advantage of SharePoint’s caching mechanism. The list is assumed to be in the root web in the below example.
namespace NameSpaceName
{
public class ClassName
{
protected Repeater repData;
protected void Page_Load(object sender, EventArgs e)
{
// Create the query.
SPQuery curQry = new SPQuery();
curQry.Query = "<Where><Eq><FieldRef Name='Field_x0020_Name' /><Value Type='Text'>" + somevalue + "</Value></Eq></Where>";
//Call the list items from cache/list – provide the list name here
SiteMapNodeCollection lItems = getDataFromListCache("List Name", curQry);
// Either Bind it to the repeater control
repData.DataSource = lItems;
repData.DataBind();
//OR
//Render the list items in a label
foreach (PortalListItemSiteMapNode pItem in lItems)
{
//do your thing with the label
}
//Function to get cached SP List data from root web
private SiteMapNodeCollection getDataFromListCache(string listname, SPQuery filter)
{
// Get the Root Web object. This is where the list reside.
SPWeb rWeb = SPContext.Current.Site.RootWeb;
// Create an instance of PortalSiteMapProvider. Used for Caching SP data.
PortalSiteMapProvider ps = PortalSiteMapProvider.WebSiteMapProvider;
PortalWebSiteMapNode pNode = ps.FindSiteMapNode(rWeb.ServerRelativeUrl) as PortalWebSiteMapNode;
// Retrieve the items. If the cache for the list is empty, the cache gets created.
SiteMapNodeCollection pItems = ps.GetCachedListItemsByQuery(pNode, listname, filter, rWeb);
return pItems;
}
}
}
Imagine calling the list directly. Sound alright but now imagine every user making a call to a list and the number of users being in four/five figures! It will be expensive so make use of caching techniques where possible to optimise.

3 comments:
hey.. I have a question.. What if the item is added newly ? during that time will it check back the list?
Hi Pradeepa
I have noticed delays at times and the cache does refresh but not instantly.
This requires Microsoft.sharepoint.publishing.dll which is not available in SharePoint Foundation. Is there any workaround for this for SP Foundation?
Any inputs will be helpful.
Thanks
Post a Comment