SharePoint中删除列表记录

方法1(快速,以理解,可以封装):

SPList spListQuestion = spWeb.Lists["Question List"];
for (int i = spListQuestion.Items.Count - 1; i >= 0; i--)
{
    spListQuestion.Items[i].Delete();
}

方法2(繁琐):

SPList spListQuestion = spWeb.Lists["Question List"];
string sDIDTitle = spListQuestion.Fields["DID"].InternalName;
SPQuery spQuery = new SPQuery();
spQuery.Query = "" + "" + spDocItem.ID.ToString() + "";
SPListItemCollection collListItems = spListQuestion.GetItems (spQuery);
for (int i = 0; i < collListItems.Count; i++)
{
    string ID = collListItems[i]["ID"].ToString();
    SPItem spItem = spListQuestion.GetItemById (Int16.Parse (ID) );
    spItem.Delete();
}

方法3(推荐):

SPList targetList = null;
try
{
    targetList = currentWeb.GetList (currentWeb.ServerRelativeUrl.TrimEnd ('/') + listUrl);
}
catch { }
if (targetList != null)
{
    StringBuilder sbDelete = new StringBuilder();
    sbDelete.Append ("<?xml version=\"1.0\" encoding=\"UTF-8\"?><Batch>");
    foreach (SPItem item in targetList.Items)
    {
        if (item["Title"] != null)
        {
            sbDelete.Append ("<Method>");
            sbDelete.Append ("<SetList Scope=\"Request\">" + targetList.ID + "</SetList>");
            sbDelete.Append ("<SetVar Name=\"ID\">" + Convert.ToString (item.ID) + "</SetVar>");
            sbDelete.Append ("<SetVar Name=\"Cmd\">Delete</SetVar>");
            sbDelete.Append ("</Method>");
        }
    }
    sbDelete.Append ("</Batch>");
    try
    {
        currentWeb.ProcessBatchData (sbDelete.ToString() );
        targetList.Update();
    }
    catch (Exception ex)
    { }
}

 

转载于:https://www.cnblogs.com/yixiaozi/p/3702614.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值