Saturday 9 February 2013

Getting List Level ContentTypes from SharePoint 2010 using Client Object Model (C#)



Step 1: Create New Project. (Minimum .NET Framework 3.5)
Step 2: Add “Microsoft.SharePoint.Client” and “Microsoft.SharePoint.Client.Runtime” namespaces.
Step 3: Write the following code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//SharePoint namespace
using Microsoft.SharePoint.Client;

namespace ListLevelContentTypes
{
    class Program
    {
        static void Main(string[] args)
        {
            string siteUrl = "http://sharepointserver.com";
            ClientContext clientContext = new ClientContext(siteUrl);
            clientContext.Credentials = new System.Net.NetworkCredential("admin", "password", "sharepointserver.com");
            Web site = clientContext.Web;
            clientContext.Load(site);
            clientContext.ExecuteQuery();

            //Getting List ContentTypes
            List list = site.Lists.GetByTitle("Students");
            clientContext.Load(list);
            clientContext.ExecuteQuery();
            ContentTypeCollection contentTypeCollection = list.ContentTypes;
            clientContext.Load(contentTypeCollection);
            clientContext.ExecuteQuery();

            //Printing ContentTypes
            foreach (ContentType contentType in contentTypeCollection)
            {
                Console.WriteLine(contentType.Name);
            }
            Console.ReadLine();
        }
    }
}


1 comment: