views:

1339

answers:

2

I'm not sure how to use the GroupBy clause when querying a list. The SPListItemCollection or datatable looks exactly the same regardless of the groupby clause.

SPQuery query = new SPQuery();
query.Query = "<GroupBy><FieldRef Name=\"Area\"/></GroupBy>";

DataTable result = list.GetItems(query).GetDataTable();

// result.Rows.Count = Same as ungrouped query

Is GroupBy only supported by Lists.asmx webservice?

Found a reference on MS Social which suggests it's not supported on the SPSiteDataQuery object http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/ab8df6f5-35a0-401e-88cb-3eb31362bf0c/

+1  A: 

I believe that clause is more related to how the grouping will be displayed in the UI. As far as the data returned goes, you might/should get some sorting but that's about it.

Regarding the UI, there is the GroupLimit element which limits how many groups are returned. There is also the Collapse element which has no meaning when used with SPQuery.

How were you hoping the data would be different?

Alex Angas
It looks you're right, I was naively hoping that the dataset might have a column indicating the grouped value, or there would be a property on the listitem collection that would return a hierarchical view of the data to support the grouping structure.
Edward Wilde
Oh! No way, SharePoint's not that helpful I'm afraid!
Alex Angas
A: 

For instance, let us take a State List which holds all the states of India. States are grouped under different zones i.e. North, South, East & West. If I need to retrieve all the States for North zone then my SP Query would look somewhat like,

SPQuery stateQuery = new SPQuery(); stateQuery.Query = "North"; SPListItemCollection stateCol = StateList.GetItems(stateQuery);

eliza sahoo