1

We are developing an API to be called by external application.

To have unqiue document ID across Site Collection we have enabled "Document ID service"

Using CSOM we have been able to implement

  1. Upload File
  2. Download file by passing 'Document ID'

However struggling to file a way to delete document using 'Document ID'. That is the caller will only be passing the unique Document ID and we have to delete it. Googling around getting SOM code Delete document from document Library Using Document ID

As we are implementing it for SharePoint 2016 and donot want to use anything which will run only the Server, we are looking for CSOM / REST implementation.

Kindly let know if any further details required.

1 Answer 1

1

Document ID value is present in a document library column whose internal name is _dlc_DocId.

So, you can use CAML query to find out which document has a particular document ID and based on that you can delete that document.

Here, I am assuming a document library named Documents and document ID something like SOMEUNIQUEID-11-11

The JSOM implementation would be as below:

function deleteFileByDocumentId(documentID){

    var ctx = new SP.ClientContext.get_current();

    var camlQuery = new SP.CamlQuery();
    camlQuery.set_viewXml('<View><Query><Where><Eq>' + 
        '<FieldRef Name=\'_dlc_DocId\'/><Value Type=\'Text\'>UNIQUEID-11-11</Value>' + 
        '</Eq></Where></Query></View>');

    var items = ctx.get_web().get_lists().getByTitle('Documents').getItems(camlQuery);
    ctx.load(items);
    ctx.executeQueryAsync(function () {

        var itemCount = items.get_count();
        for (var i = itemCount - 1; i >= 0; i--) {
            var oListItem = items.itemAt(i);
            oListItem.deleteObject();
        };

        clientContext.executeQueryAsync(function(){
            console.log("success deleting document");
        }, function(){
            console.log("something went wrong");
        });

    },function(){
        console.log("something went wrong");
    });
}

RESTful implementation would be as below (modified from here):

function getItemsToDelete() {

    var requestUrl = _spPageContextInfo.webAbsoluteUrl + "/_api/web/Lists/GetByTitle('Documents')/items?$filter=OData__dlc_DocId eq 'SOMEUNIQUEID-11-11'&$select=Title,FileRef";

    return $.ajax({
        url: requestUrl,
        type: "GET",
        headers: {
            "accept": "application/json;odata=verbose",
        },
        success: function(result) {
            $.each(result.d.results, function(index, item){                
                itemsToDelete.push(deleteItem(item));
            });            
        },
        error: function(error) {
            console.log(error);
        }
    });    
}

function deleteItem(item) {

    var requestUrl = item.__metadata.uri;

    return $.ajax({
        url: requestUrl,
        type: "POST",
        headers: {
            "accept": "application/json;odata=verbose",
            "X-RequestDigest": $("#__REQUESTDIGEST").val(),
            "IF-MATCH": "*",
            "X-HTTP-Method": "DELETE"
        },
        success: function() {
            console.log("File with ID " + item.__metadata.id + " successfully deleted!");
        },
        error: function(error) {
             console.log(error);
        }
    });    
}

var itemsToDelete = [];


$.when(getItemsToDelete()).then(function () {
    $.when.apply($, itemsToDelete).then(function(){
        console.log("All docs with unique document ID are deleted!");
    });
});
4
  • Hi Gautam, Thank you for the response. However, we will be getting the Document ID. We won't be getting the Library in which the document is. Thus wont be able to use GetbyTitle() Kindly advise
    – shoab
    Commented Mar 20, 2018 at 9:23
  • does this mean you won't know where the document is and will only have the ID? Commented Mar 20, 2018 at 10:28
  • Hi Lem, yes i know only the "Document Id" (unique Document ID accross entire site-collection). Configured Document ID service
    – shoab
    Commented Mar 20, 2018 at 11:26
  • Then use search to retrieve the item, that will return you the full URL to the item. From there you can grab the web and the DocLib and the nuke it. Edit: There can be more than 1 result! Make sure you cover that ;) Commented Mar 20, 2018 at 13:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.