Content type Examples using CSOM in SharePoint Online/2013/2016

SharePoint csom content type examples
SharePoint deveopment training course

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.

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.

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 "bijay@<tenantname>@onmicrosoft.com";
            }
            catch
            {
                throw;
            }
        }

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

Retrieve content type Id from content type name using CSOM

This way we can retrieve content type id from content type name csom in SharePoint Online.

Retrieve Content type Name by Id using 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 "bijay@<tenantname>@onmicrosoft.com";
            }
            catch
            {
                throw;
            }
        }

Once you run the above code, you can see the result like below:

Retrieve Content type Name by Id using csom in SharePoint

Retrieve list content types using csom in SharePoint Online/2013/2016

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 "bijay@<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.

Retrieve content type id by content type name using SharePoint client object model code

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 "bijay@<tenantname>@onmicrosoft.com";
            }
            catch
            {
                throw;
            }
        }

Delete content type from SharePoint Online list using CSOM

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 "bijay@<tenantname>@onmicrosoft.com";
            }
            catch
            {
                throw;
            }
        }

The above code will delete the content type from the SharePoint Online list.

Retrieve list item content type name or id using csom in SharePoint

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 "bijay@<tenantname>@onmicrosoft.com";
            }
            catch
            {
                throw;
            }
        }

You may like following SharePoint content type tutorials:

The above code will retrieve list item content type name or id using csom in SharePoint Online or SharePoint 2013/2016.

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 →