0

I have a Document Library, when I select some files from it the ribbon appears and I can choose "Delete Document".

I need to make a button on the page (.aspx view of the Library) that calls a javascript, which does exactly what does ribbon option does - deleting selected items.

I already used Javascript successfully to delete items in a Sharepoint list. But this time I need to delete files in the Document Library, is it possible with JS?

Thank you,

5
  • If you already have the javascript to delete list items I'm sure you're most of the way (if not all the way) there for deleting documents. Commented Jun 5, 2013 at 13:43
  • ok thank you, just wanted to know if the Document Library files are considered by Sharepoint as simply List Items. Commented Jun 5, 2013 at 13:47
  • Well, not exactly, but it should be close enough for your purposes. Commented Jun 5, 2013 at 16:51
  • can you please let me know how do you delete files from SP with Javascript? thanks in advance! Commented Dec 18, 2013 at 21:01
  • It was a project I worked on quite some time ago and don't have access to the code anymore; but basically as I remember, the files are actually list items, so what you need is to delete them with SP.js functions. You import SP.js into your sharepoint page and then get the list with js, then delete the list items with deleteObject. see here: msdn.microsoft.com/en-us/library/office/… Commented Dec 21, 2013 at 2:23

1 Answer 1

1

Since one of the comments asked for a code how to delete files:

var items;
 var ctx;
 var count;
 var web;
 var totalCount = 0;

function Delete() {
 ctx = SP.ClientContext.get_current();
 web = ctx.get_web();
 var currentlibGuid = SP.ListOperation.Selection.getSelectedList();
 if (currentlibGuid == null) {

alert('Please select an item in the list!');
 return;
 }
 var currentLib = web.get_lists().getById(currentlibGuid);
 items = SP.ListOperation.Selection.getSelectedItems(ctx);

//Get Selected Items count
 count = CountDictionary(items);
for (var i in items) {
 var currentItem = currentLib.getItemById(items[i].id);
 ctx.load(currentItem);

currentItem.deleteObject();

ctx.executeQueryAsync(
 Function.createDelegate(this, this.Succeeded),
 Function.createDelegate(this, this.onQueryFailed)
 );

}
}

function onQueryFailed(sender, args) {

totalCount = totalCount + 1;

if (totalCount == count) {
 location.reload();
 }
 alert('Request failed. ' + args.get_message() +
 '\n' + args.get_stackTrace());
 }

function Succeeded() {

totalCount = totalCount + 1;

if (totalCount == count) {
 location.reload();
 }
 }

Link to source

1
  • exactly, with the mention that you need to import SP.js first. Commented Dec 21, 2013 at 2:26

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.