public static void AddContentType(this SPList list, SPContentType contentType, bool throwOnNotAllowed = false) { // Make sure the list accepts content types. list.ContentTypesEnabled = true; try { // Add the content type to the list. if (!list.IsContentTypeAllowed(contentType)) { throw new SPException(string.Format("The \"{0}\" content type is not allowed on the \"{1}\" list.", contentType.Name, list.Title)); } if (list.ContainsContentTypeWithId(contentType.Id)) { throw new SPException(string.Format("The content type \"{0}\" is already in use on the \"{1}\" list", contentType.Name, list.Title)); } } catch (SPException) { if (throwOnNotAllowed) { throw; } return; } list.ContentTypes.Add(contentType); }
private void ContentTypesAction(SPList list, IEnumerable <SPContentType> contentTypes) { foreach (SPContentType contentType in contentTypes) { if (!list.ContainsContentTypeWithId(contentType.Id) && list.IsContentTypeAllowed(contentType)) { list.AddContentType(contentType); } } }
private SPContentType InnerEnsureContentType(SPContentTypeCollection contentTypeCollection, ContentTypeInfo contentTypeInfo) { if (contentTypeCollection == null) { throw new ArgumentNullException("contentTypeCollection"); } SPContentTypeId contentTypeId = contentTypeInfo.ContentTypeId; SPList list = null; var contentTypeResourceTitle = this.resourceLocator.GetResourceString(contentTypeInfo.ResourceFileName, contentTypeInfo.DisplayNameResourceKey); if (TryGetListFromContentTypeCollection(contentTypeCollection, out list)) { // Make sure its not already in the list. var contentTypeInList = list.ContentTypes.Cast <SPContentType>().FirstOrDefault(ct => ct.Id == contentTypeId || ct.Parent.Id == contentTypeId); if (contentTypeInList == null) { // Can we add the content type to the list? if (list.IsContentTypeAllowed(contentTypeId)) { // Enable content types if not yet done. if (!list.ContentTypesEnabled) { list.ContentTypesEnabled = true; list.Update(true); } // Try to use the list's web's content type if it already exists var contentTypeInWeb = list.ParentWeb.Site.RootWeb.AvailableContentTypes[contentTypeId]; if (contentTypeInWeb == null) { // By convention, content types should always exist on root web as site-collection-wide // content types before they get linked on a specific list. var rootWebContentTypeCollection = list.ParentWeb.Site.RootWeb.ContentTypes; contentTypeInWeb = this.EnsureContentType(rootWebContentTypeCollection, contentTypeInfo); this.log.Warn( "EnsureContentType - Forced the creation of Content Type (name={0} ctid={1}) on the root web (url=) instead of adding the CT directly on the list (id={2} title={3}). By convention, all CTs should be provisonned on RootWeb before being re-used in lists.", contentTypeInWeb.Name, contentTypeInWeb.Id.ToString(), list.ID, list.Title); } // Add the web content type to the collection. return(list.ContentTypes.Add(contentTypeInWeb)); } } else { this.InnerEnsureFieldInContentType(contentTypeInList, contentTypeInfo.Fields); return(contentTypeInList); } } else { SPWeb web = null; if (TryGetWebFromContentTypeCollection(contentTypeCollection, out web)) { // Make sure its not already in ther web. var contentTypeInWeb = web.ContentTypes[contentTypeId]; if (contentTypeInWeb == null) { SPContentTypeCollection rootWebContentTypeCollection = null; if (web.ID == web.Site.RootWeb.ID) { rootWebContentTypeCollection = contentTypeCollection; } else { rootWebContentTypeCollection = web.Site.RootWeb.ContentTypes; this.log.Warn( "EnsureContentType - Will force creation of content type (id={0} name={1}) on root web instead of on specified sub-web. This is to enforce the following convention: all CTs should be provisioned at root of site collection, to ease maintenance. Ensure your content types on the root web's SPContentTypeCollection to avoid this warning.", contentTypeId.ToString(), contentTypeInfo.DisplayNameResourceKey); } var contentTypeInRootWeb = rootWebContentTypeCollection[contentTypeId]; if (contentTypeInRootWeb == null) { // Add the content type to the Root Web collection. By convention, we avoid provisioning // CTs directly on sub-webs to make CT management easier (i.e. all of your site collection's // content types should be configured at the root of the site collection). var newWebContentType = new SPContentType(contentTypeId, rootWebContentTypeCollection, contentTypeResourceTitle); contentTypeInRootWeb = rootWebContentTypeCollection.Add(newWebContentType); } this.InnerEnsureFieldInContentType(contentTypeInRootWeb, contentTypeInfo.Fields); return(contentTypeInRootWeb); } else { this.InnerEnsureFieldInContentType(contentTypeInWeb, contentTypeInfo.Fields); return(contentTypeInWeb); } } // Case if there is no Content Types in the Web (e.g single SPWeb) var returnedContentType = this.EnsureContentType(contentTypeCollection, contentTypeInfo); return(returnedContentType); } return(null); }
private static void CheckWeb2(string webUrl) { const string METHOD_NAME = "Program.cs - CheckWeb2()"; Console.WriteLine("{0} - starting...", METHOD_NAME); using (SPSite site = new SPSite(webUrl)) { using (SPWeb web = site.OpenWeb()) { // Check all lists in the web for (int i = 0; i < web.Lists.Count; i++) { SPList list = web.Lists[i]; Console.WriteLine("{0} - Processing List '{1}' with Url '{2}'...", METHOD_NAME, list.Title, list.RootFolder.ServerRelativeUrl); // Filter for document libraries if (list.BaseType == SPBaseType.DocumentLibrary && list.BaseTemplate == SPListTemplateType.DocumentLibrary && !SPTools.GetBlackList(web.Site.Url).Any(list.Title.Contains)) { Console.WriteLine("List '{0}' is Document Library and List is not on Blacklist.", list.Title); foreach (string CTName in Names.CONTENTTYPES_TO_ADD) { SPContentType ct = web.Site.RootWeb.AvailableContentTypes[CTName]; if (ct != null) { // Make sure the list accepts content types list.ContentTypesEnabled = true; Console.WriteLine("{0} - Try to add content type '{1}' to list '{2}'...", METHOD_NAME, ct.Name, list.Title); // Add the content type to the list. if (!list.IsContentTypeAllowed(ct)) { // Content type is not allowed on the list Console.WriteLine("{0} - Content type '{1}' is not allowed on the list '{2}'.", METHOD_NAME, ct.Name, list.Title); } else if (list.ContentTypes[ct.Name] != null) { // The content type is already in use on the list Console.WriteLine("{0} - Content type '{1}' is already assigned to list '{2}'.", METHOD_NAME, ct.Name, list.Title); } else { if (!isDebugMode) { // add the content type try { list.ContentTypes.Add(ct); Console.WriteLine("{0} - Added content type '{1}' to list '{2}' sucessfully.", METHOD_NAME, ct.Name, list.Title); } catch (Exception ex) { Console.WriteLine("{0} - Adding content type '{1}' to list '{2}' failed!\r\n ERROR: '{3}'", METHOD_NAME, ct.Name, list.Title, ex.Message); } } else { Console.WriteLine("{0} - WE ARE IN DEBUG_MODE. No changes are made to list '{1}'.", METHOD_NAME, list.Title); } } } else { Console.WriteLine("{0} - Content type '{1}' is not available on site '{2}'.", METHOD_NAME, CTName, web.Site.RootWeb.Url); } } if (!isDebugMode) { // Change the order of the content types for the list SPTools.ChangeCTOrder(list); } } else if (SPTools.GetBlackList(web.Site.Url).Any(list.Title.Contains)) { Console.WriteLine("{0} - List '{1}' is on blacklist.", METHOD_NAME, list.Title); } } } } Console.WriteLine("{0} - finished.", METHOD_NAME); }