Delete content type from SharePoint online list using csom .net managed object model code

In this post we will discuss how we can delete a content type attached to a list using csom in SharePoint online (Download PDF for FREE). Here we will use C#.Net managed object model code where we will use below dlls:
– Microsoft.SharePoint.Client.Dll
– Microsoft.SharePoint.Client.Runtime.Dll

Here I have a list name as “MyTestList” which is a custom list and in that list, I have added the default Announcement content type. You can check out: Video Tutorial: Add content type to SharePoint Online List using Browser.

SharePoint deveopment training course

By using csom code I am trying to retrieve the content type by its id and then I am deleting the object.



Below is the full code:

public static void DeleteContentType()
{
using (ClientContext contextURL = new ClientContext(“https://onlysharepoint2013.sharepoint.com/sites/Bhawana/”))
{
contextURL.AuthenticationMode = ClientAuthenticationMode.Default;
contextURL.Credentials = new SharePointOnlineCredentials(GetSPOAccountName(), GetSPOSecureStringPassword());
List lst = contextURL.Web.Lists.GetByTitle(“MyTestList”);
ContentType ct = lst.ContentTypes.GetById(“0x010400BE88F9D0169DC14194092368A99093F1”);
ct.DeleteObject();
contextURL.ExecuteQuery();
}
}

private static string GetSPOAccountName()
{
try
{
return ConfigurationManager.AppSettings[“SPOAccount”];
}
catch
{
throw;
}
}

private static SecureString GetSPOSecureStringPassword()
{
try
{
var secureString = new SecureString();
foreach (char c in ConfigurationManager.AppSettings[“SPOPassword”])
{
secureString.AppendChar(c);
}
return secureString;
}
catch
{
throw;
}
}


Read some sharepoint online tutorials:

Hope this will be helpful.

Check out Best Alternative to InfoPath -> Try Now

free sharepoint training

SharePoint Online FREE Training

JOIN a FREE SharePoint Video Course (3 Part Video Series)

envelope
envelope

About Bhawana Rathore

Hello Everyone!! I am Bhawana a SharePoint MVP and having about 8+ years of SharePoint experience as well as in .Net technologies . I have worked in all the versions of SharePoint from wss to Office 365. I have good exposure in Customization and Migration using Nintex, Metalogix tools . Now exploring more in SharePoint 2016 :) Hope here I can contribute and share my knowledge to the fullest. As I believe "There is no wealth like knowledge, and no poverty like ignorance"

View all posts by Bhawana Rathore →