Deleting Items from Sharepoint Document Libraries


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}

Published by

Phil Harding

SharePoint Consultant, Developer, Father, Husband and Climber.

13 thoughts on “Deleting Items from Sharepoint Document Libraries

  1. 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.

  2. 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”);

  3. Phil,

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

    Thanks

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

  5. public bool DeleteFile(int FileID)
    {
    try
    {
    //remove the file from the current item
    List olist = _context.Web.Lists.GetByTitle();

    ListItem item = olist.GetItemById(FileID);
    item.DeleteObject();
    _context.Load(olist, list => list.Title);

    _context.ExecuteQueryAsync(delete_succeedListner, null);
    }
    catch (Exception)
    {

    return false;
    }
    return true;
    }

  6. I’m running into an issue. Whenever I try to delete a file I get no errors, nor does the file get deleted. Anyone else has faced this issue before? I’ve triple checked my URL for errors and as far as I can tell, there are none…
    Is there a way to get some sort of result back from the webservice to give me an idea of where the error is happening..

  7. I’m having the same issue as Sessa (good URL, no errors thrown, file remains). Any ideas?

  8. Hello Sessa and ChrisEZ in sharepoint 2013 you need to use a different XML, passing also the file path to the owsfileref variable in the Method elements, as MSDN says.
    https://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spweb.processbatchdata.aspx#Anchor_1
    here is an example.
    public static string DeleteAllItems(SPSite spSite, SPWeb spWeb, SPList spList)
    {
    StringBuilder deletebuilder = BatchCommand(spList);
    return spWeb.ProcessBatchData(deletebuilder.ToString());
    }

    private static StringBuilder BatchCommand(SPList spList)
    {
    StringBuilder deletebuilder = new StringBuilder();
    deletebuilder.Append(“”);
    string command = “” + spList.ID +
    “{0}{1}Delete”;

    foreach (SPListItem item in spList.Items)
    {
    deletebuilder.Append(string.Format(command, item.ID.ToString(),item[“FileRef”].ToString()));
    }
    deletebuilder.Append(“”);
    return deletebuilder;
    }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s