开发者

Can I delete a Sharepoint List, then recreate it with the same GUID?

I'm trying to create a C# program using a Sharepoint Web Service reference that will accept user input every day and update a list. The data is potentially different every day so I need a reliable way to delete every item in the list before the update statement is sent for the new items.

The only way to delete an entry is by referencing its item ID number. I tried just creating a loop that'll generate a delete statement that blankets ID 1-50, but since the list always increments that only works until 50 items go through. In my Googling I've found that the only way to reset a list to ID 1 is to delete it and recreate it. Unfortunately, if I do that, it'll have a different GUID and my program will no longer be able to call it the next time it's run.

Is there so开发者_开发问答me solution I'm not seeing here?


You can't re-create a list with a specified GUID.

But from your comment you're off on a tangent here if the only reason you need to reset the ID to 1 is to help out with your empty list operation.

The XML returned from GetListItems() is in the format

<listitems xmlns:s="uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882" 
   xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882" 
   xmlns:rs="urn:schemas-microsoft-com:rowset" xmlns:z="#RowsetSchema" 
   xmlns="http://schemas.microsoft.com/sharepoint/soap/">
   <rs:data ItemCount="4">
      <z:row ows_Number_Field="6555.00000000000" 
         ows_Created="2003-06-18T03:41:09Z" 
         ows_ID="100" ows_owshiddenversion="3" />
      <z:row ows_Number_Field="78905456.0000000" 
         ows_Created="2003-06-18T17:15:58Z" 
         ows_ID="101" ows_owshiddenversion="2" />
         ...
   </rs:data>
</listitems>

So to 'reliably' loop through that is something like (not tested but you get the idea)

// Call GetListItems and setup XmlDocument with results
System.Xml.XmlNode nodeListItems = 
    listService.GetListItems
    (listName, viewName,query,viewFields,rowLimit,queryOptions,null);

/*Loop through each node in the XML response and display each item.*/
foreach (System.Xml.XmlNode listItem in nodeListItems)
{
   Console.WriteLine("ID:{0}",listItem.getAttribute("ows_ID"));
}

See this MSDN article for more inspiration

Further - to delete all these records with one call you can do a batch update by sending UpdateListItems by building up an XML fragment something like this in the for loop above.

<Batch>
   <Method ID='1' Cmd='Delete'><Field Name='ID'>100</Field></Method>
   <Method ID='2' Cmd='Delete'><Field Name='ID'>101</Field></Method>
</Batch>

Notes

  • the "Method ID='XXX'..." has to be sequential +1 for each batch method.
  • the "Field Name='ID'100..." matches the ID's of the list you found in the for loop above.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