In this SharePoint csom tutorial, we will discuss, how to work with content types using csom (.Net client object model) code in SharePoint Online as well as SharePoint 2013/2016.
Here we will see how to retrieve content type Id from content type name using csom in SharePoint? How to delete a content type from SharePoint Online list using csom .net managed object model code? Also, we will check, how to retrieve list content types using csom in SharePoint Online or SharePoint 2013/2016? How to retrieve content type by Id using csom (client side object model) in SharePoint Online?
Also, we will see a few more SharePoint csom examples on how to retrieve content type id by content type name using the client object model (csom) code SharePoint Online? How to retrieve Content type id by content type name using csom .Net managed object model in SharePoint online? And how to retrieve list item content type name or id using .Net client object model in SharePoint Online?
You can use a windows application or console application to test the csom code. Here I have used a Windows application and a SharePoint Online site to do these CSOM examples.
You can check out below tutorials on how to start with csom application.
- How to create a console application to work with SharePoint Online Office 365 sites?
- SharePoint Online console application with csom
To work with SharePoint CSOM (.Net client object model (C#.Net)), you need to add the below dlls:
- Microsoft.SharePoint.Client.Dll
- Microsoft.SharePoint.Client.Runtime.Dll
These two dlls we can add from NuGet, which I have already explained in the above examples.
SharePoint Tutorial Contents
- Retrieve content type Id from content type name using CSOM
- Retrieve Content type Name by Id using csom in SharePoint Online
- Retrieve list content types using csom in SharePoint Online/2013/2016
- Retrieve content type id by content type name using SharePoint client object model code
- Delete content type from SharePoint Online list using CSOM
- Retrieve list item content type name or id using csom in SharePoint
Retrieve content type Id from content type name using CSOM
First, we will see, how we to retrieve content type name from content type id using.Net managed object model code (csom) in SharePoint Online.
Here in the below csom code, we are passing the content type name as “Document Set” and it will return the content type id of “Document Set” from the SharePoint Online site.
I have added a button in my windows application and call the below method.
private static string GetContentTypeIDFromName()
{
string contentTypeId = string.Empty;
using (ClientContext clientContext = new ClientContext("https://<tenantname>.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 "MyPassword")
{
secureString.AppendChar(c);
}
return secureString;
}
catch
{
throw;
}
}
private static string GetSPOAccountName()
{
try
{
return "[email protected]<tenantname>@onmicrosoft.com";
}
catch
{
throw;
}
}
If you run the code, you can see it will return the content type id like below:
This way we can retrieve content type id from content type name csom in SharePoint Online.
Now, we will discuss how to retrieve content type name by content type id using csom (.Net client object model c#) code in SharePoint Online or SharePoint 2013/2016.
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 csom code to retrieve the content type name from content type id in SharePoint using csom.
public static void GetContentTypeByID()
{
string contentTypeName = string.Empty;
using (ClientContext context = new ClientContext("https://<tenant name>.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 SecureString GetSPOSecureStringPassword()
{
try
{
var secureString = new SecureString();
foreach (char c in "MyPassword")
{
secureString.AppendChar(c);
}
return secureString;
}
catch
{
throw;
}
}
private static string GetSPOAccountName()
{
try
{
return "[email protected]<tenantname>@onmicrosoft.com";
}
catch
{
throw;
}
}
Once you run the above code, you can see the result like below:
Now, we will see how to retrieve content types associated with a list using csom in SharePoint Online or SharePoint 2013/2016.
First, we can retrieve the SharePoint 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 csom code to retrieve all list content types in SharePoint.
public static void RetrieveListContentType()
{
string contentTypes = string.Empty;
using (ClientContext context = new ClientContext("https://<tenantname>.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 SecureString GetSPOSecureStringPassword()
{
try
{
var secureString = new SecureString();
foreach (char c in "MyPassword")
{
secureString.AppendChar(c);
}
return secureString;
}
catch
{
throw;
}
}
private static string GetSPOAccountName()
{
try
{
return "[email protected]<tenantname>@onmicrosoft.com";
}
catch
{
throw;
}
}
Once you run the above code, it will retrieve all content types associated with MyTestList list in SharePoint Online/2013/2016.
Now, we will see how to retrieve content type id by content type name using the client object model in SharePoint Online.
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.
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 "MyPassword")
{
secureString.AppendChar(c);
}
return secureString;
}
catch
{
throw;
}
}
private static string GetSPOAccountName()
{
try
{
return "[email protected]<tenantname>@onmicrosoft.com";
}
catch
{
throw;
}
}
This CSOM example, we will discuss how to delete a content type attached to a list using csom in SharePoint Online.
Here I have a list name as “MyTestList” which is a SharePoint custom list and in that list, I have added the default Announcement content type.
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 to delete a content type from the SharePoint list using csom.
public static void DeleteContentType()
{
using (ClientContext contextURL = new ClientContext("https://<tenantname>.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 SecureString GetSPOSecureStringPassword()
{
try
{
var secureString = new SecureString();
foreach (char c in "MyPassword")
{
secureString.AppendChar(c);
}
return secureString;
}
catch
{
throw;
}
}
private static string GetSPOAccountName()
{
try
{
return "[email protected]<tenantname>@onmicrosoft.com";
}
catch
{
throw;
}
}
The above code will delete the content type from the SharePoint Online list.
Now we will discuss how to retrieve list item’s associated content type name or id using csom (.Net managed object model) code in SharePoint online.
Here we have a list which has few content type’s associated with it and we have created few items using those content types. Now as per the requirement we have to check which is the content type associated with particular items.
Below method will take the ItemID as an input parameter and return the content type name.
First, we are retrieving the list item by Id and then we are getting the content type associated with the item from item.ContentType;
public static string GetContentTypeNameForItem(int ItemID)
{
string contentTypeName = string.Empty;
using (ClientContext contextURL = new ClientContext("https://<tenantname>.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 "MyPassword")
{
secureString.AppendChar(c);
}
return secureString;
}
catch
{
throw;
}
}
private static string GetSPOAccountName()
{
try
{
return "[email protected]<tenantname>@onmicrosoft.com";
}
catch
{
throw;
}
}
You may like following SharePoint content type tutorials:
- This field must be indexed to enforce unique values error while adding content types in SharePoint Online
- How to publish InfoPath forms as a content type in sharepoint 2013/2016/Online
- Delete content type from SharePoint list using PowerShell
- Hide content type field in edit form in SharePoint Online/2013/2016
- How to get access token in SharePoint Online using CSOM and use in Postman or Google Rest client
The above code will retrieve list item content type name or id using csom in SharePoint Online or SharePoint 2013/2016.
Bhawana Rathore is a Microsoft MVP (3 times in Office Apps & Services) and a passionate SharePoint Consultant, having around 10 years of IT experience in the industry, as well as in .Net technologies. She likes to share her technical expertise in EnjoySharePoint.com and SPGuides.com
[…] This SharePoint csom tutorial, we will discuss, how to copy list items from one list to another list in SharePoint programmatically using CSOM. […]
[…] Content type Examples using CSOM in SharePoint Online/2013/2016 […]