0

From my SP library, I would need to retrieve a list of meetingID grouped by MeetingID. I tried several attempts with CAML but it didn't work:

CamlQuery query = new CamlQuery();
                    query.ViewXml = @"<View>  
                                        <Query>
                                            <Where>        
                                            <And>                                        
                                                <And>
                                                    <Eq>
                                                        <FieldRef Name='FSObjType'/> 
                                                        <Value Type='Counter'>0</Value>
                                                    </Eq>
                                                    <Geq>
                                                        <FieldRef Name='Modified'/>
                                                        <Value IncludeTimeValue = 'TRUE' Type = 'DateTime'>2016-01-01T15:55:52Z</Value>           
                                                    </Geq>  
                                                </And>
                                                <IsNotNull>
                                                    <FieldRef Name='Title' />                                              
                                                </IsNotNull>
                                            </And>
                                        </Where>                                       
                                        <GroupBy Collapse='True'>
                                            <FieldRef Name='MeetingID' />
                                        </GroupBy>                                        
                                        <OrderBy>
                                            <FieldRef Name='DocID' />                                       
                                        </OrderBy>
                                    </Query> 
                                    <ViewFields>
                                        <FieldRef Name='MeetingID'/>
                                    </ViewFields> 
                                </View>";

                    ListItemCollection items = list.GetItems(query);
                    cc.Load(items, o => o.Include(p => p["MeetingID"]));
                    cc.ExecuteQuery();

Considering that the CAML is returning the MeetingID and plenty of system fields, I had to use the Include syntax. However,on my 1500 items in SP, none of them have been grouped (I should have only 50 unique MeetingID). Again probably related to the system fields which prevent the grouping to occur...

I also tried:

cc.Load(items, o => o.Include(p => p["MeetingID"]).GroupBy(g=>g["MeetingID"]));

But this one return an error...

I also tried to see if a property similar to ViewFieldsOnly exist in CSOM but it doesn't seem to be the case.

Your help will be appreciated

0

I tested with the code snippet and actually, the CAML Query have done groupby as expected:

using (var clientContext = new ClientContext("https://zheguo.sharepoint.com/sites/dev"))
            {
                clientContext.Credentials = new SharePointOnlineCredentials(userName, securePassword);
                Web web = clientContext.Web;
                clientContext.Load(web);
                clientContext.ExecuteQuery();
                List targetList = clientContext.Web.Lists.GetByTitle("doc3");
                clientContext.Load(targetList);
                clientContext.ExecuteQuery();
                CamlQuery query = new CamlQuery();
                query.ViewXml = @"<View>  
                                        <Query>
                                            <Where>        
                                            <And>                                        
                                                <And>
                                                    <Eq>
                                                        <FieldRef Name='FSObjType'/> 
                                                        <Value Type='Counter'>0</Value>
                                                    </Eq>
                                                    <Geq>
                                                        <FieldRef Name='Modified'/>
                                                        <Value IncludeTimeValue = 'TRUE' Type = 'DateTime'>2016-01-01T15:55:52Z</Value>           
                                                    </Geq>  
                                                </And>
                                                <IsNotNull>
                                                    <FieldRef Name='Title' />                                              
                                                </IsNotNull>
                                            </And>
                                        </Where>                                       
                                        <GroupBy Collapse='True'>
                                            <FieldRef Name='MeetingID' />
                                        </GroupBy>                                        
                                    </Query> 
                                    <ViewFields>
                                        <FieldRef Name='MeetingID'/>
                                        <FieldRef Name='ID'/>
                                        <FieldRef Name='Title'/>
                                    </ViewFields> 
                                </View>";

                ListItemCollection items = targetList.GetItems(query);
                clientContext.Load(items, o => o.Include(p => p["MeetingID"], p=>p["ID"],p=>p["Title"] ) );
                clientContext.ExecuteQuery();
                foreach (var item in items)
                {
                    Console.WriteLine(item["MeetingID"]);
                    Console.WriteLine(item["Title"]);
                }


            }   

Here is the sample data in my library for a test:

enter image description here

There are 2 unique MeetingID- "Meeting001" "Meeting002", you can see the result from Console.WriteLine, the same MeetingID item will be grouped together: enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.