More Soma Please…

Developers are in a race with the Universe to create better idiot-proof programs, while the Universe is trying to create better idiots. So far the Universe is winning.

Deleting Items from Sharepoint Document Libraries

Posted by Phil Harding on May 8, 2008

Deleting items from Sharepoint Document Libraries or Lists should be fairly straightforward, however there are a couple of gotcha’s depending on which method from Sharepoint Webservices you want to use and accordingly, there are differences in the formatted CAML command you would use.

Firstly a Document Library is a form of List, but the CAML command to delete items from these differ slightly.

To delete an item from a list you would format a CAML command like this;

<Batch OnError="Continue">
  <Method ID="1" Cmd="Delete">
    <Field Name="ID">1</Field>
  </Method>
</Batch>

The ID field contains the ID value of the list item to delete.

To delete an item from a document library you would format a CAML command like this;

<Batch OnError="Continue">
  <Method ID="1" Cmd="Delete">
    <Field Name="ID">1</Field>
    <Field Name="FileRef">http://spdev/Library Name/Msg-4218faa2-727e.xml</Field>
  </Method>
</Batch>

Of note here is the ID and FileRef fields. The ID field is required to be present but it’s value is ignored. The FileRef field contains the document items FileRef column value and this is used to ascertain the document item to delete. This value is the full URL of the document item to delete, however it should be noted that this URL must not be URL encoded.

In both cases it is important that there is no whitespace/linefeed characters in the Batch elements inner or outer XML, although the examples shown above contain LF characters for clarity.

Both the above commands would be executed using the Sharepoint Webservices UpdateListItems() method, the first parameter of which is the List/Document Library GUID and the second is the XML node containing the CAML Batch command.

The List/Document Library GUID should be formatted using braces and hyphen characters as show below.

{508B0C42-BC06-42A2-9531-2844FEAE9C00}

9 Responses to “Deleting Items from Sharepoint Document Libraries”

  1. Ziyad said

    How about deleting a document which is checked out by a user? I am not able to

  2. Ziyad, AFAIK you can’t delete a document checked out by another user, although it may be possible if you’re an an administrator or the system account.

  3. Faizal said

    How can I delete a folder in a document library?

    Thanks for the help.

  4. Faizal,
    I’ve not done this myself using CAML, but you could try somthing like this using the object model;

    SPWeb web = new SPSite("your site's url").OpenWeb();
    web.Folders[“document library name”].SubFolders.Delete(“folder name”);

  5. Faizal said

    Phil,

    I am unable to use the object model since I need to execute this code from a remote asp.net web server.

    Thanks

  6. [...] by Phil Harding on July 13, 2009 This is an update to a post I wrote on deleting list items from document libraries, prompted by a comment made on that post asking how to delete Folder(s) from within a document [...]

  7. Faizal, Delete a Folder from a SharePoint Document Library.

  8. Faizal said

    Thanks. I will test this out.

  9. Alan said

    Thanks, Couldn’t understand why I couldn’t delete a file until I saw that I needed the extra fileRef URL parameter.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>