0

I am trying to delete a list that contains documents using the SharePoint 2010 CSOM and am using the following code:

//find the 'Project Documents' library and delete it
var list = context.Web.Lists.GetByTitle("Project Documents");
list.DeleteObject();

context.ExecuteQuery();

But I always get the exception "This list cannot be deleted". The list is empty, but still I am not able to delete it. What am I doing wrong?

1

I think we have to load the web object first, before you retrieve any lists from it.

Follow the example from MSDN (paraphrased) :

function deleteList(siteUrl) {
    var clientContext = new SP.ClientContext(siteUrl);
    var oWebsite = clientContext.get_web();
    this.listTitle = 'Project Documents';

    this.oList = oWebsite.get_lists().getByTitle(listTitle);
    oList.deleteObject();

    clientContext.executeQueryAsync(
        Function.createDelegate(this, this.onQuerySucceeded), 
        Function.createDelegate(this, this.onQueryFailed)
    );
}

function onQuerySucceeded() {
    var result = listTitle + ' deleted.';
    alert(result);
}

function onQueryFailed(sender, args) {
    alert('Request failed. ' + args.get_message() + 
        '\n' + args.get_stackTrace());
}
|improve this answer|||||
  • 1
    Isn't that the same as what I'm doing above in C#? I have the context loaded, and I can access the Web using context.Web and then just grab the list using .GetByTitle("Project Documents"). I'm not sure how the .js above is loading anything any different. I don't have the ability to call .GetLists, but instead just grab the list by title. – awh112 Jan 16 '15 at 20:16
  • So, I have found that if I go into PowerShell and do $list.AllowDeletion = $True, I can do it. Apparently when I created the lists programmatically I didn't set that field and it doesn't allow me to delete unless I change that. – awh112 Jan 16 '15 at 20:20
  • List deletions are set to true for SharePoint generated lists, such as for publishing, workflow, BI features, etc. Thanks for sharing! – Mike Jan 16 '15 at 20:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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