Monday, April 17, 2017

Delete List/Library on SharePoint Online when there is no remove or delete option

There are two possible reasons as to why you cannot see the remove or delete option on a list or a library.

  1. You don't have enough permissions.
  2. The library or list was created with "Allow Deleting" property set to false.
In this post I am going to talk more about Point #2 and discuss how can we set it to true using CSOM for SharePoint Online.
I am creating a console application using visual studio and using the latest version of SharePoint online CSOM.  The older version of this nugget package does not contain enabling "Allow Deletion: . So make sure you get the latest version.

Add the nugget package in below picture.



string siteCollectionUrl = "https://company.sharepoint.com/";
            string userName = "user@microsoft.com";
            string password = "xxxxxxxx";

            
            ClientContext ctx = new ClientContext(siteCollectionUrl);

            
            SecureString secureString = new SecureString();
            password.ToList().ForEach(secureString.AppendChar);

            
            ctx.Credentials = new SharePointOnlineCredentials(userName, secureString);

           
            Web web = ctx.Web;

            ctx.Load(web);
            ctx.ExecuteQuery();

            Console.WriteLine(web.Url.ToString());  

            Console.WriteLine("Current Site: " + web.Title);

            List library = web.Lists.GetByTitle("Documents");
            ctx.Load(library, l=>l.AllowDeletion);
             ctx.ExecuteQuery();

            Console.WriteLine("Allow deletion for library is : " + library.AllowDeletion);



Since "Allow deletion" property is set to false. We have to set it to true to get back the delete list/library link.





if (!library.AllowDeletion)
            {
                library.AllowDeletion = true;
                library.Update();
                ctx.ExecuteQuery();

                ctx.Load(library, l => l.AllowDeletion);
                ctx.ExecuteQuery();
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine("Allow deletion for library after update is : " + library.AllowDeletion);
            }



Now if we go back to the library and refresh the settings page of list/library. We now see that the delete link is back.


Share This!

0 comments:

Post a Comment