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("https://pikasha12.sharepoint.com/sites/DLH"))
        {
            string pwd = "xyz";
            SecureString passWord = new SecureString();

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

            clientcontext.Credentials = new SharePointOnlineCredentials("rswain44@swain.onmicrosoft.com", passWord);
            ContentTypeCollection displayContentTypes = clientcontext.Web.ContentTypes;
            clientcontext.Load(displayContentTypes);
            clientcontext.ExecuteQuery();
            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;
            GridView1.DataBind();
        }
    }
}

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

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <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" />
            </asp:GridView>
        </div>
    </form>
</body>
</html>

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
{
Console.WriteLine(ContentTypes.Name);
ContentTypes.DeleteObject();
clientcontext.ExecuteQuery();
}

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.

Donwload Hub site pdf

Download SharePoint Online Tutorial PDF FREE!

Get update on Webinars, video tutorials, training courses etc.

Rajkiran Swain

Rajkiran is currently working as a SharePoint Consultant in QATAR . Rajkiran having 7+ years of experience in Microsoft Technologies such as SharePoint 2019/2016/2013/2010, MOSS 2007,WSS 3.0, Migration, Asp.Net, C#.Net, Sql Server, Ajax, jQuery etc.He is C#Corner MVP (2 Times).

>