private void EnsureListContentTypes(ref SPList list, ref SPContentType cType) { #region Ensure List has Content Type SPContentType listContenType = null; try { listContenType = list.ContentTypes[list.GetDirectChildContentType(cType.Id)]; } catch { /* expected: not yet contained */ } if (listContenType == null) { list.ContentTypesEnabled = true; listContenType = list.ContentTypes.Add(cType); list.Update(); } #endregion #region Remove unwanted Content Types if (RemoveDefaultContentTypesOnListCreation) { List <SPContentTypeId> deletionIds = new List <SPContentTypeId>(); bool someLeft = false; string prefix = string.Concat(ParentSchema.GroupName, ParentSchema.ContentTypeNameSchemaSeparator); foreach (SPContentType iType in list.ContentTypes) { if (!iType.Name.StartsWith(prefix) && iType.Name != listContenType.Name) { deletionIds.Add(iType.Id); } else { someLeft = true; } } if (someLeft) { foreach (SPContentTypeId iId in deletionIds) { list.ContentTypes.Delete(iId); } list.Update(); } } #endregion }