Esempio n. 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);
            }
        }
Esempio n. 2
0
        private static void CreateContentTypeIfDoesNotExist(ClientContext cc, Web web)
        {
            ContentTypeCollection contentTypes = web.ContentTypes;
            cc.Load(contentTypes);
            cc.ExecuteQuery();

            foreach (var item in contentTypes)
            {
                if (item.StringId == "0x0101009189AB5D3D2647B580F011DA2F356FB3")
                    return;
            }

            // Create a Content Type Information object
            ContentTypeCreationInformation newCt = new ContentTypeCreationInformation();
            // Set the name for the content type
            newCt.Name = "Contoso Sample Document";
            //Inherit from oob document - 0x0101 and assign 
            newCt.Id = "0x0101009189AB5D3D2647B580F011DA2F356FB3";
            // Set content type to be avaialble from specific group
            newCt.Group = "Contoso Content Types";
            // Create the content type
            ContentType myContentType = contentTypes.Add(newCt);
            cc.ExecuteQuery();

            Console.WriteLine("Content type created.");
        }
Esempio n. 3
0
        static void CreatingContentTypes(XmlNode contenttypes, ClientContext clientcontext)
        {
            var contentTypes = clientcontext.Web.ContentTypes;

            foreach (XmlNode node in contenttypes.ChildNodes)
            {
                if (!CheckContentType(clientcontext, node.Attributes["Name"].Value))
                {
                    ContentTypeCreationInformation CT = new ContentTypeCreationInformation()
                    {
                        Group = node.Attributes["Group"].Value,
                        Name  = node.Attributes["Name"].Value
                    };
                    var contentType = contentTypes.Add(CT);
                    clientcontext.Load(contentType);
                    clientcontext.ExecuteQuery();
                    Console.WriteLine("New Content Type" + node.Attributes["Name"].Value + " has been created");
                    Console.WriteLine("Adding Site Columns to " + node.Attributes["Name"].Value + "Content Type");
                    foreach (XmlNode childnode in node.ChildNodes)
                    {
                        AddSiteColumnsToContentType(clientcontext, childnode.Attributes["Name"].Value, node.Attributes["Name"].Value);
                        Console.WriteLine(childnode.Attributes["Name"].Value + " is added to " + node.Attributes["Name"].Value + " Content Type");
                    }
                }
                else
                {
                    Console.WriteLine("Content Type " + node.Attributes["Name"].Value + " is already exists now we are site columns......");
                    foreach (XmlNode childnode in node.ChildNodes)
                    {
                        AddSiteColumnsToContentType(clientcontext, childnode.Attributes["Name"].Value, node.Attributes["Name"].Value);
                        Console.WriteLine(childnode.Attributes["Name"].Value + " is added to " + node.Attributes["Name"].Value + " Content Type");
                    }
                }
            }
        }
Esempio n. 4
0
        private void CreateContentType(ClientContext context)
        {
            //Reference -
            //http://www.srinisistla.com/blog/Lists/Posts/Post.aspx?ID=100
            //https://msdn.microsoft.com/library/office/microsoft.sharepoint.client.contenttypecollection.add.aspx
            try
            {
                //ContentTypeCollection cTCollection = context.Site.RootWeb.AvailableContentTypes;  // AvailableContentTypes is read only. It returns all the CT from web as well as root site
                ContentTypeCollection cTCollection = context.Site.RootWeb.ContentTypes;  // ContentTypes returns all the CT from current web site only
                //context.Load(cTCollection);
                //context.ExecuteQuery();
                //foreach (ContentType ct in cTCollection)
                //{
                //    Console.WriteLine(ct.Name + "   :   " + ct.Parent);
                //}

                ContentTypeCreationInformation cti = new ContentTypeCreationInformation();
                cti.Description = "this is a test Content Type";
                cti.Name        = "TestContentTypeRohit";
                cti.Group       = "RohitCTGroup";
                ContentType ct = cTCollection.Add(cti);
                context.Load(cTCollection);
                context.ExecuteQuery();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception in CreateContent Type :  " + ex.Message.ToString());
            }
        }
Esempio n. 5
0
        private static void CreateContentTypeIfDoesNotExist(ClientContext cc, Web web)
        {
            ContentTypeCollection contentTypes = web.ContentTypes;

            cc.Load(contentTypes);
            cc.ExecuteQuery();

            foreach (var item in contentTypes)
            {
                if (item.StringId == "0x0101009189AB5D3D2647B580F011DA2F356FB3")
                {
                    return;
                }
            }

            // Create a Content Type Information object
            ContentTypeCreationInformation newCt = new ContentTypeCreationInformation();

            // Set the name for the content type
            newCt.Name = "Contoso Sample Document";
            //Inherit from oob document - 0x0101 and assign
            newCt.Id = "0x0101009189AB5D3D2647B580F011DA2F356FB3";
            // Set content type to be avaialble from specific group
            newCt.Group = "Contoso Content Types";
            // Create the content type
            ContentType myContentType = contentTypes.Add(newCt);

            cc.ExecuteQuery();

            Console.WriteLine("Content type created.");
        }
        /// <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);
            }
        }
        public virtual ContentType Create()
        {
            ContentTypeCollection contentTypeColl = _context.Web.ContentTypes;

            _context.Load(contentTypeColl);
            _context.ExecuteQuery();

            var targetContentType = GetContentType(contentTypeColl);

            if (targetContentType != null)
            {
                throw new Exception("Content type already Exists");
            }
            else
            {
                var parentContentType = GetContentType(contentTypeColl, ParentTypeTitle);
                // Create content Type
                ContentTypeCreationInformation contentTypeCreationInformation = new ContentTypeCreationInformation
                {
                    Name              = Name,
                    Description       = Description,
                    Group             = Group,
                    ParentContentType = parentContentType
                };

                targetContentType = contentTypeColl.Add(contentTypeCreationInformation);

                _context.Load(targetContentType);
                _context.ExecuteQuery();
                return(targetContentType);
            }
        }
Esempio n. 8
0
        private static void AddContentTypeToList()
        {
            string contentTypename;
            Console.Write("Enter content type - \n");
            contentTypename = Console.ReadLine();
            Console.WriteLine("You entered", contentTypename);

            string contentdescription;
            Console.Write("Enter a description for content type - \n");
            contentdescription = Console.ReadLine();
            Console.WriteLine("You entered ", contentdescription);

            string groupName;
            Console.Write("Enter group name - \n");
            groupName = Console.ReadLine();
            Console.WriteLine("You entered ", groupName);

            ContentTypeCollection contentTypeColl = globalctx.Web.ContentTypes;

            ContentTypeCreationInformation contentTypeCreation = new ContentTypeCreationInformation();
            contentTypeCreation.Name = contentTypename;
            contentTypeCreation.Description = contentdescription;
            contentTypeCreation.Group = groupName;

            ContentType ct = contentTypeColl.Add(contentTypeCreation);
            globalctx.Load(ct);
            globalctx.ExecuteQuery();

            Console.WriteLine(ct.Name + " content type is created successfully");

        }
Esempio n. 9
0
        public static void CreateContentType(Web web)
        {
            ClientContext context = (ClientContext)web.Context;

            context.Load(web, w => w.ContentTypes);
            ContentTypeCollection contentTypes = web.ContentTypes;
            var itemContentType = contentTypes.GetById("0x01");

            ContentTypeCreationInformation lessonsLearnedInfo = new ContentTypeCreationInformation();

            lessonsLearnedInfo.ParentContentType = itemContentType;
            lessonsLearnedInfo.Name  = "Lessons Learned";
            lessonsLearnedInfo.Group = "Webcor Custom";


            ContentType lessonsLearnedContentType = contentTypes.Add(lessonsLearnedInfo);

            //add Projects Column (using Term Set)
            //add Keywords Column (using Term Set)
            //add Lessons Learned Field
            //add Date-Time Field
            //add Innovator Field
            //add Issue Field
            //add Resolution Field
            //add Status Field
        }
Esempio n. 10
0
        public static void MyFirstContentType(ClientContext ctx)
        {
            Web root = ctx.Site.RootWeb;

            string outofboxAnnouncementCtId = "0x0104";
            string seperator = "00";
            string newGuid   = "251AC5249AA044CB84583885D2750C72"; // example {DCDE4E19-7860-4BDA-968D-0D20C78FB160} with chars removed = DCDE4E1978604BDA968D0D20C78FB160

            string newCtid = outofboxAnnouncementCtId + seperator + newGuid;


            if (!root.ContentTypeExistsById(newCtid))
            {
                ContentTypeCreationInformation ctInfo = new ContentTypeCreationInformation();
                ctInfo.Name        = "myFirstContentType";
                ctInfo.Group       = "Custom Content Type";
                ctInfo.Description = "some stuff you want to say";
                ctInfo.Id          = newCtid;
                root.ContentTypes.Add(ctInfo);
                ctx.ExecuteQuery();
            }


            ContentType myNewContentType = root.GetContentTypeById(newCtid);

            myNewContentType.AddFieldById(new Guid("{388F3ECA-394F-4257-802C-92A4427D0EF0}"), true, false);
            myNewContentType.AddFieldById(new Guid("{D443E51B-1609-4CE6-96BD-9FCEA3FDE329}"), false, false);


            //root.CreateContentType("AnotherCT", "0x010067DAF351B3314A4DB1391A903CBC82F8", "Custom Content Type");

            string pathToXML = AppDomain.CurrentDomain.BaseDirectory + "fields.xml";

            root.CreateContentTypeFromXMLFile(pathToXML);
        }
Esempio n. 11
0
        /// <summary>
        /// Creates the type of the content.
        /// </summary>
        /// <param name="clientContext">The client context.</param>
        /// <param name="web">Object of site</param>
        /// <param name="siteColumns">The site columns.</param>
        /// <param name="contentTypeName">Name of Content Type</param>
        internal static bool CreateContentType(ClientContext clientContext, Microsoft.SharePoint.Client.Web web, List <string> siteColumns, string contentTypeName, string contentTypegroup)
        {
            bool        status            = true;
            ContentType parentContentType = null;

            try
            {
                ContentTypeCollection contentTypeCollection = web.ContentTypes;
                clientContext.Load(contentTypeCollection);
                clientContext.Load(web.Fields);
                clientContext.ExecuteQuery();
                parentContentType = (from ct in contentTypeCollection
                                     where ct.Name == ServiceConstantStrings.OneDriveParentContentType
                                     select ct).FirstOrDefault();
                ContentTypeCreationInformation contentType = new ContentTypeCreationInformation();
                contentType.Name  = contentTypeName;
                contentType.Group = contentTypegroup;
                ///// contentType.Id = "0x010100B1ED198475FB3A4AABC59AAAD89B7EAD";
                if (parentContentType != null)
                {
                    contentType.ParentContentType = parentContentType;
                }
                ContentType finalObj = web.ContentTypes.Add(contentType);
                AddColumnsToContentType(web, siteColumns, finalObj);
                web.Update();
                clientContext.ExecuteQuery();
            }
            catch (Exception exception)
            {
                status = false;
                Logger.LogError(exception, MethodBase.GetCurrentMethod().DeclaringType.Name, MethodBase.GetCurrentMethod().Name, ServiceConstantStrings.LogTableName);
            }
            return(status);
        }
Esempio n. 12
0
        public ContentType Create()
        {
            ContentTypeCollection cntTypeList = context.Web.ContentTypes;

            context.Load(cntTypeList);
            context.ExecuteQuery();

            var targetContentType = GetContentType(cntTypeList);

            // not exist content type yet
            if (targetContentType == null)
            {
                var parentContentType = GetContentType(cntTypeList, ParentType);
                // Create content Type
                ContentTypeCreationInformation newCntType = new ContentTypeCreationInformation
                {
                    Name              = Name,
                    Description       = Description,
                    Group             = Group,
                    ParentContentType = parentContentType
                };

                targetContentType = cntTypeList.Add(newCntType);

                LoadSiteColumn(targetContentType);
                context.Load(targetContentType);
                context.ExecuteQuery();
            }
            return(targetContentType);
        }
Esempio n. 13
0
        private void ContentTypeCode(ClientContext context)
        {
            Guid guid = Guid.NewGuid();

            try
            {
                Web rootWeb = context.Site.RootWeb;
                var field1  = rootWeb.Fields.AddFieldAsXml("<Field DisplayName='TestSiteColumn' Name='SessionName' ID='" + guid + "' Type='Text' />", false, AddFieldOptions.AddFieldInternalNameHint);
                //context.ExecuteQuery();

                ContentTypeCollection ctCollection = context.Web.ContentTypes;
                context.Load(ctCollection);
                context.ExecuteQuery();

                // create by reference
                //ContentType itemContentTypes = context.LoadQuery(rootWeb.ContentTypes.Where(ct => ct.Name == "Item"));

                ContentType itemContentTypes = ctCollection.GetById("0x0101");
                context.ExecuteQuery();
                ContentTypeCreationInformation cti = new ContentTypeCreationInformation();
                cti.Name              = "CT1";
                cti.Description       = "test content type CSOM";
                cti.ParentContentType = itemContentTypes;
                cti.Group             = "RohitCustomCT";
                ContentType myContentType = ctCollection.Add(cti);
                context.ExecuteQuery();
                //myContentType.Fields.Add(field1);
                myContentType.FieldLinks.Add(new FieldLinkCreationInformation
                {
                    Field = field1
                });
                myContentType.Update(true);
                context.ExecuteQuery();


                ListCreationInformation lct = new ListCreationInformation();
                lct.Title        = "LogList";
                lct.Description  = "this is test list";
                lct.TemplateType = (int)ListTemplateType.GenericList;
                List logList = context.Web.Lists.Add(lct);
                context.Load(logList);
                context.ExecuteQuery();

                if (!logList.ContentTypesEnabled)
                {
                    logList.ContentTypesEnabled = true;
                    logList.Update();
                    context.ExecuteQuery();
                }
                logList.ContentTypes.AddExistingContentType(myContentType);
                context.ExecuteQuery();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception in CreateSiteColumn :  " + ex.Message.ToString());
            }
        }
Esempio n. 14
0
        public static ContentType CreateContentType(this Web site, ContentTypeCreationInformation contentTypeCreateInfo)
        {
            ContentTypeCollection contentTypes = site.ContentTypes;
            ContentType           contentType  = contentTypes.Add(contentTypeCreateInfo);

            site.Context.Load(contentType);
            site.Context.ExecuteQuery();

            return(contentType);
        }
        public ContentTypeCreationInformation ToCreationObject()
        {
            var info = new ContentTypeCreationInformation()
            {
                Id          = this.ContentTypeId,
                Name        = this.Name,
                Description = this.Description,
                Group       = this.ContentTypeGroup
            };

            return(info);
        }
Esempio n. 16
0
        void createContentType()
        {
            ContentTypeCollection contentTypes = web.ContentTypes;

            context.Load(contentTypes);
            context.ExecuteQuery();

            ContentTypeCreationInformation contentTypeCreationInformation = new ContentTypeCreationInformation();

            contentTypeCreationInformation.Name        = "Employee";
            contentTypeCreationInformation.Description = "Employee";
            contentTypeCreationInformation.Group       = "CSOM Assignment";

            ContentType newContentType = contentTypes.Add(contentTypeCreationInformation);

            context.Load(newContentType);
            context.ExecuteQuery();

            Field fieldFirstName           = web.Fields.GetByInternalNameOrTitle("First Name");
            Field fieldLastName            = web.Fields.GetByInternalNameOrTitle("Last name");
            Field fieldEmail               = web.Fields.GetByInternalNameOrTitle("E-Mail");
            Field fieldShortDescription    = web.Fields.GetByInternalNameOrTitle("Short description");
            Field fieldProgrammingLanguage = web.Fields.GetByInternalNameOrTitle("Programming language");

            ContentType Employee = (from c in contentTypes
                                    where c.Name == "Employee"
                                    select c).FirstOrDefault();

            Employee.FieldLinks.Add(new FieldLinkCreationInformation
            {
                Field = fieldFirstName
            });
            Employee.FieldLinks.Add(new FieldLinkCreationInformation
            {
                Field = fieldLastName
            });
            Employee.FieldLinks.Add(new FieldLinkCreationInformation
            {
                Field = fieldEmail
            });
            Employee.FieldLinks.Add(new FieldLinkCreationInformation
            {
                Field = fieldShortDescription
            });
            Employee.FieldLinks.Add(new FieldLinkCreationInformation
            {
                Field = fieldProgrammingLanguage
            });
            Employee.Update(true);
            context.ExecuteQuery();

            Console.WriteLine("Success create content type");
        }
Esempio n. 17
0
        public static ContentType CreateContentType(this Web site, ContentType parentContentType, string name, string groupName, string desc)
        {
            ContentTypeCreationInformation contentTypeCreateInfo = new ContentTypeCreationInformation
            {
                Name = name,
                ParentContentType = parentContentType,
                Group             = groupName,
                Description       = desc
            };

            return(CreateContentType(site, contentTypeCreateInfo));
        }
Esempio n. 18
0
        static ContentType CreateContentType(string contentTypeName, string baseContentType)
        {
            DeleteContentType(contentTypeName);

              ContentTypeCreationInformation contentTypeCreateInfo = new ContentTypeCreationInformation();
              contentTypeCreateInfo.Name = contentTypeName;
              contentTypeCreateInfo.ParentContentType = site.ContentTypes.GetById(baseContentType); ;
              contentTypeCreateInfo.Group = "Wingtip";
              ContentType ctype = site.ContentTypes.Add(contentTypeCreateInfo);
              clientContext.ExecuteQuery();
              return ctype;
        }
        void createContentType()
        {
            ContentTypeCollection contentTypes = web.ContentTypes;

            context.Load(contentTypes);
            context.ExecuteQuery();

            ContentTypeCreationInformation contentTypeCreationInformation = new ContentTypeCreationInformation();

            contentTypeCreationInformation.Name        = "Project";
            contentTypeCreationInformation.Description = "Project";
            contentTypeCreationInformation.Group       = "CSOM Assignment";

            ContentType newContentType = contentTypes.Add(contentTypeCreationInformation);

            context.Load(newContentType);
            context.ExecuteQuery();

            Field fieldProjectName  = web.Fields.GetByInternalNameOrTitle("Project name");
            Field fieldStartDate    = web.Fields.GetByInternalNameOrTitle("Start Date");
            Field fieldEndDate      = web.Fields.GetByInternalNameOrTitle("End Date");
            Field fieldNDescription = web.Fields.GetByInternalNameOrTitle("NDescription");
            Field fieldState        = web.Fields.GetByInternalNameOrTitle("State");

            ContentType Project = (from c in contentTypes
                                   where c.Name == "Project"
                                   select c).FirstOrDefault();

            Project.FieldLinks.Add(new FieldLinkCreationInformation
            {
                Field = fieldProjectName
            });
            Project.FieldLinks.Add(new FieldLinkCreationInformation
            {
                Field = fieldStartDate
            });
            Project.FieldLinks.Add(new FieldLinkCreationInformation
            {
                Field = fieldEndDate
            });
            Project.FieldLinks.Add(new FieldLinkCreationInformation
            {
                Field = fieldNDescription
            });
            Project.FieldLinks.Add(new FieldLinkCreationInformation
            {
                Field = fieldState
            });
            Project.Update(true);
            context.ExecuteQuery();

            Console.WriteLine("Success create content type");
        }
