Retrieve content type Id from content type name using .Net managed object model code in SharePoint online

sharepoint Retrieve content type name from content type id csom

Here we will discuss how we can retrieve content type name from content type id using.Net managed object model code (csom) in SharePoint online. Here we will try to retrieve inside a console application. To work with.Net managed object model code, we need to have below dlls which we can add from Nuget package manager.

Here in the below code we are passing the content type name as “Document Set” and it will return the content type id of “Document Set”. Below is the full code:

SharePoint deveopment training course

You can also read:

– How to retrieve list name from guid using .Net managed object model code in SharePoint online?

– Step by step create a web api and deploy in Microsoft Azure for SharePoint online using visual studio 2015

private static string GetContentTypeIDFromName()

{

string contentTypeId = string.Empty;

using (ClientContext clientContext = new ClientContext(“https://onlysharepoint2013.sharepoint.com/sites/Bhawana/”))

{

clientContext.AuthenticationMode = ClientAuthenticationMode.Default;

clientContext.Credentials = new SharePointOnlineCredentials(GetSPOAccountName(), GetSPOSecureStringPassword());

Web web = clientContext.Web;

ContentTypeCollection contentTypeColl = web.ContentTypes;

clientContext.Load(contentTypeColl);

clientContext.ExecuteQuery();

foreach (ContentType ct in contentTypeColl)

{

if (ct.Name.Equals(“Document Set”))

{

contentTypeId = ct.Id.ToString();

break;

}

}

}

return contentTypeId;

}

private static SecureString GetSPOSecureStringPassword()

{

try

{

var secureString = new SecureString();

foreach (char c in ConfigurationManager.AppSettings[“SPOPassword”])

{

secureString.AppendChar(c);

}

return secureString;

}

catch

{

throw;

}

}

private static string GetSPOAccountName()

{

try

{

return ConfigurationManager.AppSettings[“SPOAccount”];

}

catch

{

throw;

}

}


If you run the code, you can see it will return the content type id like below:

sharepoint Retrieve content type name from content type id csom
sharepoint Retrieve content type name from content type id csom

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 →