Search code examples
c#sharepointsharepoint-2013splistitemsplist

Sharepoint List retrieve large data C#


Does TryGetList actually retrieves the entire list or does it work like IQueryable ? If my list have 1 million records is the following approach right?

  var list = web.Lists.TryGetList(<LIST NAME>);


  SPListItem item = list .Items.Cast<SPListItem>().FirstOrDefault(x => x["Id"] != null && x["Id"].ToString() == id && x["Status"] != null && x["Status"].ToString().ToLower() == "active");

Solution

  • TryGetList Will returns the SPList object. If the list exists in the corresponding web, will return the SPList else will return null.

    You can directly get the ListItem by using ID. Here you don't want to use the Linq filter. Similarly you can use the Caml Query.

    Get List Item By ID:

    SPList list = web.Lists.TryGetList(<LIST NAME>);
    SPListItem item = list.GetItemById(id);
    

    Get List Item by Caml Query:

      SPList list = web.Lists.TryGetList(<LIST NAME>);
      SPListItemCollection itemsCol=list.GetItems(new SPQuery(){Query= "<Where><And><Eq><FieldRef Name='ID'/><Value Type='Counter'>"+id+"</Value></Eq><Eq><FieldRef Name='Status'/><Value Type='Text'>Active</Value></Eq></And></Where>"});
      if(itemsCol!=null && itemsCol.count>0)
      {
        SPListItem item =itemsCol.FirstOrDefault();
      }
    

    Here you can download the Caml Builder.