none
Retrieving list items from a specific view using CSOM RRS feed

  • Question

  • How can I query a specific view of a SharePoint List using the C# CSOM? I am dealing with SharePoint Online, so the only option is to use SharePoint.Client.

    I have done this using Javascript, and I know how to do this with SharePoint On-Premises, but I haven't found a way to do with for SharePoint online using C#.


    Wednesday, October 8, 2014 12:38 PM

Answers

  • Hi dkhouri,

    According to your description, my understanding is that you want to get items from list view using Client Object Model.

    Here is a code snippet for your reference:

       static void Main(string[] args)
            {
                ClientContext context = new ClientContext("http://sp2013sps/sites/test");
                List list = context.Web.Lists.GetByTitle("List66688");
                context.Load(list);
                context.ExecuteQuery();
                View view = list.Views.GetByTitle("All Items");
    
                context.Load(view);
                context.ExecuteQuery();
                CamlQuery query = new CamlQuery();
                query.ViewXml = view.ViewQuery;
    
                ListItemCollection items = list.GetItems(query);
                context.Load(items);
                context.ExecuteQuery();
                Console.Write(items.Count);
            }

    Here is a helpful thread for your reference:

    http://sharepoint.stackexchange.com/questions/101668/lient-object-caml-getting-items-of-a-specific-view

    Best Regards


    Zhengyu Guo
    TechNet Community Support


    Friday, October 10, 2014 9:27 AM

All replies

  • Hi

    Greetings.

    Hope it helps

    http://stackoverflow.com/questions/20758997/retrieve-list-item-using-csom-on-sharepoint-online-isnt-working


    Please remember to click 'Mark as Answer' on the answer if it helps you

    Wednesday, October 8, 2014 12:55 PM
  • hi Dkhouri,

    thanks for posting your issue, you can create a specific view of a list using CSOM and C#.

    Kindly find the code snippet below fort he same.

    Code for CSOM :- 

    // Starting with ClientContext, the constructor requires a URL to the 
    // server running SharePoint. 
    ClientContext context = new ClientContext("http:SiteUrl"); 
    
    // Assume the web has a list named "Announcements". 
    List announcementsList = context.Web.Lists.GetByTitle("Announcements"); 
    
    // This creates a CamlQuery that has a RowLimit of 100, and also specifies Scope="RecursiveAll" 
    // so that it grabs all list items, regardless of the folder they are in. 
    CamlQuery query = CamlQuery.CreateAllItemsQuery(100); 
    ListItemCollection items = announcementsList.GetItems(query); 
    
    // Retrieve all items in the ListItemCollection from List.GetItems(Query). 
    context.Load(items); 
    context.ExecuteQuery(); 
    foreach (ListItem listItem in items) 
    { 
     // We have all the list item data. For example, Title. 
      label1.Text = label1.Text + ", " + listItem["Title"]; 
    } 

    For C# 

    public CamlQuery CreateInventoryQuery(string searchSku)
    {
       var qry = new CamlQuery();
       qry.ViewXml =
          @"<View>
             <Query>
              <Where>
                <BeginsWith>
                  <FieldRef Name='SKU' />
                  <Value Type='Text'>" + searchSku + @"</Value>
                </BeginsWith>
              </Where>
            </Query>
           </View>";
       return qry;
    }

    Also, checkout below mentioned URLs for more details

    http://www.c-sharpcorner.com/UploadFile/sagarp/sharepoint-2013-caml-query-for-item-id-with-jquery/

    http://msdn.microsoft.com/en-us/library/ff798388.aspx

    I hope this is helpful to you, mark it as Helpful. If this works, Please mark it as Answered.

    Regards,

    Dharmendra Singh (MCPD-EA | MCTS)

    Blog : http://sharepoint-community.net/profile/DharmendraSingh

    Wednesday, October 8, 2014 12:56 PM
  • List.GetItems() takes in the CAML query, you can get the View from List.Views, Lookup the View.ViewQuery and use this CAML query in List.GetItems(camlFromView).

    Hope this helps

    Dave


    My SharePoint Blog - www.davehunter.co.uk - Twitter @davehunter

    Wednesday, October 8, 2014 1:03 PM
  • The above examples only show how to query for items from a list, which is not what I'm having issues with. I am trying to query items from a specific view in a list. 

    For example, say you have a SharePoint List with 10 different views. Each view has it's own filters + columns which have already been defined on SharePoint. I want to query one of those 10 views, not all list items.


    Wednesday, October 8, 2014 1:04 PM
  • Hi dkhouri,

    According to your description, my understanding is that you want to get items from list view using Client Object Model.

    Here is a code snippet for your reference:

       static void Main(string[] args)
            {
                ClientContext context = new ClientContext("http://sp2013sps/sites/test");
                List list = context.Web.Lists.GetByTitle("List66688");
                context.Load(list);
                context.ExecuteQuery();
                View view = list.Views.GetByTitle("All Items");
    
                context.Load(view);
                context.ExecuteQuery();
                CamlQuery query = new CamlQuery();
                query.ViewXml = view.ViewQuery;
    
                ListItemCollection items = list.GetItems(query);
                context.Load(items);
                context.ExecuteQuery();
                Console.Write(items.Count);
            }

    Here is a helpful thread for your reference:

    http://sharepoint.stackexchange.com/questions/101668/lient-object-caml-getting-items-of-a-specific-view

    Best Regards


    Zhengyu Guo
    TechNet Community Support


    Friday, October 10, 2014 9:27 AM