Пример #1
0
        public static void CreateContentTypeFromXML(this Web web, XmlDocument xmlDoc)
        {
            XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);

            nsmgr.AddNamespace("namespace", xmlDoc.DocumentElement.NamespaceURI);

            XmlNodeList contentTypes = xmlDoc.SelectNodes("//namespace:ContentType", nsmgr);
            int         count        = contentTypes.Count;

            foreach (XmlNode ct in contentTypes)
            {
                string ctid = ct.Attributes["ID"].Value;
                string name = ct.Attributes["Name"].Value;
                if (web.ContentTypeExistsByName(name))
                {
                    LoggingUtility.Internal.TraceWarning((int)EventId.ContentTypeAlreadyExists, CoreResources.FieldAndContentTypeExtensions_ContentType01AlreadyExists, name, ctid);
                    // Skip
                }
                else
                {
                    var description = "";
                    if (((XmlElement)ct).HasAttribute("Description"))
                    {
                        description = ((XmlElement)ct).GetAttribute("Description");
                    }
                    var group = "";
                    if (((XmlElement)ct).HasAttribute("Group"))
                    {
                        group = ((XmlElement)ct).GetAttribute("Group");
                    }

                    //Create CT
                    web.CreateContentType(name, description, ctid, group);

                    //Add fields to content type
                    XmlNodeList fieldRefs = ct.SelectNodes(".//namespace:FieldRef", nsmgr);
                    foreach (XmlNode fr in fieldRefs)
                    {
                        bool   required = false;
                        bool   hidden   = false;
                        string frid     = fr.Attributes["ID"].Value;
                        string frName   = fr.Attributes["Name"].Value;
                        var    attr     = fr.Attributes["Required"];
                        if (attr != null)
                        {
                            required = attr.Value.ToBoolean();
                        }
                        attr = fr.Attributes["Hidden"];
                        if (attr != null)
                        {
                            hidden = attr.Value.ToBoolean();
                        }
                        web.AddFieldToContentTypeById(ctid, frid, required, hidden);
                    }
                }
            }
        }