Esempio n. 20
0
        public static void CreateContentType(ClientContext ctx, string contentTypeName, string categoryFieldName)
        {
            //ctx.Web.ContentTypeExistsByName
            ContentTypeCollection contentTypes = ctx.Web.ContentTypes;

            ctx.Load(contentTypes);
            ctx.ExecuteQuery();
            if (ctx.Web.ContentTypeExistsByName(contentTypeName))
            {
                return;
            }


            // Create a Content Type Information object.
            ContentTypeCreationInformation newCt = new ContentTypeCreationInformation();

            // Set the name for the content type.
            newCt.Name = contentTypeName;


            //Site Page - 0x0101009D1CB255DA76424F860D91F20E6C4118
            newCt.ParentContentType = ctx.Web.ContentTypes.GetById("0x0101009D1CB255DA76424F860D91F20E6C4118");

            // Set content type to be available from specific group.
            newCt.Group = "LB Content Types";


            // Create the content type.
            Microsoft.SharePoint.Client.ContentType myContentType = contentTypes.Add(newCt);

            FieldLinkCollection fieldsCollection = myContentType.FieldLinks;

            ctx.Load(fieldsCollection);
            ctx.ExecuteQuery();



            FieldCollection fields = ctx.Site.RootWeb.Fields;

            ctx.Load(fields);
            ctx.ExecuteQuery();


            //Field f = ctx.Site.RootWeb.Fields.GetFieldByInternalName(categoryFieldName);
            fieldsCollection.Add(new FieldLinkCreationInformation
            {
                //Field = ctx.Site.RootWeb.Fields.GetFieldByInternalName(categoryFieldName)
                Field = fields.GetFieldByInternalName(categoryFieldName)
            });

            myContentType.Update(true);
            ctx.ExecuteQuery();
        }
Esempio n. 21
0
        /// <summary>
        /// Method to create Content Types
        /// </summary>
        /// <param name="clientContext">Client Context</param>
        /// <param name="siteColumns">List if site columns</param>
        /// <param name="contentTypeName">Name of Content Type</param>
        /// <param name="contentTypegroup">Name of group under which Content Type is to be added</param>
        /// <returns>Success or Failure</returns>
        internal static bool CreateContentType(ClientContext clientContext, List <string> siteColumns, string contentTypeName, string contentTypegroup)
        {
            try
            {
                int    totalSiteColumnsAdded = 0;
                string taxonomyFieldType     = ConfigurationManager.AppSettings["TaxonomyFieldType"];
                Console.WriteLine("Adding site columns to content type");
                Web web = clientContext.Web;
                ContentTypeCollection contentTypeCollection = web.ContentTypes;
                ContentType           parentContentType     = null;
                FieldCollection       fieldCol = web.Fields;
                clientContext.Load(contentTypeCollection, contentTypes => contentTypes.Include(properties => properties.Name));
                clientContext.Load(web.Fields);
                clientContext.Load(fieldCol, field => field.Include(fieldAttribute => fieldAttribute.Title, fieldAttr => fieldAttr.TypeAsString).Where(fieldAttribute => (fieldAttribute.Group == contentTypegroup) && (fieldAttribute.TypeAsString == taxonomyFieldType)));
                clientContext.ExecuteQuery();
                parentContentType = (from contentTypes in contentTypeCollection where contentTypes.Name == ConfigurationManager.AppSettings["parentcontenttype"] select contentTypes).FirstOrDefault();
                ContentType contentType = (from contentTypes in contentTypeCollection where contentTypes.Name == contentTypeName select contentTypes).FirstOrDefault();

                // Check if content type is not present, then only create new "Matter Center" content type
                if (null == contentType)
                {
                    ContentTypeCreationInformation contentTypeCreationInformation = new ContentTypeCreationInformation();
                    contentTypeCreationInformation.Name  = contentTypeName;
                    contentTypeCreationInformation.Group = contentTypegroup;
                    if (parentContentType != null)
                    {
                        contentTypeCreationInformation.ParentContentType = parentContentType;
                    }
                    contentType = web.ContentTypes.Add(contentTypeCreationInformation);
                    totalSiteColumnsAdded++;
                }

                clientContext.Load(contentType.Fields, field => field.Include(fieldAttribute => fieldAttribute.Title));
                clientContext.ExecuteQuery();
                MapMetadataColumns(clientContext, fieldCol);
                AddColumnsToContentType(web, siteColumns, contentType);
                web.Update();
                clientContext.ExecuteQuery();
                if (0 < totalSiteColumnsAdded)
                {
                    Console.WriteLine("Successfully added site columns to content type");
                }
                return(true);
            }
            catch (Exception exception)
            {
                ErrorLogger.LogErrorToTextFile(errorFilePath, "Message: " + exception.Message + "\nStacktrace: " + exception.StackTrace);
                return(false);
            }
        }
        static ContentType CreateContentType(string contentTypeName, string baseContentType)
        {
            DeleteContentType(contentTypeName);

            ContentTypeCreationInformation contentTypeCreateInfo = new ContentTypeCreationInformation();

            contentTypeCreateInfo.Name = contentTypeName;
            contentTypeCreateInfo.ParentContentType = site.ContentTypes.GetById(baseContentType);;
            contentTypeCreateInfo.Group             = "Wingtip";
            ContentType ctype = site.ContentTypes.Add(contentTypeCreateInfo);

            clientContext.ExecuteQuery();
            return(ctype);
        }
Esempio n. 23
0
        private void button2_Click_1(object sender, EventArgs e)
        {
            Microsoft.SharePoint.Client.ContentType ctDoc = ctx.Web.ContentTypes.GetById("0x0101");
            ctx.Load(ctDoc);
            ctx.ExecuteQuery();

            ContentTypeCreationInformation ctci = new ContentTypeCreationInformation();
            ctci.Description = "test";
            ctci.Group = "Test";
            ctci.ParentContentType = ctDoc;
            ctci.Name = RandomString(10);

            ctx.Web.ContentTypes.Add(ctci);
            ctx.ExecuteQuery();
        }
Esempio n. 24
0
        public static ContentType CreateContentType(ClientContext ctx, string ctyName, string group, string ctyId)
        {
            ContentTypeCreationInformation contentTypeCreation = new ContentTypeCreationInformation();
            contentTypeCreation.Name = ctyName;
            contentTypeCreation.Description = "Custom Content Type";
            contentTypeCreation.Group = group;
            contentTypeCreation.Id = ctyId;

            //Add the new content type to the collection
            ContentType ct = ctx.Web.ContentTypes.Add(contentTypeCreation);
            ctx.Load(ct);
            ctx.ExecuteQuery();

            return ct;
        }
Esempio n. 25
0
        public static ContentType CreateContentType(ClientContext ctx, string ctyName, string group, string ctyId)
        {
            ContentTypeCreationInformation contentTypeCreation = new ContentTypeCreationInformation();

            contentTypeCreation.Name        = ctyName;
            contentTypeCreation.Description = "Custom Content Type";
            contentTypeCreation.Group       = group;
            contentTypeCreation.Id          = ctyId;

            //Add the new content type to the collection
            ContentType ct = ctx.Web.ContentTypes.Add(contentTypeCreation);

            ctx.Load(ct);
            ctx.ExecuteQuery();

            return(ct);
        }
