Tuesday, June 22, 2010

Delete SharePoint file via web service

As you know, we can talk to SharePoint via web service, Microsoft has provided some default web services for us, also you can deploy your own web services. In this post I will the state the way to delete a file.

The web service method to delete an item is here, send a xml to SharePoint:
<Batch OnError=“Continue” PreCalc=“TRUE” ListVersion=“0” ViewName=“{EF2F5A21-0FD0-4654-84ED-112B4F5A48F8}”>
<Method ID=“1” Cmd=“Delete”>
<Field Name=“ID”>3</Field>
<Field Name=“FileRef”>
http://Server/[sites/][Site/]Shared Documents/File
</Field>
</Method>
</Batch>

Note that to delete a file, other than deleting other type of item, the field “FileRef” must be provided, the url cannot include any encoded characters(exclude / : * .etc that cannot used as folder/file name in SharePoint), for example the above url include a whitespace, if you encode it to ‘%20’, then SharePoint will report an error or do nothing.

Another wired thing is, In my environment, I can browse SharePoint using both machine IP(http://192.168.0.100/) and machine name(http://sharepointserver/), I can use the machine IP to fetch the file content, but when deleting a file, machine name must be used, or SharePoint will report that unable to find the file.

No comments: