示例#1
0
        /// <summary>
        /// Create Content Type
        /// </summary>
        public static void CreateContentType(ClientContext clientContext, string ContentTypeName, string ContentTypeDescription, string ContentTypeId, string[] filedNames)
        {
            var contentType = CSOMUtil.GetContentTypeById(clientContext, ContentTypeId);

            // check if the content type exists
            if (contentType == null)
            {
                ContentTypeCollection contentTypeColl = clientContext.Web.ContentTypes;
                clientContext.Load(contentTypeColl);
                clientContext.ExecuteQuery();

                // Specifies properties that are used as parameters to initialize a new content type.
                ContentTypeCreationInformation contentTypeCreation = new ContentTypeCreationInformation();
                contentTypeCreation.Name        = ContentTypeName;
                contentTypeCreation.Description = ContentTypeDescription;
                contentTypeCreation.Group       = "Property Manager My App Content Types";
                contentTypeCreation.Id          = ContentTypeId;

                //// Add the new content type to the collection
                contentType = contentTypeColl.Add(contentTypeCreation);
                clientContext.Load(contentType);
                clientContext.ExecuteQuery();

                CSOMUtil.BindFieldsToContentType(clientContext, contentType, filedNames);
            }
        }
示例#2
0
        /// <summary>
        /// Create Content Type
        /// </summary>
        public static void RemoveContentType(ClientContext clientContext, string ContentTypeId)
        {
            var contentType = CSOMUtil.GetContentTypeById(clientContext, ContentTypeId);

            if (contentType != null)
            {
                contentType.DeleteObject();
                clientContext.ExecuteQuery();
            }
        }
示例#3
0
        /// <summary>
        /// Add a list/document library to o365 web
        /// </summary>
        public static void AddSharePointList(ClientContext clientContext, string contentTypeID, string listTitle, string url, int listTemplateType, bool isHideTitle = false)
        {
            Web web = clientContext.Web;

            var contentType = CSOMUtil.GetContentTypeById(clientContext, contentTypeID);
            var list        = CSOMUtil.GetListByTitle(clientContext, listTitle);

            // check if the content type exists
            if (list == null && contentType != null)
            {
                ListCreationInformation creationInfo = new ListCreationInformation();
                creationInfo.Title        = listTitle;
                creationInfo.Url          = url;
                creationInfo.TemplateType = listTemplateType;
                list = web.Lists.Add(creationInfo);
                list.Update();
                clientContext.ExecuteQuery();

                // insert content type
                var cts = list.ContentTypes;
                list.ContentTypesEnabled = true;
                cts.AddExistingContentType(contentType);
                clientContext.Load(cts);
                clientContext.ExecuteQuery();


                // remove default content type
                var count = cts.Count;
                while (--count >= 0)
                {
                    if (cts[0].Name != "Folder" && cts[0].Name != contentType.Name)
                    {
                        cts[0].DeleteObject();
                    }
                }
                clientContext.ExecuteQuery();


                // add fields to default view
                View defaultView = list.DefaultView;
                clientContext.Load(defaultView, v => v.ViewFields);
                clientContext.Load(contentType, c => c.Fields);
                clientContext.ExecuteQuery();
                if (!defaultView.ViewFields.Contains("ID"))
                {
                    defaultView.ViewFields.Add("ID");
                    defaultView.ViewFields.MoveFieldTo("ID", 0);
                }
                //hide title field in view
                if (isHideTitle == true)
                {
                    defaultView.ViewFields.Remove("LinkTitle");
                }
                foreach (Field field in contentType.Fields)
                {
                    if (!defaultView.ViewFields.Contains(field.Title) && field.Title != "Content Type" && field.Title != "Title")
                    {
                        defaultView.ViewFields.Add(field.Title);
                    }
                }
                defaultView.Update();
                clientContext.ExecuteQuery();

                //hide title field in forms
                if (isHideTitle == true)
                {
                    var field_title = list.Fields.GetByTitle("Title");
                    clientContext.Load(field_title);
                    clientContext.ExecuteQuery();
                    field_title.Hidden = true;
                    field_title.Update();
                    clientContext.ExecuteQuery();
                }
            }
        }