You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在C#中从SharePoint.Client.ListItemCollection删除行

Got it, let's work through this—since you need to filter out specific ListItems from your ListItemCollection entirely in memory (no changes to the actual SharePoint list), here are practical solutions for both C# and JavaScript, the most common languages for CSOM:

C# Solution (Client-Side Object Model)

First off, the native ListItemCollection is read-only, so you can't directly remove items from it. The workaround is to convert it to a mutable collection (like List<ListItem>) first, then apply your filter.

Example: Remove items where a column matches a specific value

Let's say you want to remove all items where the "Status" column equals "Archived":

// Assume you already have your retrieved ListItemCollection stored as listItems
var filteredItems = listItems.Cast<ListItem>()
                            .Where(item => 
                                // Handle null cases to avoid errors
                                item["Status"]?.ToString() != "Archived")
                            .ToList();

// filteredItems now contains only the items you want—all in memory

Alternative: Directly remove items from a mutable list

If you prefer to explicitly remove items instead of creating a new list:

var itemsToRemove = listItems.Cast<ListItem>()
                             .Where(item => 
                                 item["Status"]?.ToString() == "Archived")
                             .ToList();

// Convert the read-only collection to a mutable list
var mutableItemList = listItems.Cast<ListItem>().ToList();

foreach (var item in itemsToRemove)
{
    mutableItemList.Remove(item);
}

Key Notes for C#:

  • Always check for null when accessing column values to avoid NullReferenceException.
  • If your column uses a non-string type (like numbers or dates), adjust the comparison logic accordingly (e.g., (int)item["Priority"] != 3).
JavaScript Solution (JSOM)

Just like in C#, the JSOM ListItemCollection doesn't support direct modifications. You'll need to convert it to an array, then filter out unwanted items.

Example: Filter out items with a specific column value

Let's remove items where the "Priority" column is set to "Low":

// Assume you've retrieved your ListItemCollection and stored it in listItems
var filteredItems = [];
var enumerator = listItems.getEnumerator();

while (enumerator.moveNext()) {
    var item = enumerator.get_current();
    var priority = item.get_item('Priority');
    
    // Skip items we want to remove
    if (priority !== 'Low') {
        filteredItems.push(item);
    }
}

// filteredItems holds your in-memory filtered list

ES6 Shortcut (if your environment supports it)

If you're working in a modern browser or using transpilation:

var itemsArray = [];
var enumerator = listItems.getEnumerator();

// Convert collection to array first
while (enumerator.moveNext()) {
    itemsArray.push(enumerator.get_current());
}

// Filter out unwanted items
var filteredItems = itemsArray.filter(item => {
    var status = item.get_item('Status');
    return status !== 'Archived';
});

Key Notes for JavaScript:

  • Use get_item(columnInternalName) to access column values—make sure you're using the column's internal name (not display name) to avoid issues.
  • For non-string column types (like dates or numbers), adjust the comparison to match the data type (e.g., item.get_item('DueDate') < new Date()).
Core Takeaway

The main idea here is that ListItemCollection is read-only by design, so you have to convert it to a mutable structure (list in C#, array in JS) before you can remove items. All these operations stay strictly in memory—your SharePoint list data remains untouched.

内容的提问来源于stack exchange,提问作者Gourav

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 06:56:58