Esempio n. 26
0
        private string CreateContentType(ClientContext clientContext, Web web)
        {
            ContentTypeCollection          contentTypeColl     = clientContext.Web.ContentTypes;
            ContentTypeCreationInformation contentTypeCreation = new ContentTypeCreationInformation();

            contentTypeCreation.Name        = "Home Hero";
            contentTypeCreation.Description = "Custom Content Type created for hero control.";
            contentTypeCreation.Group       = "Branding";
            contentTypeCreation.Id          = ContentTypeID;

            //Add the new content type to the collection
            ContentType ct = contentTypeColl.Add(contentTypeCreation);

            clientContext.Load(ct);
            clientContext.ExecuteQuery();
            return(ct.Id.ToString());
        }
Esempio n. 27
0
        void createContentType()
        {
            ContentTypeCollection contentTypes = web.ContentTypes;

            context.Load(contentTypes);
            context.ExecuteQuery();

            ContentType parentContentType = (from contentType in contentTypes where contentType.Name == "Document" select contentType).FirstOrDefault();

            ContentTypeCreationInformation contentTypeCreationInformation = new ContentTypeCreationInformation();

            contentTypeCreationInformation.Name              = "Project documents";
            contentTypeCreationInformation.Description       = "Project documents";
            contentTypeCreationInformation.Group             = "CSOM Assignment";
            contentTypeCreationInformation.ParentContentType = parentContentType;

            ContentType newContentType = contentTypes.Add(contentTypeCreationInformation);

            context.Load(newContentType);
            context.ExecuteQuery();

            Field       fieldNTitle         = web.Fields.GetByInternalNameOrTitle("NTitle");
            Field       fieldNDescription   = web.Fields.GetByInternalNameOrTitle("NDescription");
            Field       fieldTypeOfDocument = web.Fields.GetByInternalNameOrTitle("Type of document");
            ContentType ProjectDocuments    = (from c in contentTypes
                                               where c.Name == "Project documents"
                                               select c).FirstOrDefault();

            ProjectDocuments.FieldLinks.Add(new FieldLinkCreationInformation
            {
                Field = fieldNTitle
            });
            ProjectDocuments.FieldLinks.Add(new FieldLinkCreationInformation
            {
                Field = fieldNDescription
            });
            ProjectDocuments.FieldLinks.Add(new FieldLinkCreationInformation
            {
                Field = fieldTypeOfDocument
            });
            ProjectDocuments.Update(true);
            context.ExecuteQuery();

            Console.WriteLine("Success create content type");
        }
Esempio n. 28
0
        private string CreateContentType(Web web, string name, string description, string group, string id)
        {
            if (web.ContentTypeExistsById(id))
            {
                return(id);
            }

            // add the description
            var ctypeCreationInfo = new ContentTypeCreationInformation()
            {
                Id          = id,
                Description = description,
                Group       = group,
                Name        = name
            };

            web.ContentTypes.Add(ctypeCreationInfo);
            web.Context.ExecuteQuery();
            return(id);
        }
Esempio n. 29
0
        //Content types methods
        private static void CreateSingleContentType(string CTName,
                                                    string CTDescription,
                                                    string CTGroup,
                                                    ContentType ParrentCT,
                                                    ContentTypeCollection cTypes)
        {
            DeleteCtIfExists(CTName);

            ContentTypeCreationInformation ctInfo = new ContentTypeCreationInformation()
            {
                Name              = CTName,
                Description       = CTDescription,
                Group             = CTGroup,
                ParentContentType = ParrentCT
            };

            cTypes.Add(ctInfo);
            context.ExecuteQuery();
            web.Update();
        }
        /// <summary>
        /// Create new content type to web
        /// </summary>
        /// <param name="web">Site to be processed - can be root web or sub site</param>
        /// <param name="name">Name of the content type</param>
        /// <param name="description">Description for the content type</param>
        /// <param name="id">Complete ID for the content type</param>
        /// <param name="group">Group for the content type</param>
        /// <returns></returns>
        public static ContentType CreateContentType(this Web web, string name, string description, string id, string group)
        {
            // Load the current collection of content types
            ContentTypeCollection contentTypes = web.ContentTypes;

            web.Context.Load(contentTypes);
            web.Context.ExecuteQuery();
            ContentTypeCreationInformation newCt = new ContentTypeCreationInformation();

            // Set the properties for the content type
            newCt.Name        = name;
            newCt.Id          = id;
            newCt.Description = description;
            newCt.Group       = group;
            ContentType myContentType = contentTypes.Add(newCt);

            web.Context.ExecuteQuery();

            //Return the content type object
            return(myContentType);
        }
Esempio n. 31
0
        private static void CreateContentType(ClientContext ctx, Web web, XElement xContentType)
        {
            try
            {
                // TODO(crhodes): This needs work as "supposedly" you are only supposed to set Id or parentContentTypeName

                ContentTypeCreationInformation newContentType = new ContentTypeCreationInformation();

                string contentTypeName = xContentType.Attribute("Name").Value;

                newContentType.Name  = contentTypeName;
                newContentType.Group = xContentType.Attribute("Group").Value;

                if (xContentType.Attribute("Id") != null)
                {
                    newContentType.Id = xContentType.Attribute("Id").Value;
                }

                if (xContentType.Attribute("ParentContentType") != null)
                {
                    string parentContentTypeName = xContentType.Attribute("ParentContentType").Value;

                    var contentTypes = ctx.LoadQuery(web.ContentTypes.Where(ct => ct.Name == parentContentTypeName));
                    ctx.ExecuteQuery();

                    newContentType.ParentContentType = contentTypes.FirstOrDefault();
                }

                web.ContentTypes.Add(newContentType);
                ctx.ExecuteQuery();

                // Now add any fields that were specified

                AddFieldsToContentType(ctx, web, xContentType, contentTypeName);
            }
            catch (Exception ex)
            {
                WriteDiagnosticOutput(ex.ToString());
            }
        }
