Retrieve Content type by Id using csom in SharePoint Online

Retrieve Content type by Id using csom in SharePoint Online

Here we will discuss how we can retrieve content type name by content type id using csom SharePoint online (Download PDF for FREE). We will use here C#.Net client object model code using the below dlls inside a console application.

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

SharePoint deveopment training course

Here in this example I have used the “Workflow Task (SharePoint 2013)” whose Id is “0x0108003365C4474CAE8C42BCE396314E88E51F”.

Once I will run the below code, it will give me the content type name



Below is the full code:

public static void GetContentTypeByID()
{
string contentTypeName = string.Empty;
using (ClientContext context = new ClientContext(“https://onlysharepoint2013.sharepoint.com/sites/Bhawana/”))
{
context.AuthenticationMode = ClientAuthenticationMode.Default;
context.Credentials = new SharePointOnlineCredentials(GetSPOAccountName(), GetSPOSecureStringPassword());
ContentType ct = context.Web.ContentTypes.GetById(“0x0108003365C4474CAE8C42BCE396314E88E51F”);
context.Load(ct);
context.ExecuteQuery();
contentTypeName = 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:


You can see the result like below:

Retrieve Content type by Id using csom in SharePoint Online
Retrieve Content type by Id using csom in SharePoint Online

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 →