Monday 6 May 2013

Creating a ContentType with new Site Field in 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;

//SharePoint namespace
using Microsoft.SharePoint.Client;

namespace CreatingContentTypes
{
    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 content type name
            Console.Write("ContentType Name: ");
            string contentTypeName = Console.ReadLine();
        }
        static void CreateContentType(Web site, ClientContext clientContext, string contentTypeName)
        {
            ContentTypeCollection contentTypeCollection = site.AvailableContentTypes;
            clientContext.Load(contentTypeCollection);
            clientContext.ExecuteQuery();
            int count = 0;

            //Checking whether the ContentType already exists
            foreach (ContentType item in contentTypeCollection)
            {
                clientContext.Load(item);
                clientContext.ExecuteQuery();
                if (item.Name.ToUpper() == contentTypeName.ToUpper())
                    count++;
            }

            //If doesnot exist, create it
            if (count == 0)
            {
                Console.Write("SiteField Name: ");
                string siteFieldName = Console.ReadLine();
                string siteField =
                    "<Field Type = 'Text' " +
                    "Hidden = 'False' " +
                    "DisplayName = '" + siteFieldName + "' " +
                    "ResultType = 'Text' " +
                    "ReadOnly = 'False' " +
                    "Name = '" + siteFieldName + "' " +
                    "Group = 'Custom Columns'></Field>";
                Field field = site.Fields.AddFieldAsXml(siteField, false, AddFieldOptions.AddFieldToDefaultView);
                clientContext.ExecuteQuery();
                FieldLinkCreationInformation fieldLink = new FieldLinkCreationInformation();
                fieldLink.Field = field;

                //Creating ContentType
                ContentTypeCreationInformation contentType = new ContentTypeCreationInformation();
                contentType.Name = contentTypeName;
                contentType.Group = "SampleGroup";
                contentType.ParentContentType = contentTypeCollection[0].Parent;
                ContentType myContentType = site.ContentTypes.Add(contentType);

                //Adding fields to ContentType
                myContentType.FieldLinks.Add(fieldLink);
                field.Update();
                myContentType.Update(true);
                clientContext.ExecuteQuery();
                site.Update();
                Console.WriteLine("ContentType Created Successfully");
                Console.ReadLine();
            }
            else
            {
                Console.WriteLine("ContentType already presents in the site.");
                Console.Write("Enter another ContentType name: ");
                string content = Console.ReadLine();
                CreateContentType(site, clientContext, content);
            }
        }
    }
}