How to delete a page or File in Sharepoint 2013/Office 365 using CSOM

In this article, we will learn about delete a page or File  in Sharepoint 2013/Office 365 using CSOM. Below is the sample CSOM code to achieve the same.

ClientContext clientContext= new ClientContext(siteUrl);
clientContext.AuthenticationMode = ClientAuthenticationMode.Default;
clientContext.Credentials = new SharePointOnlineCredentials(userName, pwd);
Web web = clientContext.Web;

string serverRelatedURL = web.ServerRelativeUrl;
File file = web.GetFileByServerRelativeUrl(serverRelatedURL + filename);
clientContext.Load(file);
file.DeleteObject();
clientContext.ExecuteQuery();

 

Hope it helps!! Happy Sharepointing 🙂

Leave a Reply

Your email address will not be published.