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); } } }