Retrieve list content types using csom in SharePoint online or SharePoint 2013

In this post, we will discuss how we can retrieve content types associated with SharePoint online list using csom in SharePoint online (Download PDF for FREE). We will use here .Net managed object model code where we will use below dlls.

Microsoft.SharePoint.Client.dll;
Microsoft.SharePoint.Client.RunTime.dll;

SharePoint deveopment training course

First, we can retrieve the list using GetByTitle method and then we can retrieve all the content types by using the below code:

ContentTypeCollection contentTypeColl = lst.ContentTypes;

Below is the full code:



public static void RetrieveListContentType()
{
string contentTypes = string.Empty;
using (ClientContext context = new ClientContext(“https://onlysharepoint2013.sharepoint.com/sites/Bhawana/”))
{
context.AuthenticationMode = ClientAuthenticationMode.Default;
context.Credentials = new SharePointOnlineCredentials(GetSPOAccountName(), GetSPOSecureStringPassword());
List lst = context.Web.Lists.GetByTitle(“MyTestList”);
ContentTypeCollection contentTypeColl = lst.ContentTypes;
context.Load(contentTypeColl);
context.ExecuteQuery();
foreach (ContentType ct in contentTypeColl)
{
contentTypes += ct.Name;
}
}
}

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:

The above code will retrieve all content types associated with MyTestList list. 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 →
  • Hi Bhawana Rathore
    how r u. I am Deepak Rathore From Delhi.
    I should have some help for sharepoint.
    please helpme.
    like as
    ssom,csom,jsom ,Rest api,add-ins(Sharepoint hosted app & Provider hosted app) and work flow sp designer,Nintex and K2.
    please provide me these notes or Video.

    thanks and regards
    Deepak Rathore