Пример #2
0
        public static void CreateContentTypeFromXML(this Web web, XmlDocument xmlDoc)
        {
            XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);

            nsmgr.AddNamespace("namespace", "http://schemas.microsoft.com/sharepoint/");

            XmlNodeList fields = xmlDoc.SelectNodes("//namespace:ContentType", nsmgr);
            int         count  = fields.Count;

            foreach (XmlNode ct in fields)
            {
                string ctid        = ct.Attributes["ID"].Value;
                string name        = ct.Attributes["Name"].Value;
                string description = ct.Attributes["Description"].Value;
                string group       = ct.Attributes["Group"].Value;

                if (web.ContentTypeExistsByName(name))
                {
                    continue;
                }

                //Create CT
                web.CreateContentType(name, description, ctid, group);

                //Add fields to content type
                XmlNodeList  fieldRefs = ct.SelectNodes(".//namespace:FieldRef", nsmgr);
                XmlAttribute attr      = null;
                foreach (XmlNode fr in fieldRefs)
                {
                    bool   required = false;
                    bool   hidden   = false;
                    string frid     = fr.Attributes["ID"].Value;
                    string frName   = fr.Attributes["Name"].Value;
                    attr = fr.Attributes["Required"];
                    if (attr != null)
                    {
                        bool.TryParse(attr.Value, out required);
                    }
                    attr = fr.Attributes["Hidden"];
                    if (attr != null)
                    {
                        bool.TryParse(attr.Value, out hidden);
                    }
                    web.AddFieldToContentTypeById(ctid, frid, required, hidden);
                }
            }
        }
        public static void MyFirstContentType(ClientContext ctx)
        {
            Web rootWeb = ctx.Site.RootWeb;

            //string carsID = "0x01001703716880F147E38BD026AA8DDD14D5";

            //if (rootWeb.ContentTypeExistsById(carsID))
            //{
            //    rootWeb.DeleteContentTypeById(carsID);
            //}

            //rootWeb.CreateContentType("Cars", carsID, "Tims Columns");


            //FieldCreationInformation brandField = new FieldCreationInformation(FieldType.Text);
            //brandField.DisplayName = "Brand";
            //brandField.Id = new Guid("{F4D37EFA-3CD5-4426-8F49-D2F48F841653}");
            //brandField.InternalName = "CMS_Brand";
            //brandField.Group = "Tims Columns";

            //if (rootWeb.FieldExistsById(brandField.Id))
            //{
            //    rootWeb.RemoveFieldById(brandField.Id.ToString());
            //}


            //rootWeb.CreateField(brandField);

            //rootWeb.AddFieldToContentTypeById(carsID, "{F4D37EFA-3CD5-4426-8F49-D2F48F841653}", false);

            //FieldCreationInformation yearField = new FieldCreationInformation(FieldType.Number);
            //yearField.DisplayName = "Year";
            //yearField.Id = new Guid("{F1CE6B6C-652B-470B-8299-5081E16A8CB2}");
            //yearField.InternalName = "CMS_Year";
            //yearField.Group = "Tims Columns";

            //if (rootWeb.FieldExistsById(yearField.Id))
            //{
            //    rootWeb.RemoveFieldById(yearField.Id.ToString());
            //}

            //rootWeb.CreateField(yearField);

            //rootWeb.AddFieldToContentTypeById(carsID, "{F1CE6B6C-652B-470B-8299-5081E16A8CB2}", false);



            //FieldCreationInformation colorField = new FieldCreationInformation(FieldType.Choice);
            //colorField.DisplayName = "Color";
            //colorField.Id = new Guid("{9EC39186-A1E6-4DA3-8343-84A7C7137714}");
            //colorField.InternalName = "CMS_Color";
            //colorField.Group = "Tims Columns";

            //if (rootWeb.FieldExistsById(colorField.Id))
            //{
            //    rootWeb.RemoveFieldById(colorField.Id.ToString());

            //}



            //var fc = rootWeb.CreateField<FieldChoice>(colorField);

            //fc.Choices = new string[] { "red", "green", "blue" };

            ////rootWeb.CreateField(colorField);
            //fc.Update();
            //ctx.ExecuteQuery();

            //rootWeb.AddFieldToContentTypeById(carsID, "{9EC39186-A1E6-4DA3-8343-84A7C7137714}", false);


            //List list = rootWeb.GetListByTitle("Tims List From Pnp");
            //list.AddContentTypeToList(rootWeb.GetContentTypeById(carsID), true);

            //for (int i = 0; i < 5; i++)
            //{
            //    list.CreateDocument("dokument " + i, list.RootFolder, DocumentTemplateType.Word);
            //    Console.WriteLine("done"+i);
            //}
            //list.DefaultView.ViewFields.Add("CMS_Brand");
            //list.DefaultView.ViewFields.Add("CMS_Year");
            //list.DefaultView.ViewFields.Add("CMS_Color");
            //list.DefaultView.Update();

            //list.Update();
            //ctx.ExecuteQuery();


            //this is another way to do it using xml
            //rootWeb.Fields.AddFieldAsXml("<field DisplayName = "brand"></fields>)

            string booksID = "0x01003DCFC9B08E1E4DD18AB8BD9053D7F49E";

            if (rootWeb.ContentTypeExistsById(booksID))
            {
                rootWeb.DeleteContentTypeById(booksID);
            }

            rootWeb.CreateContentType("Books", booksID, "Tims Columns");



            FieldCreationInformation bookType = new FieldCreationInformation(FieldType.Choice);

            bookType.DisplayName  = "Book Type";
            bookType.Id           = new Guid("{F0C6D85D-C4DD-48F2-806E-5794ABDCAAB0}");
            bookType.InternalName = "CMS_BookType";
            bookType.Group        = "Tims Columns";

            var fcs = rootWeb.CreateField <FieldChoice>(bookType);

            fcs.Choices = new string[] { "Large", "Medium", "Small" };

            fcs.Update();
            ctx.ExecuteQuery();


            FieldCreationInformation author = new FieldCreationInformation(FieldType.Text);

            author.DisplayName  = "Author";
            author.Id           = new Guid("{A95D3D0B-F076-4675-A99A-7D8EED002481}");
            author.InternalName = "CMS_Author";
            author.Group        = "Tims Columns";

            FieldCreationInformation dateReleased = new FieldCreationInformation(FieldType.DateTime);

            dateReleased.DisplayName  = "Date released";
            dateReleased.Id           = new Guid("{7C81173A-141C-4008-B2E6-A8763F0850DF}");
            dateReleased.InternalName = "CMS_dateReleased";
            dateReleased.Group        = "Tims Columns";

            FieldCreationInformation description = new FieldCreationInformation(FieldType.Note);

            description.DisplayName  = "Description";
            description.Id           = new Guid("{B65E6297-0182-4CA8-A7DE-45E05FE61086}");
            description.InternalName = "CMS_description";
            description.Group        = "Tims Columns";


            //rootWeb.CreateField(bookType);
            rootWeb.CreateField(author);
            rootWeb.CreateField(dateReleased);
            rootWeb.CreateField(description);
            rootWeb.AddFieldToContentTypeById(booksID, "{F0C6D85D-C4DD-48F2-806E-5794ABDCAAB0}", false);
            rootWeb.AddFieldToContentTypeById(booksID, "{A95D3D0B-F076-4675-A99A-7D8EED002481}", false);
            rootWeb.AddFieldToContentTypeById(booksID, "{7C81173A-141C-4008-B2E6-A8763F0850DF}", false);
            rootWeb.AddFieldToContentTypeById(booksID, "{B65E6297-0182-4CA8-A7DE-45E05FE61086}", false);

            List list = rootWeb.CreateList(ListTemplateType.GenericList, "books", false, true, "TimsGenericList", true);

            list.AddContentTypeToListById(booksID, true);
        }
