Thursday, May 16, 2013

SharePoint - Iterate through large list

Retrieving list items from SharePoint list from code is the most basic thing, and every SharePoint developer is familiar with it.

This is the easiest way for retrieving items of a SharePoint list from code:


using(SPSite siteColl = new SPSite("SiteName"))
{
    using(SPWeb web = siteColl.OpenWeb())

        SPList list = web.RootWeb.Lists["ListName"];
        SPListItemCollection items = list.Items;
 
        foreach (SPListItem listItem in items)
        {
            Response.Write("Item title: " + listItem["Title"].ToString());
        } 
    }
}

But, this is a very poor solution, because this row 
SPListItemCollection items = list.Items; 
returns all items of a list at once. If your list has large amount of items (for example, more then 2000), then this can be a serious performance issue.If you have extremely large list, like 1 000 000 items, this code will certainly crash your server.


The solution is to using SPQuery class and its RowLimit property like this:

using(SPSite siteColl = new SPSite("SiteName"))
{
    using(SPWeb web = siteColl.OpenWeb())

        SPList list = web.RootWeb.Lists["ListName"];
 
        SPQuery query = new SPQuery();
        query.Query = "<Query><OrderBy><FieldRef Name='ID' /></OrderBy></Query>";
        //Scope="Recursive" retrieves items from all folders and subfolders in a list
        query.ViewAttributes = "Scope=\"Recursive\"";
        query.RowLimit = 100;

        do
        {
            SPListItemCollection items = list.GetItems(query);

            foreach (SPListItem listItem in items)
            {
                Response.Write("Item title: " + listItem["Title"].ToString());
            }

            query.ListItemCollectionPosition = items.ListItemCollectionPosition;

        } while (query.ListItemCollectionPosition != null);
        
    }
}


With SPQuery, execution is faster. This query returns all fields from a list, but you can make it even faster if you define only the fields that you need, and not all of them. RowLimit property of SPQuery class ensures that only specific number of items will be processed in one iteration.

Tuesday, May 7, 2013

Project Server 2010 Workflow - AppSettings Part II

In one of my previous posts, I've shown how to read Application Settings from workflow. 

But, that isn't always the best practice. It isn't recommended for users to edit machine.config, especially if you need to store a large amount of data in Application Settings.

Instead of tampering with machine.config, you can just open new SharePoint site in your workflow and instance configuration file of that Web Application. Here is how:





using (SPSite site = new SPSite(contextInfo.SiteGuid))
{
      System.Configuration.Configuration _config = WebConfigurationManager.OpenWebConfiguration("/", site.WebApplication.Name);
      
      string _MySetting = _config.AppSettings.Settings["MySettingName"].Value;
                   
}


Now, in a very simple way, we have read value of setting ("MySettingName") stored in Application Settings of our Web Application.