码迷,mamicode.com
首页 > 其他好文 > 详细

SharePoint中删除列表记录

时间:2014-05-01 21:01:37      阅读:448      评论:0      收藏:0      [点我收藏+]

标签:style   blog   class   code   java   tar   javascript   color   get   int   string   

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

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

方法2(繁琐):

bubuko.com,布布扣
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();
}
bubuko.com,布布扣

方法3(推荐):

bubuko.com,布布扣
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)
    { }
}
bubuko.com,布布扣

 

SharePoint中删除列表记录,布布扣,bubuko.com

SharePoint中删除列表记录

标签:style   blog   class   code   java   tar   javascript   color   get   int   string   

原文地址:http://www.cnblogs.com/yixiaozi/p/3702614.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!