Пример #4
0
        public static void CreateProductContentType(ClientContext ctx)
        {
            Web root = ctx.Site.RootWeb;

            // inherits from welcomepage. We add a 00 then our new quid without spaces to construct our new id.
            // because it starts with the welcome page id sharepoint knows that the parent of our new content type
            // is the welcome page.
            string productPageCTID = Constants.WELCOME_PAGE_ID + "00" + Constants.PRODUCT_PAGE_END_ID;

            if (!root.ContentTypeExistsByName("Product Page")) // if ct not exists
            {
                ContentType ct = root.CreateContentType("Product Page", productPageCTID, "OD2");
            }


            if (!root.FieldExistsById(Constants.FIELD_RELEASEDATE_ID.ToGuid()))
            {
                FieldCreationInformation releaseDateFieldInfo = new FieldCreationInformation(FieldType.DateTime)
                {
                    InternalName = "OD2_ReleaseDate",
                    DisplayName  = "Release Date",
                    Id           = Constants.FIELD_RELEASEDATE_ID.ToGuid(),
                    Group        = "OD2"
                };
                root.CreateField(releaseDateFieldInfo);
                root.AddFieldToContentTypeById(productPageCTID, Constants.FIELD_RELEASEDATE_ID);
            }


            if (!root.FieldExistsById(Constants.FIELD_PRODCAT_ID.ToGuid()))
            {
                TermSet ts = ctx.Site.GetDefaultKeywordsTermStore().GetTermSet(Constants.TAXONOMY_PRODUCTCAT_TERMSET_ID.ToGuid());
                ctx.Load(ts);
                ctx.ExecuteQuery();

                TaxonomyFieldCreationInformation prodCatFieldInfo = new TaxonomyFieldCreationInformation()
                {
                    Id           = Constants.FIELD_PRODCAT_ID.ToGuid(),
                    InternalName = "OD2_ProdCat",
                    DisplayName  = "ProductCategory",
                    Group        = "OD2",
                    TaxonomyItem = ts
                };

                root.CreateTaxonomyField(prodCatFieldInfo);
                root.AddFieldToContentTypeById(productPageCTID, Constants.FIELD_PRODCAT_ID);
            }

            if (!root.FieldExistsById(Constants.FIELD_OWNER_ID.ToGuid()))
            {
                FieldCreationInformation ownerFieldInfo = new FieldCreationInformation(FieldType.User)
                {
                    InternalName = "OD2_Owner",
                    DisplayName  = "Owner",
                    Id           = Constants.FIELD_OWNER_ID.ToGuid(),
                    Group        = "OD2"
                };
                root.CreateField(ownerFieldInfo);
                root.AddFieldToContentTypeById(productPageCTID, Constants.FIELD_OWNER_ID);
            }

            // taxonomy keyword.. we will add the hidden note field along with the taxonomykeyword field
            // so it does not cause any problems.
            root.AddFieldToContentTypeById(productPageCTID, "{1390a86a-23da-45f0-8efe-ef36edadfb39}"); // TaxKeyWord hidden field
            root.AddFieldToContentTypeById(productPageCTID, "{23f27201-bee3-471e-b2e7-b64fd8b7ca38}"); // TaxKeyWord
        }
