Ejemplo n.º 1
0
        /// <summary>
        /// Add content type to list
        /// </summary>
        /// <param name="list"></param>
        /// <param name="contentType"></param>
        /// <param name="defaultContent"></param>
        public static void AddContentTypeToList(this List list, ContentType contentType, bool defaultContent = false)
        {
            list.ContentTypesEnabled = true;
            list.Update();
            list.Context.ExecuteQuery();
            ContentTypeCollection contentTypes = list.ContentTypes;

            list.Context.Load(contentTypes);
            list.Context.ExecuteQuery();


            foreach (ContentType ct in contentTypes)
            {
                if (ct.Name.ToLowerInvariant() == contentType.Name.ToString().ToLowerInvariant())
                {
                    // Already there, abort
                    return;
                }
            }

            contentTypes.AddExistingContentType(contentType);
            list.Context.ExecuteQuery();
            //set the default contenttype
            if (defaultContent)
            {
                SetDefaultContentTypeToList(list, contentType);
            }
        }