如何在SharePoint对象模型中筛选ListItemCollection,排除匹配数据库文件名的项
Hey there! Let's work through this together. You've already fetched all 4 files into your listItems collection using CAML, and now you need to filter out any items where the filename matches what's stored in your database table. Here's a clean, efficient way to get this done:
Step 1: Fetch excluded filenames from your database
First, you'll want to pull all the filenames you need to exclude into a collection. Using a HashSet<string> is ideal here because it makes lookups super fast—way quicker than a regular List<string> when dealing with even moderately sized datasets.
// Replace this with your actual database call to get excluded filenames HashSet<string> excludedFileNames = GetExcludedFileNamesFromDatabase(); // Optional: If you need case-insensitive matching (e.g., "File.txt" vs "file.txt"), initialize the HashSet like this: // HashSet<string> excludedFileNames = new HashSet<string>(GetExcludedFileNamesFromDatabase(), StringComparer.OrdinalIgnoreCase);
Step 2: Filter your ListItemCollection
Now that you have your excluded filenames, you can filter the listItems collection. We'll use LINQ for this (make sure you have using System.Linq; at the top of your file), but I'll also include a loop-based alternative if you prefer that.
LINQ Approach (Clean & Concise)
// Cast the ListItemCollection to an IEnumerable<ListItem> so we can use LINQ methods var filteredItems = listItems.Cast<ListItem>() .Where(item => !excludedFileNames.Contains(item["FileLeafRef"].ToString())) .ToList();
Loop-Based Alternative (For Those Who Prefer Explicit Iteration)
List<ListItem> filteredItems = new List<ListItem>(); foreach (ListItem item in listItems) { // Get the filename from SharePoint (FileLeafRef is the internal field for the filename) string fileName = item["FileLeafRef"].ToString(); // Only keep the item if its filename isn't in the excluded list if (!excludedFileNames.Contains(fileName)) { filteredItems.Add(item); } }
Full Integrated Code
Here's how it all fits into your existing code:
CamlQuery camlQuery = new CamlQuery(); // Note: You don't need to escape the angle brackets when using a verbatim string (@) camlQuery.ViewXml = @"<View Scope='RecursiveAll'> <Query> </Query> </View>"; camlQuery.FolderServerRelativeUrl = folder.ServerRelativeUrl; ListItemCollection listItems = list.GetItems(camlQuery); clientContext.Load(listItems); clientContext.ExecuteQuery(); // Fetch excluded filenames from database HashSet<string> excludedFileNames = GetExcludedFileNamesFromDatabase(); // Filter the items var filteredItems = listItems.Cast<ListItem>() .Where(item => !excludedFileNames.Contains(item["FileLeafRef"].ToString())) .ToList();
Quick Notes
FileLeafRefis SharePoint's internal field name for the filename (including extension). If you're using a custom field to store filenames, replace this with the internal name of your custom field.- Always make sure the filenames from your database match the casing of the SharePoint filenames, or use the case-insensitive
HashSetinitialization if needed to avoid missing matches.
内容的提问来源于stack exchange,提问作者Neo