Пример #5
0
        public static void CreateCV(ClientContext ctx)
        {
            string cVCT = "0x010100A959F697950047DF80D85119D99F8CA7";

            Web web = ctx.Site.RootWeb;

            if (!web.ContentTypeExistsById(cVCT))
            {
                web.CreateContentType("CV", cVCT, "Davids ContentType");
            }


            string picFieldId = "{98A1C95C-AA0F-4D2C-92C8-5407594C440F}";

            if (!web.FieldExistsById(new Guid(picFieldId)))
            {
                FieldCreationInformation info = new FieldCreationInformation(FieldType.URL);
                info.Id           = picFieldId.ToGuid();
                info.InternalName = "DAV_Pic";
                info.DisplayName  = "Picture";
                info.Group        = "Tims Columns";

                FieldUrl picfield = web.CreateField <FieldUrl>(info);
                picfield.DisplayFormat = UrlFieldFormatType.Image;
                picfield.Update();
                ctx.ExecuteQuery();
            }

            string userFieldId = "{B0C1EFC4-189E-4626-A1DC-1CCC4693C097}";

            if (!web.FieldExistsById(new Guid(userFieldId)))
            {
                FieldCreationInformation info = new FieldCreationInformation(FieldType.User);
                info.Id           = userFieldId.ToGuid();
                info.InternalName = "DAV_User";
                info.DisplayName  = "User";
                info.Group        = "Tims Columns";
                FieldUser userfield = web.CreateField <FieldUser>(info);
                ctx.ExecuteQuery();
            }

            string activeFieldId = "{2CB24A28-3F5B-49AE-9F54-5FD8747DBF19}";

            if (!web.FieldExistsById(new Guid(activeFieldId)))
            {
                FieldCreationInformation info = new FieldCreationInformation(FieldType.Boolean);
                info.Id           = activeFieldId.ToGuid();
                info.InternalName = "DAV_Active";
                info.DisplayName  = "Active";
                info.Group        = "Tims Columns";
                web.CreateField(info);
            }

            web.AddFieldToContentTypeById(cVCT, picFieldId);
            web.AddFieldToContentTypeById(cVCT, userFieldId);
            web.AddFieldToContentTypeById(cVCT, activeFieldId);


            if (!web.ListExists("CVs"))
            {
                List list = web.CreateList(ListTemplateType.DocumentLibrary, "CVs", true, enableContentTypes: true);
                list.AddContentTypeToListById(cVCT);
            }

            List CVList = web.GetListByTitle("CVs");

            FileCreationInformation fileinfo = new FileCreationInformation();

            System.IO.FileStream fileStream = System.IO.File.OpenRead(@"C:\Users\timha\source\repos\Officedeveloper1\ContentTypesAndFields\ContentTypesAndFields\TextFile1.txt");
            fileinfo.Content = ReadFully(fileStream);
            fileinfo.Url     = "file1.txt";
            Microsoft.SharePoint.Client.File files = CVList.RootFolder.Files.Add(fileinfo);
            ctx.ExecuteQuery();

            User user = web.EnsureUser("*****@*****.**");

            ctx.Load(user);
            ctx.ExecuteQuery();


            ListItem item = files.ListItemAllFields;

            item["Title"]         = "Tim";
            item["ContentTypeId"] = cVCT;

            FieldUrlValue picvalue = new FieldUrlValue();

            picvalue.Description = "Tim";
            picvalue.Url         = "https://images.pexels.com/photos/104827/cat-pet-animal-domestic-104827.jpeg?w=940&h=650&auto=compress&cs=tinysrgb";
            item["DAV_Pic"]      = picvalue;

            item["DAV_User"]   = user.Id;
            item["DAV_Active"] = true;


            item.Update();
            ctx.ExecuteQuery();
        }
