Group By SPListItem in SharePoint ListItemCollection


The CAML Query does not support group by object collection. The other workaround is to get the object and group by LINQ query

try
{
using (SPSite oSiteCollection = new SPSite(webUrl))
{
using (SPWeb oWebsite = oSiteCollection.OpenWeb())
{
SPList listName = oWebsite.Lists.TryGetList("MCS");
if (listName != null)
{
SPView myView = listName.Views["GroupBy"];
if (myView != null)
{
SPQuery qry = new SPQuery(myView);
SPListItemCollection myItemsList = listName.GetItems(qry);
var results = (from SPListItem item in myItemsList
group item by item["Group Number"]
into grp
select new
{
GroupID = grp.Key.ToString(),
ListItemsData = grp
});
foreach (var listData in results)
{
MessageBox.Show(listData.GroupID);
foreach (var item in listData.ListItemsData)
{
MessageBox.Show(item["XmlText"].ToString());
}
}
}
}
}

}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}

Advertisements

One thought on “Group By SPListItem in SharePoint ListItemCollection

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s