Deleting Documents with SPServices

A while back I was writing a function using SPServices that would delete a document from a SharePoint library when a user clicked a button. I set up the SPServices function the same way I’d done for other lists, but it wasn’t working. Turns out for documents (as opposed to list items) you must include the FileRef field when selecting what to delete. It took me a while to figure this out because the ID is the only field needed to delete list items.

Here’s an example function that will delete a file from a SharePoint library using jQuery 1.7.1 and SPServices 0.7.1a. You’ll need to call the function by passing in the ID, the FileRef, and the name of the library. Notice the batchCmd variable includes both the ID and FileRef fields:

function deleteFile( itemID, fileRef, listName ) {

	// This is the command needed to delete the specified file. It uses the ID and the URL of the file name. These values must be passed into this function when calling it.
	var batchCmd = "<Batch OnError='Continue'><Method ID='1' Cmd='Delete'><Field Name='ID'>" + itemID + "</Field><Field Name='FileRef'>" + fileRef + "</Field></Method></Batch>";

	// Use SPServices to delete the file.
		operation: "UpdateListItems",
		async: false,
		listName: listName,
		updates: batchCmd,
		completefunc: function ( xData, Status ) {

			// Check the error codes for the web service call.
			$( xData.responseXML ).SPFilterNode( 'ErrorCode' ).each( function(){
				responseError = $( this ).text();

				// If the error codes indicate that the file was successfully deleted, inform the user.
				if ( responseError === '0x00000000' ) {
					alert( "The file has been successfully deleted." );

				// If the error codes indicate that the file was NOT successfully deleted, inform the user.
				else {
					alert( "There was an error trying to delete the file." );

To call this function, you could do something like this:

deleteFile( '23', 'http://server/documents/mydocument.docx', 'Documents' );

Of course, this is a very generic example. You’d probably want to use this in a more dynamic manner, such as when displaying a list of documents. With a data view web part (DVWP), you could display a “Delete” link with the necessary parameter information for each document and bind the deleteFile() function to the click event of the link. I generally don’t like to use the onclick attribute, but this would do the trick in the row view XSL template (usually dvt_1.rowview) of a DVWP:

<a href="/{@FileDirRef}/DispForm.aspx?ID={@ID}" onclick="deleteFile('{@ID}', '{@FileRef}', 'Documents');return false;">Delete</a>

Hopefully this helps you avoid some debugging frustration.

4 thoughts on “Deleting Documents with SPServices

    1. I think of it as the JavaScript equivalent to inline CSS. Better to use jQuery’s onClick() or similar in an external JavaScript file in my opinion. But I still use it for some applications because it’s quick, easy, and it works.

  1. Was stuck on this for a long time
    Thanks to you got it working with a few seconds
    Great article!!!

Comments are closed.