This tutorial we will see various SharePoint content type CSOM examples. We will see below examples:
- How to get content type name by id programmatically using csom in SharePoint
- How to get list content types using the SharePoint client object model code (C#.Net)
- How to get SharePoint content type id by name programmatically using CSOM
- How to delete content type from SharePoint list programmatically using CSOM
- Retrieve list item content type name or id using CSOM in SharePoint Online
Get content type name by id programmatically using csom in SharePoint
This SharePoint tutorial explains how to get content type name by id programmatically using csom (client-side object model) in SharePoint Online/2013/2016.
A content type SharePoint 2013 is a reusable collection of metadata (columns), workflow, behavior, and other settings for a category of items or documents in a SharePoint 2013 list or document library. We can retrieve content type name by content type id using csom SharePoint online.
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
Below is the code to get content type name by id csom in SharePoint Online.
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.
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;
}
}
You can see the result like below:

I hope this will be helpful to Get content type name by id programmatically using csom in SharePoint Online/2016/2013.
Retrieve List Content types using CSOM SharePoint Online/2016/2013
Now, we will see how to get list content types using the C#.Net client object model (CSOM) in SharePoint Online/2016/2013.
We can retrieve content types associated with SharePoint online list using client object model code (csom) in SharePoint Online. We will use here .Net managed object model code where we will use below dlls.
- Microsoft.SharePoint.Client.dll
- Microsoft.SharePoint.Client.RunTime.dll
Here we are connecting to SharePoint online site from a console application in visual studio 2015/2017/2019.
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 to retrieve list content types programmatically using CSOM in SharePoint Online/2016/2013.
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;
}
}
Delete content type from SharePoint list programmatically using CSOM
Now, we will see how to delete a content type attached to a list using csom in SharePoint online/2013/2016.
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.
By using csom code I am trying to retrieve the content type by its id and then I am deleting the object by calling DeleteObject() method.
Below is the full code to delete the content type from the SharePoint Online list using csom programmatically.
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;
}
}
This is how we can delete a content type from the SharePoint list programmatically using client object model (CSOM) in SharePoint Online/2016/2013.
Get SharePoint content type id by name programmatically using CSOM
This SharePoint tutorial explains how to get content type id by name programmatically using csom in SharePoint Online.
As per one of the requirements we need to retrieve content type id by content type name using the client object model (csom) in SharePoint online. In this SharePoint tutorial, we will discuss how to get content type id by content type name using csom client object model code in SharePoint online or SharePoint 2013.
Here we have used C#.net managed object model code, by using Microsoft.SharePoint.Client.dll and Microsoft.SharePoint.Client.Runtime.dll.
The below method will take two parameters as URL and contentTypeName. Here URL is the site URL and contentTypeName is the content type name for which we are trying to retrieve the content type id.
This we are doing in a console application and we are connecting to a SharePoint online site.
Below is the full code to get content type id by name programmatically using csom in SharePoint Online/2013/2016.
public static string GetContentTypeIdByName(string URL, string contentTypeName)
{
string contentTypeId = string.Empty;
using (ClientContext clientContext = new ClientContext(URL))
{
clientContext.AuthenticationMode = ClientAuthenticationMode.Default;
clientContext.Credentials = new SharePointOnlineCredentials(GetSPOAccountName(), GetSPOSecureStringPassword());
Web rootWeb = clientContext.Site.RootWeb;
var itemContentType = clientContext.LoadQuery(rootWeb.ContentTypes.Where(ct => ct.Name == contentTypeName));
clientContext.ExecuteQuery();
var sessionContentType = itemContentType.FirstOrDefault();
contentTypeId = sessionContentType.Id.ToString();
}
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;
}
}
This way we will be able to get content type id by content type name using csom in SharePoint Online or SharePoint 2013/2016.
Retrieve list item content type name or id using CSOM in SharePoint Online
Content types in SharePoint define a field collection that is reusable across multiple lists or multiple document libraries.
A content type represents a group of informational items in your organization that share common properties. Properties may be like name, description, a grouping category etc. We can create a content type and we can add into various SharePoint lists of a document library.
Now, we will see how to get content type name or id using csom (.Net client object model) in SharePoint Online.
Once a content type is associated to a SharePoint list, we can retrieve list item’s associated content type name or id using .Net managed object model code in SharePoint online.
We have a list that has a content type associated with it. And we have created few items using those content types in the list. We got one requirement to check the content type of a particular list item. Suppose I have one item as “Item-1”, we need to retrieve the content type name associated with the list item.
The below method will take the ItemID as an input parameter and return the content type name which we have written inside a console application using visual studio 2015.
First, we are retrieving the list item by Id and then we are getting the content type associated with the item from item ContentType;
Below is the code to retrieve list item content type name or id using CSOM in SharePoint Online.
public static string GetContentTypeNameForItem(int ItemID)
{
string contentTypeName = string.Empty;
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");
ListItem item = lst.GetItemById(ItemID);
contextURL.Load(item);
contextURL.Load(lst);
contextURL.ExecuteQuery();
ContentType ct = item.ContentType;
contextURL.Load(ct, n => n.Name);
contextURL.ExecuteQuery();
if (ct != null)
{
contentTypeName = ct.Name;
}
}
return contentTypeName;
}
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;
}
}
This is how we can retrieve list item content type name or id using CSOM in .Net client object model in SharePoint Online.
You may like the following SharePoint tutorials:
In this SharePoint tutorial, we learned:
- How to get content type name by id programmatically using csom in SharePoint
- How to get list content types using the SharePoint client object model code (C#.Net)
- How to delete the content type from SharePoint list programmatically using CSOM
- How to get SharePoint content type id by name programmatically using CSOM
- How to retrieve list item content type name or id using CSOM in SharePoint Online
Hello Everyone!! I am Bhawana a SharePoint MVP and having about 10+ 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”
Hello Bhawana
How to set the wildcard filter value using c# client object model?