Delete the document using SPServices


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.
 $().SPServices({
  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 the function:
deleteFile( '23', 'http://server/documentLibrary/mydocument.docx', 'Documents' );
For More Info: http://joshmccarty.com/2012/02/deleting-documents-with-spservices/ 

Comments

Popular posts from this blog

Bulk update and delete using SPservice in SharePoint

Javascript function to populate from query string