private static void LocalizeContentTypeAndField(ClientContext cc, Web web) { ContentTypeCollection contentTypes = web.ContentTypes; ContentType myContentType = contentTypes.GetById("0x0101009189AB5D3D2647B580F011DA2F356FB2"); cc.Load(contentTypes); cc.Load(myContentType); cc.ExecuteQuery(); // Title of the content type myContentType.NameResource.SetValueForUICulture("en-US", "Contoso Document"); myContentType.NameResource.SetValueForUICulture("fi-FI", "Contoso Dokumentti"); myContentType.NameResource.SetValueForUICulture("fr-FR", "Contoso Document (FR)"); // Description of the content type myContentType.DescriptionResource.SetValueForUICulture("en-US", "This is the Contoso Document."); myContentType.DescriptionResource.SetValueForUICulture("fi-FI", "Tämä on geneerinen Contoso dokumentti."); myContentType.DescriptionResource.SetValueForUICulture("fr-FR", "French Contoso document."); myContentType.Update(true); cc.ExecuteQuery(); // Do localization also for the site column FieldCollection fields = web.Fields; Field fld = fields.GetByInternalNameOrTitle("ContosoString"); fld.TitleResource.SetValueForUICulture("en-US", "Contoso String"); fld.TitleResource.SetValueForUICulture("fi-FI", "Contoso Teksti"); fld.TitleResource.SetValueForUICulture("fr-FR", "Contoso French String"); // Description entry fld.DescriptionResource.SetValueForUICulture("en-US", "Used to store Contoso specific metadata."); fld.DescriptionResource.SetValueForUICulture("fi-FI", "Tää on niiku Contoso metadatalle."); fld.DescriptionResource.SetValueForUICulture("fr-FR", "French Description Goes here"); fld.UpdateAndPushChanges(true); cc.ExecuteQuery(); }
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 }
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()); } }
/// <summary> /// Set localized labels for content type /// </summary> /// <param name="list"></param> /// <param name="contentTypeId"></param> /// <param name="cultureName"></param> /// <param name="nameResource"></param> /// <param name="descriptionResource"></param> public static void SetLocalizationForContentType(this List list, string contentTypeId, string cultureName, string nameResource, string descriptionResource) { ContentTypeCollection contentTypes = list.ContentTypes; list.Context.Load(contentTypes); list.Context.ExecuteQuery(); ContentType contentType = contentTypes.GetById(contentTypeId); list.Context.ExecuteQuery(); contentType.SetLocalizationForContentType(cultureName, nameResource, descriptionResource); }
private void AddSiteColumnsToContentType(ShContentType configContentType) { Log.Debug("Attempting to add fields to content type " + configContentType.DisplayName); Web web = ClientContext.Web; ContentTypeCollection contentTypes = web.ContentTypes; ClientContext.Load(contentTypes); ClientContext.ExecuteQuery(); ContentType contentType = contentTypes.GetById(configContentType.ID); FieldCollection webFields = web.Fields; ClientContext.Load(contentType); ClientContext.Load(webFields); ClientContext.ExecuteQuery(); foreach (var fieldName in configContentType.Fields) { // Need to load content type fields every iteration because fields are added to the collection Field webField = webFields.GetByInternalNameOrTitle(fieldName); FieldLinkCollection contentTypeFields = contentType.FieldLinks; ClientContext.Load(contentTypeFields); ClientContext.Load(webField); ClientContext.ExecuteQuery(); var fieldLink = contentTypeFields.FirstOrDefault(existingFieldName => existingFieldName.Name == fieldName); if (fieldLink == null) { var link = new FieldLinkCreationInformation { Field = webField }; fieldLink = contentType.FieldLinks.Add(link); } fieldLink.Required = configContentType.RequiredFields.Contains(fieldName); if (configContentType.HiddenFields.Contains(fieldName)) { fieldLink.Hidden = true; fieldLink.Required = false; //var hiddenField = contentType.Fields.FirstOrDefault(ct => ct.InternalName == fieldName); //if (hiddenField != null) //{ // hiddenField.Required = false; // hiddenField.Update(); //} } contentType.Update(true); ClientContext.ExecuteQuery(); } }
private void AddSiteColumnsToContentType(ShContentType configContentType) { Log.Debug("Attempting to add fields to content type " + configContentType.DisplayName); Web web = ClientContext.Web; ContentTypeCollection contentTypes = web.ContentTypes; ClientContext.Load(contentTypes); ClientContext.ExecuteQuery(); ContentType contentType = contentTypes.GetById(configContentType.ID); FieldCollection webFields = web.Fields; ClientContext.Load(contentType); ClientContext.Load(webFields); ClientContext.ExecuteQuery(); foreach (var fieldNameToRemove in configContentType.RemovedFields) { try { RemoveFieldFromContentType(webFields, fieldNameToRemove, contentType); } catch (Exception ex) { Log.ErrorFormat("Field {0} could not be removed from the content type {1} with error {2}", fieldNameToRemove, configContentType.DisplayName, ex.Message); } } foreach (var fieldName in configContentType.Fields) { // We don't want to add removed fields to the content type if (configContentType.RemovedFields.Contains(fieldName)) { continue; } try { AddOrUpdateFieldOfContentType(configContentType, webFields, fieldName, contentType); } catch (Exception ex) { Log.ErrorFormat("Field {0} could not be added to the content type {1} with error {2}", fieldName, configContentType.DisplayName, ex.Message); Log.Info("Field " + fieldName + " does not exist. If this is a lookup field, run content type creation again after setting up site hierarchy"); } } }
private static void CreateContentTypes() { ContentTypeCollection ContentTypesCollection = web.ContentTypes; ContentType itemCT = ContentTypesCollection.GetById(AdministrativeStrings.ItemContentTypeId); //Create clients credits CT CreateSingleContentType(ContentTypeNames.Clients, ContentTypeDescriptions.Clients, AdministrativeNames.CoontentTypesGroup, itemCT, ContentTypesCollection); //Create companies credits CT CreateSingleContentType(ContentTypeNames.Companies, ContentTypeDescriptions.Companies, AdministrativeNames.CoontentTypesGroup, itemCT, ContentTypesCollection); }
private static void AddSiteColumnToContentType(ClientContext cc, Web web) { ContentTypeCollection contentTypes = web.ContentTypes; cc.Load(contentTypes); cc.ExecuteQuery(); ContentType myContentType = contentTypes.GetById("0x0101009189AB5D3D2647B580F011DA2F356FB2"); cc.Load(myContentType); cc.ExecuteQuery(); FieldCollection fields = web.Fields; Field fld = fields.GetByInternalNameOrTitle("ContosoString"); cc.Load(fields); cc.Load(fld); cc.ExecuteQuery(); FieldLinkCollection refFields = myContentType.FieldLinks; cc.Load(refFields); cc.ExecuteQuery(); foreach (var item in refFields) { if (item.Name == "ContosoString") { return; } } // ref does nt FieldLinkCreationInformation link = new FieldLinkCreationInformation(); link.Field = fld; myContentType.FieldLinks.Add(link); myContentType.Update(true); cc.ExecuteQuery(); }
private void AddSiteColumnsToContentType(GtContentType configContentType) { Web web = ClientContext.Web; ContentTypeCollection contentTypes = web.ContentTypes; ClientContext.Load(contentTypes); ClientContext.ExecuteQuery(); ContentType contentType = contentTypes.GetById(configContentType.ID); FieldCollection fields = web.Fields; ClientContext.Load(contentType); ClientContext.Load(fields); ClientContext.ExecuteQuery(); foreach (var fieldName in configContentType.Fields) { // Need to load content type fields every iteration because fields are added to the collection Field webField = fields.GetByInternalNameOrTitle(fieldName); FieldLinkCollection contentTypeFields = contentType.FieldLinks; ClientContext.Load(contentTypeFields); ClientContext.Load(webField); ClientContext.ExecuteQuery(); var fieldLink = contentTypeFields.FirstOrDefault(existingFieldName => existingFieldName.Name == fieldName); if (fieldLink == null) { var link = new FieldLinkCreationInformation { Field = webField }; fieldLink = contentType.FieldLinks.Add(link); } fieldLink.Required = configContentType.RequiredFields.Contains(fieldName); fieldLink.Hidden = configContentType.HiddenFields.Contains(fieldName); contentType.Update(true); ClientContext.ExecuteQuery(); } }
private SPOContentType Read(ContentTypeCollection contentTypes) { var ctx = SPOSiteContext.CurrentSiteContext.Context; ContentType contentType = null; if (_contentTypeId != null) { contentType = contentTypes.GetById(_contentTypeId); } else if (!string.IsNullOrEmpty(_name)) { ctx.Load(contentTypes); ctx.ExecuteQuery(); foreach (ContentType ct in contentTypes) { if (ct.Name.ToLower() == _name.ToLower()) { contentType = ct; break; } } } if (contentType != null) { SPOContentType.LoadContentType(ctx, contentType); if (contentType.ServerObjectIsNull.Value) { return(null); } return(new SPOContentType(contentType)); } return(null); }
private SPOContentType Read(ContentTypeCollection contentTypes) { var ctx = SPOSiteContext.CurrentSiteContext.Context; ContentType contentType = null; if (_contentTypeId != null) { contentType = contentTypes.GetById(_contentTypeId); } else if (!string.IsNullOrEmpty(_name)) { ctx.Load(contentTypes); ctx.ExecuteQuery(); foreach (ContentType ct in contentTypes) { if (ct.Name.ToLower() == _name.ToLower()) { contentType = ct; break; } } } if (contentType != null) { SPOContentType.LoadContentType(ctx, contentType); if (contentType.ServerObjectIsNull.Value) return null; return new SPOContentType(contentType); } return null; }