Esempio n. 32
0
        /// <summary>
        /// Method to create Content Types
        /// </summary>
        /// <param name="clientContext">Client Context</param>
        /// <param name="contentTypeName">Name of Content Type</param>
        /// <param name="parentContentTypeName">Name of Parent Content Type</param>
        /// <param name="contentTypeGroup">Name of group under which Content Type is to be added</param>
        public static void CreateContentType(ClientContext clientContext, string contentTypeName, string parentContentTypeName, string contentTypeGroup)
        {
            try
            {
                Console.WriteLine("Creating content type");

                Web web = clientContext.Web;
                ContentTypeCollection contentTypeCollection = web.ContentTypes;
                ContentType           parentContentType     = null;
                clientContext.Load(contentTypeCollection, contentTypes => contentTypes.Include(properties => properties.Name));
                clientContext.Load(web.Fields);
                clientContext.ExecuteQuery();
                parentContentType = (from contentTypes in contentTypeCollection where contentTypes.Name == parentContentTypeName select contentTypes).FirstOrDefault();
                ContentType contentType = (from contentTypes in contentTypeCollection where contentTypes.Name == contentTypeName select contentTypes).FirstOrDefault();

                if (null != contentType)
                {
                    contentType.DeleteObject();
                    web.Update();
                    clientContext.ExecuteQuery();
                }

                ContentTypeCreationInformation contentTypeCreationObj = new ContentTypeCreationInformation();
                contentTypeCreationObj.Name  = contentTypeName;
                contentTypeCreationObj.Group = contentTypeGroup;
                if (parentContentType != null)
                {
                    contentTypeCreationObj.ParentContentType = parentContentType;
                }
                web.ContentTypes.Add(contentTypeCreationObj);
                web.Update();
                clientContext.ExecuteQuery();
                Console.WriteLine("Successfully created content type: " + contentTypeName);
            }
            catch (Exception exception)
            {
                Console.WriteLine("Exception occurred while creating content type: " + exception.Message);
            }
        }
Esempio n. 33
0
        internal static void CreateCustomerCT(ClientContext ctx)
        {
            //Looking for The Contetype by ID!
            if (!ctx.Web.ContentTypeExistsById(Constants.GUID.CustomerCT.CustomerCTGUID))
            {
                ContentTypeCreationInformation ctci = new ContentTypeCreationInformation();
                ctci.Name        = "ODA1_CustomerCT";
                ctci.Group       = "ODA1";
                ctci.Id          = Constants.GUID.CustomerCT.CustomerCTGUID;
                ctci.Description = "This is the contentype used for Office Devlopment 1 Mandetory Assinment";


                ContentType customerCT = ctx.Web.ContentTypes.Add(ctci);
                ctx.ExecuteQueryRetry();
                try {
                    ClearFields(ctx);
                }
                catch (Exception) {
                }
                CreateFields(ctx, customerCT);
            }
        }
Esempio n. 34
0
        /// <summary>
        /// Method to create Content Types
        /// </summary>
        /// <param name="clientContext">Client Context</param>
        /// <param name="contentTypeName">Name of Content Type</param>
        /// <param name="parentContentTypeName">Name of Parent Content Type</param>
        /// <param name="contentTypeGroup">Name of group under which Content Type is to be added</param>
        public static void CreateContentType(ClientContext clientContext, string contentTypeName, string parentContentTypeName, string contentTypeGroup)
        {
            try
            {
                Console.WriteLine("Creating content type");

                Web web = clientContext.Web;
                ContentTypeCollection contentTypeCollection = web.ContentTypes;
                ContentType parentContentType = null;
                clientContext.Load(contentTypeCollection, contentTypes => contentTypes.Include(properties => properties.Name));
                clientContext.Load(web.Fields);
                clientContext.ExecuteQuery();
                parentContentType = (from contentTypes in contentTypeCollection where contentTypes.Name == parentContentTypeName select contentTypes).FirstOrDefault();
                ContentType contentType = (from contentTypes in contentTypeCollection where contentTypes.Name == contentTypeName select contentTypes).FirstOrDefault();

                if (null != contentType)
                {
                    contentType.DeleteObject();
                    web.Update();
                    clientContext.ExecuteQuery();
                }

                ContentTypeCreationInformation contentTypeCreationObj = new ContentTypeCreationInformation();
                contentTypeCreationObj.Name = contentTypeName;
                contentTypeCreationObj.Group = contentTypeGroup;
                if (parentContentType != null)
                {
                    contentTypeCreationObj.ParentContentType = parentContentType;
                }
                web.ContentTypes.Add(contentTypeCreationObj);
                web.Update();
                clientContext.ExecuteQuery();
                Console.WriteLine("Successfully created content type: " + contentTypeName);
            }
            catch (Exception exception)
            {
                Console.WriteLine("Exception occurred while creating content type: " + exception.Message);
            }
        }
Esempio n. 35
0
        private ContentType CreateDocumentSetContentType(ClientContext clientContext, string contentTypeName, List <Field> fields)
        {
            ContentType ret = null;
            Web         web = clientContext.Web;

            clientContext.Load(web, w => w.ContentTypes, w => w.Fields);
            clientContext.ExecuteQuery();

            //Get default document set content type.
            ContentType ctDocumentSet = web.ContentTypes.FirstOrDefault(ct => ct.Name.IndexOf("document set", StringComparison.CurrentCultureIgnoreCase) != -1);

            ret = web.ContentTypes.FirstOrDefault(ct => ct.Name == contentTypeName);
            if (ret == null)
            {
                ContentTypeCreationInformation ctNewInfo = new ContentTypeCreationInformation();
                ctNewInfo.Name              = CONTENTTYPENAME;
                ctNewInfo.Description       = CONTENTTYPENAME;
                ctNewInfo.Group             = CONTENTTYPEGROUP;
                ctNewInfo.ParentContentType = ctDocumentSet;
                ret = web.ContentTypes.Add(ctNewInfo);
            }
            clientContext.Load(ret, ct => ct.FieldLinks, ct => ct.Id);
            clientContext.ExecuteQuery();
            foreach (Field field in fields)
            {
                FieldLink flAuthor = ret.FieldLinks.FirstOrDefault(link => link.Name == field.InternalName);
                if (flAuthor == null)
                {
                    FieldLinkCreationInformation fNewInfo = new FieldLinkCreationInformation();
                    fNewInfo.Field = field;
                    ret.FieldLinks.Add(fNewInfo);
                    ret.Update(true);
                    clientContext.Load(ret, ct => ct.FieldLinks);
                    clientContext.ExecuteQuery();
                }
            }
            return(ret);
        }
