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"); }
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 }
/// <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 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); }
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); } }
public void CreateContentTypes() { Web web = ClientContext.Web; ContentTypeCollection existingContentTypes = web.ContentTypes; ClientContext.Load(existingContentTypes); ClientContext.ExecuteQuery(); foreach (GtContentType contentType in ContentTypes) { if (existingContentTypes.Any(item => item.Id.ToString().Equals(contentType.ID.ToString(CultureInfo.InvariantCulture)))) { // We want to add fields even if the content type exists (?) AddSiteColumnsToContentType(contentType); } else { var contentTypeCreationInformation = contentType.GetContentTypeCreationInformation(); var newContentType = existingContentTypes.Add(contentTypeCreationInformation); ClientContext.ExecuteQuery(); // Update display name (internal name will not be changed) newContentType.Name = contentType.DisplayName; newContentType.Update(true); ClientContext.ExecuteQuery(); AddSiteColumnsToContentType(contentType); } } }
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."); }
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()); } }
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()); } }
public void EnsureContentTypes() { Web web = ClientContext.Web; ContentTypeCollection existingContentTypes = web.ContentTypes; ClientContext.Load(existingContentTypes); ClientContext.ExecuteQuery(); foreach (ShContentType contentType in ContentTypes) { var existingContentType = existingContentTypes.SingleOrDefault( item => item.Id.ToString().Equals(contentType.ID.ToString(CultureInfo.InvariantCulture))); if (existingContentType != null) { if (existingContentType.Group != contentType.Group) { Log.Debug("Updating group of content type " + contentType.DisplayName); existingContentType.Group = contentType.Group; existingContentType.Update(true); ClientContext.ExecuteQuery(); } if (existingContentType.Name != contentType.DisplayName) { Log.Debug("Updating display name of content type " + contentType.DisplayName); existingContentType.Name = contentType.DisplayName; existingContentType.Update(true); ClientContext.ExecuteQuery(); } var template = contentType.Template; if (template != null) { AddTemplateToContentType(contentType); } } else { Log.Debug("Creating content type " + contentType.DisplayName); var contentTypeCreationInformation = contentType.GetContentTypeCreationInformation(); var newContentType = existingContentTypes.Add(contentTypeCreationInformation); ClientContext.ExecuteQuery(); // Update display name (internal name will not be changed) newContentType.Name = contentType.DisplayName; newContentType.Update(true); ClientContext.ExecuteQuery(); var template = contentType.Template; if (template != null) { AddTemplateToContentType(contentType); } } Log.Debug("Adding fields to existing content type " + contentType.DisplayName); // We want to add fields even if the content type exists AddSiteColumnsToContentType(contentType); } }
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); }
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"); }
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(); }
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"); }
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()); }
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"); }
//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); }
public void CreateContentType(string contentTypeName, string parentContentTypeName) { ContentTypeCollection collection = _context.Site.RootWeb.ContentTypes; _context.Load(collection); _context.ExecuteQuery(); //Emp ContentType parentContentType = (from c in collection where c.Name == parentContentTypeName select c).FirstOrDefault(); ContentTypeCreationInformation contentType = new ContentTypeCreationInformation { Name = contentTypeName, Group = "IT Content Type", ParentContentType = parentContentType }; collection.Add(contentType); _context.ExecuteQuery(); }
public ContentTypeImporter(ImportContext importContext, String documentLibraryName, bool clearFirst) : base("content types", importContext) { this.Log.Info(string.Format("Mapping the object types to content types and columns to the site and the library [{0}]...", documentLibraryName)); // TODO: Make these two configurable this.TypeGroup = TYPE_GROUP; this.FieldGroup = FIELD_GROUP; using (ObjectPool <SharePointSession> .Ref sessionRef = this.ImportContext.SessionFactory.GetSession()) { SharePointSession session = sessionRef.Target; ClientContext clientContext = session.ClientContext; List documentLibrary = clientContext.Web.Lists.GetByTitle(documentLibraryName); if (clearFirst) { try { Log.Warn("Cleaning out document library content types..."); CleanContentTypes(sessionRef.Target, documentLibrary.ContentTypes); Log.Warn("Cleaning out document library fields..."); CleanFields(sessionRef.Target, documentLibrary.Fields); Log.Warn("Cleaning out site content types..."); CleanContentTypes(sessionRef.Target, clientContext.Web.ContentTypes); Log.Warn("Cleaning out site fields..."); CleanFields(sessionRef.Target, clientContext.Web.Fields); Log.Warn("Fields and content types cleared!"); } catch (Exception e) { Log.Warn("Tried to remove the existing content types, but failed", e); } } ContentTypeCollection contentTypeCollection = clientContext.Web.ContentTypes; clientContext.Load(contentTypeCollection, c => c.Include(t => t.Id, t => t.Name, t => t.Group, t => t.Parent, t => t.FieldLinks)); clientContext.Load(documentLibrary.ContentTypes, c => c.Include(t => t.Id, t => t.Name, t => t.Parent)); clientContext.Load(clientContext.Web, w => w.Fields, w => w.AvailableFields); session.ExecuteQuery(); // First we gather up whatever's already there Dictionary <string, ImportedContentType> siteContentTypes = new Dictionary <string, ImportedContentType>(); Dictionary <string, ImportedContentType> libraryContentTypes = new Dictionary <string, ImportedContentType>(); Dictionary <string, ImportedContentType> contentTypesById = new Dictionary <string, ImportedContentType>(); Dictionary <string, ContentType> allTypes = new Dictionary <string, ContentType>(); foreach (ContentType type in contentTypeCollection) { ImportedContentType ct = new ImportedContentType(this, type); siteContentTypes[type.Name] = ct; contentTypesById[type.Id.StringValue] = ct; } Dictionary <string, Field> existingFields = new Dictionary <string, Field>(); foreach (Field f in clientContext.Web.Fields) { existingFields[f.StaticName] = f; } HashSet <string> documentLibraryTypes = new HashSet <string>(); foreach (ContentType ct in documentLibrary.ContentTypes) { documentLibraryTypes.Add(ct.Name); ImportedContentType ict = new ImportedContentType(this, ct); libraryContentTypes[ct.Name] = ict; contentTypesById[ct.Id.StringValue] = ict; } // Now we go over the XML declarations HashSet <string> newTypes = new HashSet <string>(); XElement types = XElement.Load(this.ImportContext.LoadIndex("types")); XNamespace ns = types.GetDefaultNamespace(); foreach (XElement type in types.Elements(ns + "type")) { string typeName = (string)type.Element(ns + "name"); ImportedContentType finalType = null; HashSet <string> linkNames = new HashSet <string>(); bool skipInherited = true; bool patchFields = false; bool versionableType = false; bool containerType = false; if (siteContentTypes.ContainsKey(typeName)) { finalType = siteContentTypes[typeName]; foreach (FieldLink link in finalType.Type.FieldLinks) { linkNames.Add(link.Name); } if (typeName == "dm_sysobject" || typeName == "dm_folder") { patchFields = true; versionableType = (typeName == "dm_sysobject"); containerType = (typeName == "dm_folder"); } } else { // New type...create it ImportedContentType superType = null; XElement superTypeElement = type.Element(ns + "superType"); if ((superTypeElement != null) && (typeName != "dm_folder")) { string stName = (string)superTypeElement; if (siteContentTypes.ContainsKey(stName)) { superType = siteContentTypes[stName]; } } if (superType == null) { switch (typeName) { case "dm_sysobject": superType = siteContentTypes["Document"]; // skipInherited = false; patchFields = true; versionableType = true; break; case "dm_folder": superType = siteContentTypes["Folder"]; // skipInherited = false; patchFields = true; containerType = true; break; default: // This isn't a type we're intereseted in, so we skip it continue; } } Log.Info(string.Format("Creating content type {0} (descended from [{1}]])", typeName, superType.Name)); ContentTypeCreationInformation ctInfo = new ContentTypeCreationInformation(); ctInfo.Description = string.Format("Documentum Type {0}", typeName); ctInfo.Name = typeName; ctInfo.ParentContentType = (superType != null ? superType.Type : null); ctInfo.Group = this.TypeGroup; ContentType contentTypeObj = contentTypeCollection.Add(ctInfo); clientContext.Load(contentTypeObj, t => t.Id); session.ExecuteQuery(); finalType = new ImportedContentType(this, contentTypeObj, superType.Id); siteContentTypes[typeName] = finalType; contentTypesById[finalType.Id.StringValue] = finalType; } // Now we link the type to its fields, as needed int updateCount = 0; XElement attributeContainer = type.Element(ns + "attributes"); if (patchFields) { XElement versionAtt = new XElement(ns + "attribute"); versionAtt.SetAttributeValue("length", "32"); versionAtt.SetAttributeValue("repeating", "false"); versionAtt.SetAttributeValue("inherited", "false"); versionAtt.SetAttributeValue("sourceName", "version"); versionAtt.SetAttributeValue("name", "cmf:version"); versionAtt.SetAttributeValue("dataType", "STRING"); attributeContainer.AddFirst(versionAtt); versionAtt = new XElement(ns + "attribute"); versionAtt.SetAttributeValue("length", "400"); versionAtt.SetAttributeValue("repeating", "false"); versionAtt.SetAttributeValue("inherited", "false"); versionAtt.SetAttributeValue("sourceName", "title"); versionAtt.SetAttributeValue("name", "cmis:description"); versionAtt.SetAttributeValue("dataType", "STRING"); attributeContainer.AddFirst(versionAtt); versionAtt = new XElement(ns + "attribute"); versionAtt.SetAttributeValue("length", "128"); versionAtt.SetAttributeValue("repeating", "false"); versionAtt.SetAttributeValue("inherited", "false"); versionAtt.SetAttributeValue("sourceName", "author_name"); versionAtt.SetAttributeValue("name", "shpt:authorName"); versionAtt.SetAttributeValue("dataType", "STRING"); attributeContainer.AddFirst(versionAtt); versionAtt = new XElement(ns + "attribute"); versionAtt.SetAttributeValue("length", "0"); versionAtt.SetAttributeValue("repeating", "false"); versionAtt.SetAttributeValue("inherited", "false"); versionAtt.SetAttributeValue("sourceName", "author"); versionAtt.SetAttributeValue("name", "shpt:author"); versionAtt.SetAttributeValue("dataType", "USER"); attributeContainer.AddFirst(versionAtt); versionAtt = new XElement(ns + "attribute"); versionAtt.SetAttributeValue("length", "128"); versionAtt.SetAttributeValue("repeating", "false"); versionAtt.SetAttributeValue("inherited", "false"); versionAtt.SetAttributeValue("sourceName", "editor_name"); versionAtt.SetAttributeValue("name", "shpt:editorName"); versionAtt.SetAttributeValue("dataType", "STRING"); attributeContainer.AddFirst(versionAtt); versionAtt = new XElement(ns + "attribute"); versionAtt.SetAttributeValue("length", "0"); versionAtt.SetAttributeValue("repeating", "false"); versionAtt.SetAttributeValue("inherited", "false"); versionAtt.SetAttributeValue("sourceName", "editor"); versionAtt.SetAttributeValue("name", "shpt:editor"); versionAtt.SetAttributeValue("dataType", "USER"); attributeContainer.AddFirst(versionAtt); versionAtt = new XElement(ns + "attribute"); versionAtt.SetAttributeValue("length", "16"); versionAtt.SetAttributeValue("repeating", "false"); versionAtt.SetAttributeValue("inherited", "false"); versionAtt.SetAttributeValue("sourceName", "object_id"); versionAtt.SetAttributeValue("name", "cmis:objectId"); versionAtt.SetAttributeValue("dataType", "STRING"); attributeContainer.AddFirst(versionAtt); versionAtt = new XElement(ns + "attribute"); versionAtt.SetAttributeValue("length", "256"); versionAtt.SetAttributeValue("repeating", "false"); versionAtt.SetAttributeValue("inherited", "false"); versionAtt.SetAttributeValue("sourceName", "location"); versionAtt.SetAttributeValue("name", "cmf:location"); versionAtt.SetAttributeValue("dataType", "STRING"); attributeContainer.AddFirst(versionAtt); versionAtt = new XElement(ns + "attribute"); versionAtt.SetAttributeValue("length", "256"); versionAtt.SetAttributeValue("repeating", "false"); versionAtt.SetAttributeValue("inherited", "false"); versionAtt.SetAttributeValue("sourceName", "path"); versionAtt.SetAttributeValue("name", "cmis:path"); versionAtt.SetAttributeValue("dataType", "STRING"); attributeContainer.AddFirst(versionAtt); versionAtt = new XElement(ns + "attribute"); versionAtt.SetAttributeValue("length", "256"); versionAtt.SetAttributeValue("repeating", "false"); versionAtt.SetAttributeValue("inherited", "false"); versionAtt.SetAttributeValue("sourceName", "name"); versionAtt.SetAttributeValue("name", "cmis:name"); versionAtt.SetAttributeValue("dataType", "STRING"); attributeContainer.AddFirst(versionAtt); versionAtt = new XElement(ns + "attribute"); versionAtt.SetAttributeValue("length", "1024"); versionAtt.SetAttributeValue("repeating", "true"); versionAtt.SetAttributeValue("inherited", "false"); versionAtt.SetAttributeValue("sourceName", "keywords"); versionAtt.SetAttributeValue("name", "dctm:keywords"); versionAtt.SetAttributeValue("dataType", "STRING"); attributeContainer.AddFirst(versionAtt); versionAtt = new XElement(ns + "attribute"); versionAtt.SetAttributeValue("length", "16"); versionAtt.SetAttributeValue("repeating", "false"); versionAtt.SetAttributeValue("inherited", "false"); versionAtt.SetAttributeValue("sourceName", "acl_id"); versionAtt.SetAttributeValue("name", "dctm:acl_id"); versionAtt.SetAttributeValue("dataType", "STRING"); attributeContainer.AddFirst(versionAtt); if (versionableType) { versionAtt = new XElement(ns + "attribute"); versionAtt.SetAttributeValue("length", "16"); versionAtt.SetAttributeValue("repeating", "false"); versionAtt.SetAttributeValue("inherited", "false"); versionAtt.SetAttributeValue("sourceName", "history_id"); versionAtt.SetAttributeValue("name", "cmis:versionSeriesId"); versionAtt.SetAttributeValue("dataType", "STRING"); attributeContainer.AddFirst(versionAtt); versionAtt = new XElement(ns + "attribute"); versionAtt.SetAttributeValue("length", "16"); versionAtt.SetAttributeValue("repeating", "false"); versionAtt.SetAttributeValue("inherited", "false"); versionAtt.SetAttributeValue("sourceName", "antecedent_id"); versionAtt.SetAttributeValue("name", "cmf:version_antecedent_id"); versionAtt.SetAttributeValue("dataType", "STRING"); attributeContainer.AddFirst(versionAtt); versionAtt = new XElement(ns + "attribute"); versionAtt.SetAttributeValue("length", "0"); versionAtt.SetAttributeValue("repeating", "false"); versionAtt.SetAttributeValue("inherited", "false"); versionAtt.SetAttributeValue("sourceName", "current"); versionAtt.SetAttributeValue("name", "cmis:isLatestVersion"); versionAtt.SetAttributeValue("dataType", "BOOLEAN"); attributeContainer.AddFirst(versionAtt); } } foreach (XElement att in attributeContainer.Elements(ns + "attribute")) { // The attribute is either not inherited or its inheritance is ignored, so add it to the content type's declaration string attName = att.Attribute("name").Value; string attSourceName = att.Attribute("sourceName").Value; string finalName = string.Format("dctm_{0}", attSourceName); // Special case for folder attributes inherited from dm_sysobject bool inherited = XmlConvert.ToBoolean(att.Attribute("inherited").Value) && (typeName != "dm_folder"); bool repeating = XmlConvert.ToBoolean(att.Attribute("repeating").Value); // If this is an inherited attribute, we won't add it because we're not interested in it if (inherited && skipInherited) { continue; } ImportedContentTypeField finalField = null; if (linkNames.Contains(finalName) || (inherited && skipInherited)) { // Existing or inherited link... finalField = new ImportedContentTypeField(existingFields[finalName], attName, finalName, attSourceName, repeating); } else { FieldLinkCreationInformation fieldLink = new FieldLinkCreationInformation(); if (existingFields.ContainsKey(finalName)) { finalField = new ImportedContentTypeField(existingFields[finalName], attName, finalName, attSourceName, repeating); fieldLink.Field = finalField.Field; } else { Log.Info(string.Format("Creating field {0} (first declared by {1})", finalName, typeName)); FieldType attType = Tools.DecodeFieldType(att.Attribute("dataType").Value); if (repeating) { // Default repeating fields to strings, since they'll be concatenated attType = FieldType.Note; } int length = XmlConvert.ToInt32(att.Attribute("length").Value); if (length > 255) { attType = FieldType.Note; } Guid guid = Guid.NewGuid(); string fieldXml = string.Format("<Field DisplayName='{0}' Name='{1}' ID='{2}' Group='{3}' Type='{4}' />", finalName, finalName, guid.ToString(), this.FieldGroup, attType); fieldLink.Field = clientContext.Web.Fields.AddFieldAsXml(fieldXml, false, AddFieldOptions.AddFieldInternalNameHint); clientContext.Load(fieldLink.Field, f => f.Id, f => f.FieldTypeKind, f => f.StaticName, f => f.Group); existingFields[finalName] = fieldLink.Field; finalField = new ImportedContentTypeField(existingFields[finalName], attName, finalName, attSourceName, repeating); } finalType.Type.FieldLinks.Add(fieldLink); linkNames.Add(finalName); updateCount++; } finalType.AddField(finalField); } if (updateCount > 0) { finalType.Type.Update(true); session.ExecuteQuery(); } newTypes.Add(typeName); } // Now, make sure this type exists in the document library List <ContentType> newContentTypes = new List <ContentType>(); foreach (string typeName in newTypes) { if (!documentLibraryTypes.Contains(typeName)) { ContentType ct = documentLibrary.ContentTypes.AddExistingContentType(siteContentTypes[typeName].Type); newContentTypes.Add(ct); clientContext.Load(ct); clientContext.Load(ct, c => c.Parent); } } if (newContentTypes.Count > 0) { session.ExecuteQuery(); foreach (ContentType ct in newContentTypes) { ImportedContentType newCt = new ImportedContentType(this, ct); contentTypesById[ct.Id.StringValue] = newCt; libraryContentTypes[ct.Name] = newCt; } } this.SiteContentTypes = siteContentTypes; this.LibraryContentTypes = libraryContentTypes; this.ContentTypesById = contentTypesById; } }
/// <summary> /// Creates a new ContentType. /// </summary> /// <param name="sourceContentTypeCollection">ContentTypeCollection of the source SharePoint</param> /// <param name="targetContentTypeCollection">ContentTypeCollection of the target SharePoint</param> /// <param name="sourceContentType">ContentType from source with information for the new one</param> private void CreateContentType(ContentTypeCollection sourceContentTypeCollection, ContentTypeCollection targetContentTypeCollection, Microsoft.SharePoint.Client.ContentType sourceContentType) { ContentTypeCreationInformation creationObject = new ContentTypeCreationInformation(); creationObject.Description = sourceContentType.Description; creationObject.Group = sourceContentType.Group; creationObject.Name = sourceContentType.Name; //// creationObject.ParentContentType = this.AddParent(sourceContentTypeCollection, targetContentTypeCollection, sourceContentType.Parent); ContentType targetContentType = targetContentTypeCollection.Add(creationObject); try { targetContentType.DisplayFormTemplateName = sourceContentType.DisplayFormTemplateName; } catch (PropertyOrFieldNotInitializedException) { } try { targetContentType.DisplayFormUrl = sourceContentType.DisplayFormUrl; } catch (PropertyOrFieldNotInitializedException) { } try { targetContentType.DocumentTemplate = sourceContentType.DocumentTemplate; } catch (PropertyOrFieldNotInitializedException) { } try { targetContentType.EditFormTemplateName = sourceContentType.EditFormTemplateName; } catch (PropertyOrFieldNotInitializedException) { } try { targetContentType.EditFormUrl = sourceContentType.EditFormUrl; } catch (PropertyOrFieldNotInitializedException) { } try { targetContentType.Hidden = sourceContentType.Hidden; } catch (PropertyOrFieldNotInitializedException) { } try { targetContentType.NewFormTemplateName = sourceContentType.NewFormTemplateName; } catch (PropertyOrFieldNotInitializedException) { } try { targetContentType.NewFormUrl = sourceContentType.NewFormUrl; } catch (PropertyOrFieldNotInitializedException) { } try { targetContentType.ReadOnly = sourceContentType.ReadOnly; } catch (PropertyOrFieldNotInitializedException) { } try { targetContentType.Sealed = sourceContentType.Sealed; } catch (PropertyOrFieldNotInitializedException) { } try { targetContentType.Tag = sourceContentType.Tag; } catch (PropertyOrFieldNotInitializedException) { } }
public static void CreateContentType(ClientContext context, ContentType parentContentType, string contentName, string description, string contentGroup, List <Field> fields) { //// Get the content type collection for the website ContentTypeCollection contentTypeColl = context.Web.ContentTypes; ContentType contentType = contentTypeColl.Where(a => a.Name == contentName).FirstOrDefault(); if (contentType == null) { //// Specifies properties that are used as parameters to initialize a new content type. ContentTypeCreationInformation contentTypeCreation = new ContentTypeCreationInformation(); contentTypeCreation.Name = contentName; contentTypeCreation.Description = contentName; contentTypeCreation.Group = contentGroup; contentTypeCreation.ParentContentType = parentContentType; //// Add the new content type to the collection contentType = contentTypeColl.Add(contentTypeCreation); } var siteFields = context.Web.Fields; context.Load(siteFields); context.Load(contentType); context.Load(contentType.Fields); context.ExecuteQuery(); var existingSiteFields = siteFields.ToList(); foreach (var field in fields) { var siteField = field; if (!contentType.Fields.Where(a => a.InternalName == field.InternalName).Any()) { var fieldSchema = XDocument.Parse(field.SchemaXml); string internalName = CreateUnqiueName(existingSiteFields, fieldSchema.Root.Attribute("StaticName").Value); string typeName = fieldSchema.Root.Attribute("Type").Value; string displayName = fieldSchema.Root.Attribute("DisplayName").Value; string required = fieldSchema.Root.Attribute("Required") != null?fieldSchema.Root.Attribute("Required").Value : ""; string enforceUniqueValues = fieldSchema.Root.Attribute("EnforceUniqueValues") != null?fieldSchema.Root.Attribute("EnforceUniqueValues").Value : ""; string showField = fieldSchema.Root.Attribute("ShowField") != null?fieldSchema.Root.Attribute("ShowField").Value : ""; string unlimitedLengthInDocumentLibrary = fieldSchema.Root.Attribute("UnlimitedLengthInDocumentLibrary") != null?fieldSchema.Root.Attribute("UnlimitedLengthInDocumentLibrary").Value : ""; string relationshipDeleteBehavior = fieldSchema.Root.Attribute("RelationshipDeleteBehavior") != null?fieldSchema.Root.Attribute("RelationshipDeleteBehavior").Value : ""; string staticName = internalName; string name = internalName; string sourceId = fieldSchema.Root.Attribute("SourceID") != null?fieldSchema.Root.Attribute("SourceID").Value : ""; string list = fieldSchema.Root.Attribute("List") != null?fieldSchema.Root.Attribute("List").Value : ""; var test = existingSiteFields.Where(a => a.InternalName.ToLower() == "email1").FirstOrDefault(); if (!existingSiteFields.Where(a => a.InternalName == field.InternalName).Any()) { siteField = CreateSiteColumn(context, typeName, displayName, required, enforceUniqueValues, showField, unlimitedLengthInDocumentLibrary, relationshipDeleteBehavior, staticName, name, sourceId, list); } contentType.FieldLinks.Add(new FieldLinkCreationInformation() { Field = siteField }); contentType.Update(true); context.Load(contentType); context.Load(contentType.FieldLinks); context.ExecuteQuery(); } } }