private void CreateField(ShField field, FieldCollection fields) { // code to handle lookup fields. does not work if the list has not been created... if (field.Type == "Lookup" || field.Type == "LookupMulti") { var web = ClientContext.Web; if (!web.IsObjectPropertyInstantiated("Lists")) { ClientContext.Load(web, x => x.Lists); ClientContext.ExecuteQuery(); } var listTitle = field.List; var list = web.Lists.GetByTitle(listTitle); try { ClientContext.Load(list, x => x.Id); ClientContext.ExecuteQuery(); field.List = list.Id.ToString(); } catch (Exception) { Log.Info("Lookup field " + field.DisplayName + " id:" + field.ID + " cannot be created since the list " + listTitle + " has not been created. Please rund content type creation again after setting up the site hierarchy"); return; } } var fieldXml = field.GetFieldAsXml(); Field newField = fields.AddFieldAsXml(fieldXml, true, AddFieldOptions.AddFieldInternalNameHint); ClientContext.Load(newField); ClientContext.ExecuteQuery(); }
/// <summary> /// We don't want to update all properties of an existing field. For now, only the Hidden property is being updated. /// </summary> /// <param name="configField"></param> /// <param name="existingField"></param> private void UpdateExistingField(ShField configField, Field existingField) { ClientContext.Load(existingField, i => i.DefaultValue, i => i.Title, i => i.Group, i => i.Hidden); ClientContext.ExecuteQuery(); var fieldUpdated = false; if (configField.Hidden != existingField.Hidden) { existingField.Hidden = configField.Hidden; fieldUpdated = true; } if (configField.DisplayName != existingField.Title) { existingField.Title = configField.DisplayName; fieldUpdated = true; } if (configField.Group != existingField.Group) { existingField.Group = configField.Group; fieldUpdated = true; } if (configField.Default != existingField.DefaultValue && !string.IsNullOrEmpty(configField.Default)) { existingField.DefaultValue = configField.Default; fieldUpdated = true; } if (fieldUpdated) { Log.DebugFormat("Updating field {0} with changes", configField.DisplayName); existingField.UpdateAndPushChanges(true); ClientContext.ExecuteQueryRetry(); } }
private void CreateField(ShField field, FieldCollection fields) { var fieldXml = field.GetFieldAsXml(); Field newField = fields.AddFieldAsXml(fieldXml, true, AddFieldOptions.AddFieldInternalNameHint); ClientContext.Load(newField); ClientContext.ExecuteQuery(); }
private Guid GetTermSetId(ShField field) { if (field.TermSetId != Guid.Empty) { return(field.TermSetId); } if (string.IsNullOrEmpty(field.TermSetName)) { throw new Exception("Invalid taxonomy configuration settings for field " + field.DisplayName); } var manager = new TaxonomyManager(); return(manager.GetTermSetId(ClientContext, field.TermSetName)); }
private void UpdateTaxonomyField(ShField configField, Field existingField) { try { var existingTaxField = ClientContext.CastTo <TaxonomyField>(existingField); if (existingTaxField != null) { ClientContext.Load(existingTaxField, i => i.TermSetId, i => i.Title, i => i.Group, i => i.Hidden); ClientContext.ExecuteQuery(); var fieldUpdated = false; if (!configField.TermSetId.Equals(existingTaxField.TermSetId)) { existingTaxField.TermSetId = configField.TermSetId; fieldUpdated = true; } if (configField.Hidden != existingField.Hidden) { existingField.Hidden = configField.Hidden; fieldUpdated = true; } if (configField.DisplayName != existingTaxField.Title) { existingTaxField.Title = configField.DisplayName; fieldUpdated = true; } if (configField.Group != existingTaxField.Group) { existingTaxField.Group = configField.Group; fieldUpdated = true; } if (fieldUpdated) { Log.DebugFormat("Updating taxonomy field '{0}' with changes", configField.DisplayName); existingTaxField.UpdateAndPushChanges(true); ClientContext.ExecuteQueryRetry(); } } else { Log.ErrorFormat("Could not cast field '{0}' to TaxonomyField", configField.DisplayName); } } catch { Log.ErrorFormat("Could not cast field '{0}' to TaxonomyField"); } }
private void CreateTaxonomyField(ShField field, FieldCollection fields) { Log.Debug("Attempting to create taxonomy field " + field.DisplayName); var fieldSchema = field.GetFieldAsXml(); var newField = fields.AddFieldAsXml(fieldSchema, false, AddFieldOptions.AddFieldInternalNameHint); ClientContext.Load(newField); ClientContext.ExecuteQuery(); var termSetId = GetTermSetId(field); var newTaxonomyField = ClientContext.CastTo <TaxonomyField>(newField); newTaxonomyField.SspId = field.SspId; newTaxonomyField.TermSetId = termSetId; newTaxonomyField.TargetTemplate = String.Empty; newTaxonomyField.AnchorId = Guid.Empty; newTaxonomyField.CreateValuesInEditForm = field.OpenTermSet; newTaxonomyField.Open = field.OpenTermSet; newTaxonomyField.Update(); ClientContext.ExecuteQuery(); }