Esempio n. 36
0
		internal static void CreateCustomerCT(ClientContext ctx) {

			//Looking for The Contetype by ID!
			if (!ctx.Web.ContentTypeExistsById(Constants.GUID.CustomerCT.CustomerCTGUID)) {
				ContentTypeCreationInformation ctci = new ContentTypeCreationInformation();
				ctci.Name = "ODA1_CustomerCT";
				ctci.Group = "ODA1";
				ctci.Id = Constants.GUID.CustomerCT.CustomerCTGUID;
				ctci.Description = "This is the contentype used for Office Devlopment 1 Mandetory Assinment";


				ContentType customerCT = ctx.Web.ContentTypes.Add(ctci);
				ctx.ExecuteQueryRetry();
				try {
					ClearFields(ctx);

				}
				catch (Exception) {

				}
				CreateFields(ctx, customerCT);
			}
		}
		public void ReorderContentTypesTest()
		{
			using (var clientContext = TestCommon.CreateClientContext())
			{
				var web = clientContext.Web;
				clientContext.Load(web, w => w.ContentTypes);
				clientContext.ExecuteQueryRetry();

				// create content types
				var documentCtype = web.ContentTypes.FirstOrDefault(ct => ct.Name == "Document");
				var newCtypeInfo1 = new ContentTypeCreationInformation()
				{
					Name = "Test_ContentType1",
					ParentContentType = documentCtype,
					Group = "Test content types",
					Description = "This is a test content type"
				};
				var newCtypeInfo2 = new ContentTypeCreationInformation()
				{
					Name = "Test_ContentType2",
					ParentContentType = documentCtype,
					Group = "Test content types",
					Description = "This is a test content type"
				};
				var newCtypeInfo3 = new ContentTypeCreationInformation()
				{
					Name = "Test_ContentType3",
					ParentContentType = documentCtype,
					Group = "Test content types",
					Description = "This is a test content type"
				};

				var newCtype1 = web.ContentTypes.Add(newCtypeInfo1);
				var newCtype2 = web.ContentTypes.Add(newCtypeInfo2);
				var newCtype3 = web.ContentTypes.Add(newCtypeInfo3);
				clientContext.Load(newCtype1);
				clientContext.Load(newCtype2);
				clientContext.Load(newCtype3);
				clientContext.ExecuteQueryRetry();

				var newList = new ListCreationInformation()
				{
					TemplateType = (int)ListTemplateType.DocumentLibrary,
					Title = DOC_LIB_TITLE,
					Url = "TestLibrary"
				};

				var doclib = clientContext.Web.Lists.Add(newList);
				doclib.ContentTypesEnabled = true;
				doclib.ContentTypes.AddExistingContentType(newCtype1);
				doclib.ContentTypes.AddExistingContentType(newCtype2);
				doclib.ContentTypes.AddExistingContentType(newCtype3);
				doclib.Update();
				clientContext.Load(doclib.ContentTypes);
				clientContext.ExecuteQueryRetry();

				var expectedIds = new string[]{
					newCtype3.Name,
					newCtype1.Name,
					newCtype2.Name,
					documentCtype.Name
				};

				doclib.ReorderContentTypes(expectedIds);
				var reorderedCtypes = clientContext.LoadQuery(doclib.ContentTypes);
				clientContext.ExecuteQueryRetry();

				var actualIds = reorderedCtypes.Except(
					// remove the folder content type
										reorderedCtypes.Where(ct => ct.Id.StringValue.StartsWith("0x012000"))
									).Select(ct => ct.Name).ToArray();

				CollectionAssert.AreEqual(expectedIds, actualIds);
			}
		}
 /// <summary>
 /// Creates the type of the content.
 /// </summary>
 /// <param name="clientContext">The client context.</param>
 /// <param name="web">Object of site</param>
 /// <param name="siteColumns">The site columns.</param>
 /// <param name="contentTypeName">Name of Content Type</param>
 internal static bool CreateContentType(ClientContext clientContext, Microsoft.SharePoint.Client.Web web, List<string> siteColumns, string contentTypeName, string contentTypegroup)
 {
     bool status = true;
     ContentType parentContentType = null;
     try
     {
         ContentTypeCollection contentTypeCollection = web.ContentTypes;
         clientContext.Load(contentTypeCollection);
         clientContext.Load(web.Fields);
         clientContext.ExecuteQuery();
         parentContentType = (from ct in contentTypeCollection
                              where ct.Name == ServiceConstantStrings.OneDriveParentContentType
                              select ct).FirstOrDefault();
         ContentTypeCreationInformation contentType = new ContentTypeCreationInformation();
         contentType.Name = contentTypeName;
         contentType.Group = contentTypegroup;
         ///// contentType.Id = "0x010100B1ED198475FB3A4AABC59AAAD89B7EAD";
         if (parentContentType != null)
         {
             contentType.ParentContentType = parentContentType;
         }
         ContentType finalObj = web.ContentTypes.Add(contentType);
         AddColumnsToContentType(web, siteColumns, finalObj);
         web.Update();
         clientContext.ExecuteQuery();
     }
     catch (Exception exception)
     {
         status = false;
         Logger.LogError(exception, MethodBase.GetCurrentMethod().DeclaringType.Name, MethodBase.GetCurrentMethod().Name, ServiceConstantStrings.LogTableName);
     }
     return status;
 }
