C# CSOM Get List of ContentTypes in a Library

369 Views Asked by At

I can easily get a list of Libraries I can easily get a list of all ContentTypes

But having trouble getting a list if libraries and what contenttypes belong to that library.

Can anyone assist wtih this request? We utilize a library and have a small list of content types in a library.

Thanks in advance.

This gets me the list of Libraries.

List<string> sOut = new List<string>();
var context = new ClientContext(url);

context.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
var web = context.Web;

ListCollection collList = web.Lists;

context.Load(collList);
context.ExecuteQuery();

foreach (List oList in collList)
{
    Console.WriteLine("Title: {0} Created: {1}", oList.Title, 
    oList.Created.ToString());
}
1

There are 1 best solutions below

0
Jerry On

For getting all the content types list, you could refer this code demo:

      ListCollection collList = context.Web.Lists;

      context.Load(collList);
      context.ExecuteQuery();

      foreach (List oList in collList)
      {
        ContentTypeCollection contentTypeColl = oList.ContentTypes;
        context.Load(contentTypeColl);
        context.ExecuteQuery();
        foreach (ContentType ct in contentTypeColl)
        {
          Console.WriteLine(oList.Title + ": "+ ct.Name);
        }
      }
      Console.ReadLine();