Create Content Type using CSOM

Create Content Type using CSOM Programmatically in SharePoint 2013

Create Content Type using CSOM in SharePoint 2013 or SharePoint Online. I describe you to Create multiple content type on Single site using csom. We need to Create Content Type using CSOM Programmatically because any time we have to create ate content type in bulk. Content type also can create manually as i discussed in my previous post.Create Content Type in SharePoint .Below is code to create content type using csom.[AdSense-A]

public static void CreateContentType(string siteurls, string ContentTypeName)
        {
            try
            {
                
                ClientContext clientContext = new ClientContext(siteurls);
                Web rootWeb = clientContext.Site.RootWeb;
                ContentTypeCollection contentTypes = rootWeb.ContentTypes;
                clientContext.Load(contentTypes);
                clientContext.ExecuteQuery();
                if (ContentTypeName == "House Rent".Trim())
                {

                }
                var isExist1 = 0;
                isExist1 = isExist_Helper(clientContext, ContentTypeName, "contenttype");
                if (isExist1 == 0)
                {

                    Console.WriteLine("Content type: " + ContentTypeName + " is being created...");
                    ReadWriteException.LogFileWrite("Content type: " + ContentTypeName + " is being created...");
                    using (SPSite spsite = new SPSite(siteurls))
                    {
                        using (SPWeb spweb = spsite.OpenWeb())
                        {
                            SPContentType parentCType = spweb.AvailableContentTypes[SPBuiltInContentTypeId.Document];
                            SPContentType childCType = new SPContentType(parentCType, spweb.ContentTypes, ContentTypeName);
                            childCType = spweb.ContentTypes.Add(childCType);
                        }

                    }
                    Console.WriteLine("Content type: " + ContentTypeName + " is successfully created.");
                    
                    
                }
            }
            catch (Exception ex)
            {
                
            }

 

 


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

error: Content is protected !!