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.
0 0 votes
Article Rating
Subscribe
Notify of
guest

Solve the maths problem shown below before posting: *

2 Comments
Inline Feedbacks
View all comments
André

Thanks!

Minh

It’s helpfull, thanks.