使用Web服务从sharepoint删除文件

时间:2012-11-15 09:56:15

标签: web-services sharepoint

我正在尝试从sharepoint文档库中删除文件。 我的应用程序是在C#中,它使用sharepoint的Web服务。 想知道如何做到这一点。 提前谢谢。

2 个答案:

答案 0 :(得分:2)

使用Web服务在SharePoint中删除文档

1.将网页参考添加到http:// [您的网站] / _ vti_bin / Lists.asmx

2.您需要文档ID,库名称和网址到要删除的文档

var spWebServiceLists = "http://[your site]/_vti_bin/Lists.asmx";
var listService = new Lists
{
    Credentials = CredentialCache.DefaultCredentials,
    Url = spWebServiceLists
};

string id = 10;
string library = @"Shared Documents";
string url = @"http://[your site]/Shared Documents/Test.docx";
string xml = "<Method ID='1' Cmd='Delete'>" + 
             "<Field Name='ID'>" + id + "</Field>" +
             "<Field Name='FileRef'>" + HttpUtility.UrlDecode(url) + "</Field>" +
             "</Method>";

/*Get Name attribute values (GUIDs) for list and view. */
System.Xml.XmlNode ndListView = listService.GetListAndView(library, "");
string strListID = ndListView.ChildNodes[0].Attributes["Name"].Value;
string strViewID = ndListView.ChildNodes[1].Attributes["Name"].Value;

/*Create an XmlDocument object and construct a Batch element and its
attributes. Note that an empty ViewName parameter causes the method to use the default view. */
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
System.Xml.XmlElement batchElement = doc.CreateElement("Batch");
batchElement.SetAttribute("OnError", "Continue");
batchElement.SetAttribute("ListVersion", "1");
batchElement.SetAttribute("ViewName", strViewID);

/*Specify methods for the batch post using CAML. To update or delete, 
specify the ID of the item, and to update or add, specify 
the value to place in the specified column.*/
batchElement.InnerXml = xml;

XmlNode item;
item = listService.UpdateListItems(library, batchElement);

我刚刚测试了这段代码,效果很好。

有关详细信息,请参阅以下链接

Lists.UpdateListItems Method

How to: Update List Items

答案 1 :(得分:1)

如果使用SharePoint 2010,则可以使用CSOM访问SharePoint Web服务。 This link可能有助于执行crud操作。如果您使用SharePoint 2013,还有CSOM API,它具有与2010年类似的功能。