Пример #6
0
        public static void CreateBookCT(ClientContext ctx)
        {
            string bookCT = "0x01000E870749A9444905BB8A362E475B0798";

            Web web = ctx.Site.RootWeb;

            //web.GetListByTitle("Books2").DeleteObject();
            //ctx.ExecuteQuery();
            //web.DeleteContentTypeById(bookCT);

            if (!web.ContentTypeExistsById(bookCT))
            {
                web.CreateContentType("David Books", bookCT, "Davids ContentType");
            }

            string bookTypeFieldId = "{DBB24705-0DEA-4C4F-8C2A-95CB6F0DE25E}";

            if (!web.FieldExistsById(new Guid(bookTypeFieldId)))
            {
                FieldCreationInformation info = new FieldCreationInformation(FieldType.Choice);
                info.Id           = bookTypeFieldId.ToGuid();
                info.InternalName = "DAV_BookType";
                info.DisplayName  = "Book Type";
                info.Group        = "Tims Columns";


                FieldChoice field = web.CreateField <FieldChoice>(info);
                field.Choices = new string[] { "Romance", "Drama", "Horror", "Thriller" };
                field.Update();
                ctx.ExecuteQuery();
            }


            string authorFieldId = "{D6996667-0BEA-4C9F-9904-DEB21CC5AA84}";

            if (!web.FieldExistsById(new Guid(authorFieldId)))
            {
                FieldCreationInformation info = new FieldCreationInformation(FieldType.Text);
                info.Id           = authorFieldId.ToGuid();
                info.InternalName = "DAV_Author";
                info.DisplayName  = "Author";
                info.Group        = "Tims Columns";


                Field field = web.CreateField(info);
            }

            string releaseDateFieldId = "{84716863-06CA-4D31-BAA0-7D099FC501E7}";

            if (!web.FieldExistsById(new Guid(releaseDateFieldId)))
            {
                FieldCreationInformation info = new FieldCreationInformation(FieldType.DateTime);
                info.Id           = releaseDateFieldId.ToGuid();
                info.InternalName = "DAV_Realesedate";
                info.DisplayName  = "ReleaseDate";
                info.Group        = "Tims Columns";


                FieldDateTime field = web.CreateField <FieldDateTime>(info);
                field.DisplayFormat = DateTimeFieldFormatType.DateOnly;
                field.Update();
                ctx.ExecuteQuery();
            }


            string descriptionDateFieldId = "{4BD3F599-4D5C-412D-8431-6ECD36AEB015}";

            // web.RemoveFieldById(descriptionDateFieldId);

            if (!web.FieldExistsById(new Guid(descriptionDateFieldId)))
            {
                FieldCreationInformation info = new FieldCreationInformation(FieldType.Note);
                info.Id           = descriptionDateFieldId.ToGuid();
                info.InternalName = "DAV_description";
                info.DisplayName  = "Description";
                info.Group        = "Tims Columns";
                info.Required     = true;
                FieldMultiLineText field = web.CreateField <FieldMultiLineText>(info);

                field.RichText       = true;
                field.NumberOfLines  = 10;
                field.AllowHyperlink = true;
                field.Update();
                ctx.ExecuteQuery();
            }

            web.AddFieldToContentTypeById(bookCT, bookTypeFieldId);
            web.AddFieldToContentTypeById(bookCT, authorFieldId);
            web.AddFieldToContentTypeById(bookCT, releaseDateFieldId);
            web.AddFieldToContentTypeById(bookCT, descriptionDateFieldId, true);


            if (!web.ListExists("Books2"))
            {
                List list = web.CreateList(ListTemplateType.GenericList, "Books2", false, urlPath: "lists/books2", enableContentTypes: true);
                list.AddContentTypeToListById(bookCT, true);

                View listView = list.DefaultView;
                listView.ViewFields.Add("DAV_BookType");
                listView.ViewFields.Add("DAV_Author");
                listView.ViewFields.Add("DAV_Realesedate");
                listView.ViewFields.Add("DAV_description");
                listView.Update();
                ctx.ExecuteQueryRetry();
            }

            List bookList = web.GetListByTitle("Books2");

            ListItem item = bookList.AddItem(new ListItemCreationInformation());

            item["Title"]           = "MistBorn";
            item["DAV_BookType"]    = "Fantasy";
            item["DAV_Author"]      = "Brandon Sanderson";
            item["DAV_Realesedate"] = DateTime.Parse("2001-02-12");
            item["DAV_description"] = "This is a decription \n\n is this a new line?";

            item.Update();
            ctx.ExecuteQuery();


            //ListItemCollection items = bookList.GetItems(CamlQuery.CreateAllItemsQuery());
            //ctx.Load(items);
            //ctx.ExecuteQuery();
        }