Add Content Type to List or Document Library Programmatically SharePoint 2013

Add Content Type to List or Document Library Programmatically SharePoint 2013

Add Content Type to List or Document Library Programmatically SharePoint 2013. In this post, I described you to Add Content Type to List or Document Library Programmatically SharePoint. You should be very familiar with the content type. For more detail about content type follow my previous post. Also, you can create content type manually. Create content type in SharePoint 2013. if you need to add the content type to SPlist or library programmatically, Below is code.

[AdSense-A]

 
public static void ADDContentypeTodoclibray(string Siteurl, string ContentTypeName, string Listname)
        {

            try
            {
                using (SPSite siteCollection = new SPSite(Siteurl))
                {
                    using (SPWeb site = siteCollection.OpenWeb())
                    {

                        SPContentType ct = site.AvailableContentTypes[ContentTypeName];

                        if (ct != null)
                        {
                            SPList list = site.Lists[Listname]; 

                            list.ContentTypesEnabled = true;
                            if (!list.IsContentTypeAllowed(ct))
                            {
                            }
                            else if (list.ContentTypes[ct.Name] != null)
                            {
                            }
                            else
                                list.ContentTypes.Add(ct);
                           
						   
                            list.EnableVersioning = true;
                            list.EnableMinorVersions = true;

                            list.Update();

                        }
                        else 
                        {
                            ReadWriteException.LogFileWrite("No content type is found.");
                        }

                    }
                }
            }
            catch (Exception ex)
            {
                ReadWriteException.LogFileWrite(ReadWriteException.CreateErrorMessage(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 !!