Enmarcha.SharePoint.Entities.Artefacts.ListSharePoint.AddContentType C# (CSharp) Method

AddContentType() public method

Add Content Type
public AddContentType ( string contentTypeName ) : bool
contentTypeName string
return bool
        public bool AddContentType(string contentTypeName)
        {
            try
            {
                 var contentType = Web.Site.RootWeb.ContentTypes[contentTypeName] ?? Web.ContentTypes[contentTypeName];

                var listAdd = Web.Lists.TryGetList(Name);
                if (listAdd != null)
                {
                    listAdd.ContentTypesEnabled = true;
                    listAdd.Update();
                    listAdd.ContentTypes.Add(contentType);
                    listAdd.Update();
                    try
                    {
                        var orderedContentTypes = new SPContentType[1];
                        orderedContentTypes[0] = contentType;
                        listAdd.RootFolder.UniqueContentTypeOrder = orderedContentTypes;
                        listAdd.RootFolder.Update();
                    }
                    catch (Exception exception)
                    {
                     Logger.Error(string.Concat("Add ContentType", exception.Message));
                    }
                }
                else
                {
                    return false;
                }
                
            }
            catch (Exception exception)
            {
                Logger.Error(string.Concat("Add ContentType", exception.Message));                
                return false;
            }
            return true;
        }

Usage Example

 public void CreateListAddContentType()
 {
     if (!ContextSharePoint.VerifyServer(Site)) Assert.Inconclusive();
     var contentType= new ContentType(Site.RootWeb,this.Logger,"TESTHELLO","TEST","Elemento");
     contentType.Create(string.Empty);
     ListSharePoint = new ListSharePoint(Site.RootWeb, Logger, "LISTATEST");
     Assert.IsTrue(ListSharePoint.Create("descripcion", ListTemplateType.Contacts, true));
   Assert.IsTrue(ListSharePoint.AddContentType("TESTHELLO"));
   Assert.IsTrue(ListSharePoint.DeleteContentType("TESTHELLO"));
     Assert.IsFalse(ListSharePoint.AddContentType("TESTBYE"));
     Assert.IsTrue(ListSharePoint.Delete());
 }
All Usage Examples Of Enmarcha.SharePoint.Entities.Artefacts.ListSharePoint::AddContentType