Esempio n. 39
0
        /// <summary>
        /// Method to create Content Types
        /// </summary>
        /// <param name="clientContext">Client Context</param>
        /// <param name="siteColumns">List if site columns</param>
        /// <param name="contentTypeName">Name of Content Type</param>
        /// <param name="contentTypegroup">Name of group under which Content Type is to be added</param>
        /// <returns>Success or Failure</returns>
        internal static bool CreateContentType(ClientContext clientContext, List<string> siteColumns, string contentTypeName, string contentTypegroup)
        {
            try
            {
                int totalSiteColumnsAdded = 0;
                string taxonomyFieldType = ConfigurationManager.AppSettings["TaxonomyFieldType"];
                Console.WriteLine("Adding site columns to content type");
                Web web = clientContext.Web;
                ContentTypeCollection contentTypeCollection = web.ContentTypes;
                ContentType parentContentType = null;
                FieldCollection fieldCol = web.Fields;
                clientContext.Load(contentTypeCollection, contentTypes => contentTypes.Include(properties => properties.Name));
                clientContext.Load(web.Fields);
                clientContext.Load(fieldCol, field => field.Include(fieldAttribute => fieldAttribute.Title, fieldAttr => fieldAttr.TypeAsString).Where(fieldAttribute => (fieldAttribute.Group == contentTypegroup) && (fieldAttribute.TypeAsString == taxonomyFieldType)));
                clientContext.ExecuteQuery();
                parentContentType = (from contentTypes in contentTypeCollection where contentTypes.Name == ConfigurationManager.AppSettings["parentcontenttype"] select contentTypes).FirstOrDefault();
                ContentType contentType = (from contentTypes in contentTypeCollection where contentTypes.Name == contentTypeName select contentTypes).FirstOrDefault();

                // Check if content type is not present, then only create new "Matter Center" content type
                if (null == contentType)
                {
                    ContentTypeCreationInformation contentTypeCreationInformation = new ContentTypeCreationInformation();
                    contentTypeCreationInformation.Name = contentTypeName;
                    contentTypeCreationInformation.Group = contentTypegroup;
                    if (parentContentType != null)
                    {
                        contentTypeCreationInformation.ParentContentType = parentContentType;
                    }
                    contentType = web.ContentTypes.Add(contentTypeCreationInformation);
                    totalSiteColumnsAdded++;
                }

                clientContext.Load(contentType.Fields, field => field.Include(fieldAttribute => fieldAttribute.Title));
                clientContext.ExecuteQuery();
                MapMetadataColumns(clientContext, fieldCol);
                AddColumnsToContentType(web, siteColumns, contentType);
                web.Update();
                clientContext.ExecuteQuery();
                if (0 < totalSiteColumnsAdded)
                    Console.WriteLine("Successfully added site columns to content type");
                return true;
            }
            catch (Exception exception)
            {
                ErrorLogger.LogErrorToTextFile(errorFilePath, "Message: " + exception.Message + "\nStacktrace: " + exception.StackTrace);
                return false;
            }
        }
        protected override void ProcessRecord()
        {
            base.ProcessRecord();
            var ctx = base.Context;
            Web web = ctx.Site.OpenWeb(Web.Read());

            SPOContentType newContentType = null;
            SPOContentType parentContentType = ParentContentType.Read(web);
            if (parentContentType == null)
            {
                throw new ArgumentException("Unable to locate the specified parent content type.");
            }
            SPOContentType existingContentType = SPOContentType.GetContentType(ctx, web.AvailableContentTypes, Name);
            if (existingContentType != null)
            {
                WriteWarning("The content type \"" + Name + "\" already exists within the Site.");
                WriteObject(existingContentType);
                return;
            }
            List<SPOField> fields = new List<SPOField>();
            if (FieldsToAdd != null)
            {
                foreach (string fieldName in FieldsToAdd)
                {
                    SPOField existingField = SPOField.GetField(ctx, web.AvailableFields, fieldName);
                    if (existingField == null)
                        WriteError(new ErrorRecord(new ArgumentOutOfRangeException("Unable to locate field " + fieldName + ". Content Type will not be created."), null, ErrorCategory.InvalidData, web.AvailableFields));
                    else
                        fields.Add(existingField);
                }
                if (fields.Count != FieldsToAdd.Length)
                    return;
            }

            var ctli = new ContentTypeCreationInformation();
            ctli.Description = Description;
            ctli.Group = Group;
            ctli.Name = Name;
            ctli.ParentContentType = parentContentType.ContentType;

            ContentType ct = web.ContentTypes.Add(ctli);
            ctx.ExecuteQuery();
            SPOContentType.LoadContentType(ctx, ct);
            newContentType = new SPOContentType(ct);

            foreach (SPOField field in fields)
            {
                FieldLinkCreationInformation flci = new FieldLinkCreationInformation();
                flci.Field = field.Field;
                newContentType.ContentType.FieldLinks.Add(flci);
            }
            if (fields.Count > 0)
            {
                newContentType.ContentType.Update(true);
                ctx.ExecuteQuery();
            }

            WriteObject(newContentType);
        }
Esempio n. 41
0
        private ContentType CreateDocumentSetContentType(ClientContext clientContext, string contentTypeName, List<Field> fields)
        {
            ContentType ret = null;
            Web web = clientContext.Web;
            clientContext.Load(web, w => w.ContentTypes, w => w.Fields);
            clientContext.ExecuteQuery();

            //Get default document set content type.
            ContentType ctDocumentSet = web.ContentTypes.FirstOrDefault(ct => ct.Name.IndexOf("document set", StringComparison.CurrentCultureIgnoreCase) != -1);
            ret = web.ContentTypes.FirstOrDefault(ct => ct.Name == contentTypeName);
            if (ret == null)
            {
                ContentTypeCreationInformation ctNewInfo = new ContentTypeCreationInformation();
                ctNewInfo.Name = CONTENTTYPENAME;
                ctNewInfo.Description = CONTENTTYPENAME;
                ctNewInfo.Group = CONTENTTYPEGROUP;
                ctNewInfo.ParentContentType = ctDocumentSet;
                ret = web.ContentTypes.Add(ctNewInfo);
            }
            clientContext.Load(ret, ct => ct.FieldLinks, ct => ct.Id);
            clientContext.ExecuteQuery();
            foreach (Field field in fields)
            {
                FieldLink flAuthor = ret.FieldLinks.FirstOrDefault(link => link.Name == field.InternalName);
                if (flAuthor == null)
                {
                    FieldLinkCreationInformation fNewInfo = new FieldLinkCreationInformation();
                    fNewInfo.Field = field;
                    ret.FieldLinks.Add(fNewInfo);
                    ret.Update(true);
                    clientContext.Load(ret, ct => ct.FieldLinks);
                    clientContext.ExecuteQuery();
                }
            }
            return ret;

        }
Esempio n. 42
0
        private string CreateContentType(Web web, string name, string description, string group, string id) {
            if (web.ContentTypeExistsById(id))
                return id;

            // add the description
            var ctypeCreationInfo = new ContentTypeCreationInformation() {
                Id = id,
                Description = description,
                Group = group,
                Name = name
            };
            web.ContentTypes.Add(ctypeCreationInfo);
            web.Context.ExecuteQuery();
            return id;
        }
Esempio n. 43
0
        private string CreateContentType(ClientContext clientContext, Web web)
        {
            ContentTypeCollection contentTypeColl = clientContext.Web.ContentTypes;
            ContentTypeCreationInformation contentTypeCreation = new ContentTypeCreationInformation();
            contentTypeCreation.Name = "Home Hero";
            contentTypeCreation.Description = "Custom Content Type created for hero control.";
            contentTypeCreation.Group = "Branding";
            contentTypeCreation.Id = ContentTypeID;

            //Add the new content type to the collection
            ContentType ct = contentTypeColl.Add(contentTypeCreation);
            clientContext.Load(ct);
            clientContext.ExecuteQuery();
            return ct.Id.ToString();
        }
        /// <summary>
        /// Create new content type to web
        /// </summary>
        /// <param name="web">Site to be processed - can be root web or sub site</param>
        /// <param name="name">Name of the content type</param>
        /// <param name="description">Description for the content type</param>
        /// <param name="id">Complete ID for the content type</param>
        /// <param name="group">Group for the content type</param>
        /// <param name="parentContentType">Parent Content Type</param>
        /// <returns></returns>
        public static ContentType CreateContentType(this Web web, string name, string description, string id, string group, ContentType parentContentType = null)
        {
            // Load the current collection of content types
            ContentTypeCollection contentTypes = web.ContentTypes;
            web.Context.Load(contentTypes);
            web.Context.ExecuteQuery();
            ContentTypeCreationInformation newCt = new ContentTypeCreationInformation();

            // Set the properties for the content type
            newCt.Name = name;
            newCt.Id = id;
            newCt.Description = description;
            newCt.Group = group;
            newCt.ParentContentType = parentContentType;
            ContentType myContentType = contentTypes.Add(newCt);
            web.Context.ExecuteQuery();

            //Return the content type object
            return myContentType;
        }