Get List Items by Field Value

SharePoint Short #6
To retrieve all SPListItem objects that have a specified field value, instead of writing a CAML query, use the following LINQ statement:

public List<SPListItem>  GetItemsContainingValue(SPList list, Guid fieldId, string match)
{
    List<SPListItem> matchingItems =
        (from SPListItem listItem in list.Items
         where
             listItem.Fields.Contains(fieldId) &&
             listItem[fieldId] != null &&
             listItem[fieldId].ToString().Equals(match, StringComparison.InvariantCultureIgnoreCase)
         select listItem).ToList<SPListItem>();
 
    return matchingItems;
}
This entry was posted in SharePoint Shorts and tagged , . Bookmark the permalink.

2 Responses to Get List Items by Field Value

  1. André says:

    Thanks!

  2. Minh says:

    It’s helpfull, thanks.

Leave a Reply

Your email address will not be published. Required fields are marked *

Solve the maths problem shown below before posting: *