How to get all content types using csom in SharePoint

This SharePoint tutorial explains, how to get all content types using csom in SharePoint.

Before we start, you should have your own office 365 account where you have created your site collection. In the below code I have called SharePointOnlineCredentials class to authenticate the site.

You have to add the Microsoft.SharePoint.client reference in your project to get the client context. For getting this dll, you have to download it from NuGet packages. Please look int the below screenshot.

get all content types sharepoint

SharePoint csom get all content types

Below is the source code which can call to SharePoint online site to get the all content type.

using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Security;
using Microsoft.SharePoint.Client;
using System.Data;

public partial class getDataFromSharePointOnline : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)
        using (ClientContext clientcontext = new ClientContext(""))
            string pwd = "xyz";
            SecureString passWord = new SecureString();

            foreach (char c in pwd.ToCharArray()) passWord.AppendChar(c);

            clientcontext.Credentials = new SharePointOnlineCredentials("[email protected]", passWord);
            ContentTypeCollection displayContentTypes = clientcontext.Web.ContentTypes;
            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn("Name", typeof(string)));
            dt.Columns.Add(new DataColumn("Description", typeof(string)));

            foreach (ContentType ContentTypes in displayContentTypes)
                dt.Rows.Add(ContentTypes.Name, ContentTypes.Description);
            GridView1.DataSource = dt;

On this page, I have added a GridView to show the all content type Name with Description.

<html xmlns="">
<head runat="server">
    <form id="form1" runat="server">
            <asp:GridView ID="GridView1" runat="server" BackColor="White" BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" CellPadding="4">
                <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
                <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
                <PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
                <RowStyle BackColor="White" ForeColor="#330099" />
                <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
                <SortedAscendingCellStyle BackColor="#FEFCEB" />
                <SortedAscendingHeaderStyle BackColor="#AF0101" />
                <SortedDescendingCellStyle BackColor="#F6F0C0" />
                <SortedDescendingHeaderStyle BackColor="#7E0000" />

Here is the O/P of this code .

sharepoint csom get all content types

If you want to delete content types, then you have to use ContentTypes.DeleteObject() .

foreach (ContentType ContentTypes in displayContentTypes

Issue 1: The “SharePointOnlineCredentials” is marked as type or namespace not found.

using (var context = new ClientContext(webUrl))
context.Credentials = new SharePointOnlineCredentials(userName, password);

Solution: According to this article, that should be a member of the Microsoft.SharePoint.Client namespace.

In Visual Studio Nuget Packages, search “Microsoft.SharePointOnline.CSOM” and install the latest CSOM API for SharePoint Online.

You may like following SharePoint content type tutorials:

This SharePoint tutorial, we learned how to get all content types using csom in SharePoint 2013/2016 or SharePoint Online.