private static void CreateFields(ClientContext ctx, ContentType orderCT) { #region Customer FieldCreationInformation customer = new FieldCreationInformation(FieldType.Lookup); customer.DisplayName = "Customer"; customer.InternalName = "Customer"; customer.Group = "ODA1"; customer.Id = Constants.GUID.OrderCT.CUSTOMER.ToGuid(); ctx.Web.AddFieldToContentType(orderCT, ctx.Web.CreateField<FieldUrl>(customer, false)); #endregion #region Product TaxonomyFieldCreationInformation product = new TaxonomyFieldCreationInformation(); product.DisplayName = "Product"; product.InternalName = "Product_2"; product.Group = "ODA1"; product.TaxonomyItem = GetProductTermSet(ctx); product.Id = Constants.GUID.OrderCT.PRODUCT.ToGuid(); var meh = ctx.Web.CreateTaxonomyField(product); ctx.ExecuteQuery(); ctx.Web.WireUpTaxonomyField(meh, product.TaxonomyItem as TermSet); ctx.Web.AddFieldToContentType(orderCT, meh ); #endregion #region Price FieldCreationInformation price = new FieldCreationInformation(FieldType.Currency); price.DisplayName = "Price"; price.InternalName = "Price"; price.Group = "ODA1"; price.Id = Constants.GUID.OrderCT.PRICE.ToGuid(); FieldUrl addedPrice = ctx.Web.CreateField<FieldUrl>(price, false); ctx.Web.AddFieldToContentType(orderCT, addedPrice); #endregion }
protected override void ExecuteCmdlet() { Field field = null; var termSet = ClientContext.Site.GetTaxonomyItemByPath(TermSetPath, TermPathDelimiter); Guid id = Id.Id; if (id == Guid.Empty) { id = Guid.NewGuid(); } TaxonomyFieldCreationInformation fieldCI = new TaxonomyFieldCreationInformation() { Id = id, InternalName = InternalName, DisplayName = DisplayName, Group = Group, TaxonomyItem = termSet, MultiValue = MultiValue, Required = Required, AddToDefaultView = AddToDefaultView }; if (List != null) { var list = this.SelectedWeb.GetList(List); field = list.CreateTaxonomyField(fieldCI); } else { field = this.SelectedWeb.CreateTaxonomyField(fieldCI); } WriteObject(field); }
/// <summary> /// Used to create a custom document library and Contoso Content type /// </summary> /// <param name="ctx">The authenticated ClientContext</param> /// <param name="library">The Library to create</param> public void CreateContosoDocumentLibrary(ClientContext ctx, Library library) { //Check the fields if (!ctx.Web.FieldExistsById(FLD_CLASSIFICATION_ID)) { TermStore termStore = GetDefaultTermStore(ctx.Web); if (termStore == null) { throw new NullReferenceException("The default term store is not available."); } // get the term group and term set TermGroup termGroup = termStore.Groups.GetByName(TAXONOMY_GROUP); TermSet termSet = termGroup.TermSets.GetByName(TAXONOMY_TERMSET_CLASSIFICATION_NAME); ctx.Load(termStore); ctx.Load(termSet); ctx.ExecuteQuery(); TaxonomyFieldCreationInformation fldCreate = new TaxonomyFieldCreationInformation() { Id = FLD_CLASSIFICATION_ID, InternalName = FLD_CLASSIFICATION_INTERNAL_NAME, DisplayName = FLD_CLASSIFICATION_DISPLAY_NAME, Group = FIELDS_GROUP_NAME, TaxonomyItem = termSet, }; ctx.Web.CreateTaxonomyField(fldCreate); } //check the content type if (!ctx.Web.ContentTypeExistsById(CONTOSODOCUMENT_CT_ID)) { ctx.Web.CreateContentType(CONTOSODOCUMENT_CT_NAME, CT_DESC, CONTOSODOCUMENT_CT_ID, CT_GROUP); } //associate fields to content types if (!ctx.Web.FieldExistsByNameInContentType(CONTOSODOCUMENT_CT_NAME, FLD_CLASSIFICATION_INTERNAL_NAME)) { ctx.Web.AddFieldToContentTypeById(CONTOSODOCUMENT_CT_ID, FLD_CLASSIFICATION_ID.ToString(), false); } CreateLibrary(ctx, library, CONTOSODOCUMENT_CT_ID); }
protected void btnScenario2_Click(object sender, EventArgs e) { // Taxonomy field to host web - Note that this requires that group and taxonomy set exists when the code is executed. var spContext = SharePointContextProvider.Current.GetSharePointContext(Context); // Notice that you can assign the guid if needed - in here we randomize it for demo purposes var taxFieldId = Guid.NewGuid(); using (var ctx = spContext.CreateUserClientContextForSPHost()) { var groupName = drpGroups.SelectedItem.Text; var termSetName = drpTermSets.SelectedItem.Text; var taxFieldName = "ContosoTaxonomySample"; if (!ctx.Web.FieldExistsByName(taxFieldName)) { // Get access to the right term set TaxonomySession taxonomySession = TaxonomySession.GetTaxonomySession(ctx.Web.Context); TermStore termStore = taxonomySession.GetDefaultSiteCollectionTermStore(); TermGroup termGroup = termStore.Groups.GetByName(groupName); TermSet termSet = termGroup.TermSets.GetByName(termSetName); ctx.Web.Context.Load(termStore); ctx.Web.Context.Load(termSet); ctx.Web.Context.ExecuteQueryRetry(); TaxonomyFieldCreationInformation fieldCI = new TaxonomyFieldCreationInformation() { Id = taxFieldId, InternalName = taxFieldName, DisplayName = "Contoso Taxonomy Sample", Group = "Contoso Fields", TaxonomyItem = termSet, }; ctx.Web.CreateTaxonomyField(fieldCI); lblStatus2.Text = string.Format("Created new taxonomy field with name of 'Contoso Taxonomy Sample'. Move to <a href='{0}'>host web</a> and test the functionality.", spContext.SPHostUrl.ToString()); } else { ctx.Web.WireUpTaxonomyField(taxFieldId, groupName, termSetName); lblStatus2.Text = "Taxonomy field with planned Id already existed"; } } }
/// <summary> /// Used to create a custom document library and Contoso Content type /// </summary> /// <param name="ctx">The client context that has be authenticated</param> /// <param name="library">The Library to create</param> public void CreateContosoDocumentLibrary(ClientContext ctx, Library library) { //Check the fields if (!ctx.Web.FieldExistsById(FLD_CLASSIFICATION_ID)){ // Get access to the right term set TaxonomySession taxonomySession = TaxonomySession.GetTaxonomySession(ctx.Web.Context); TermStore termStore = taxonomySession.GetDefaultSiteCollectionTermStore(); TermGroup termGroup = termStore.Groups.GetByName(TAXONOMY_GROUP); TermSet termSet = termGroup.TermSets.GetByName(TAXONOMY_TERMSET_CLASSIFICATION_NAME); ctx.Web.Context.Load(termStore); ctx.Web.Context.Load(termSet); ctx.Web.Context.ExecuteQueryRetry(); TaxonomyFieldCreationInformation fieldCI = new TaxonomyFieldCreationInformation() { Id = FLD_CLASSIFICATION_ID, InternalName = FLD_CLASSIFICATION_INTERNAL_NAME, DisplayName = FLD_CLASSIFICATION_DISPLAY_NAME, Group = FIELDS_GROUP_NAME, TaxonomyItem = termSet }; ctx.Web.CreateTaxonomyField(fieldCI); } //check the content type if (!ctx.Web.ContentTypeExistsById(CONTOSODOCUMENT_CT_ID)){ ctx.Web.CreateContentType(CONTOSODOCUMENT_CT_NAME, CT_DESC, CONTOSODOCUMENT_CT_ID, CT_GROUP); } //associate fields to content types if (!ctx.Web.FieldExistsByNameInContentType(CONTOSODOCUMENT_CT_NAME, FLD_CLASSIFICATION_INTERNAL_NAME)){ ctx.Web.AddFieldToContentTypeByName(CONTOSODOCUMENT_CT_NAME, FLD_CLASSIFICATION_ID); } CreateLibrary(ctx, library, CONTOSODOCUMENT_CT_ID); }
/// <summary> /// Can be used to create taxonomy field remotely to web. /// </summary> /// <param name="web">Site to be processed - can be root web or sub site</param> /// <param name="fieldCreationInformation">Creation Information of the field</param> /// <returns>New taxonomy field</returns> public static Field CreateTaxonomyField(this Web web, TaxonomyFieldCreationInformation fieldCreationInformation) { fieldCreationInformation.InternalName.ValidateNotNullOrEmpty("internalName"); fieldCreationInformation.DisplayName.ValidateNotNullOrEmpty("displayName"); fieldCreationInformation.TaxonomyItem.ValidateNotNullOrEmpty("taxonomyItem"); CleanupTaxonomyHiddenField(web, web.Fields, fieldCreationInformation); if (fieldCreationInformation.Id == Guid.Empty) { fieldCreationInformation.Id = Guid.NewGuid(); } var showFieldAttribute = new KeyValuePair<string, string>(); if (fieldCreationInformation.AdditionalAttributes != null) { showFieldAttribute = fieldCreationInformation.AdditionalAttributes.FirstOrDefault(a => a.Key == "ShowField"); } if (showFieldAttribute.Key == null) { if (fieldCreationInformation.AdditionalAttributes == null) { fieldCreationInformation.AdditionalAttributes = new List<KeyValuePair<string, string>>(); } ((List<KeyValuePair<string, string>>)fieldCreationInformation.AdditionalAttributes).Add(new KeyValuePair<string, string>("ShowField", "Term1033")); } var _field = web.CreateField(fieldCreationInformation); WireUpTaxonomyFieldInternal(_field, fieldCreationInformation.TaxonomyItem, fieldCreationInformation.MultiValue); _field.Update(); web.Context.ExecuteQueryRetry(); return _field; }
protected override void ExecuteCmdlet() { TaxonomyItem taxItem = null; Field field; if (ParameterSetName == "Path") { taxItem = ClientContext.Site.GetTaxonomyItemByPath(TermSetPath, TermPathDelimiter); } else { var taxSession = ClientContext.Site.GetTaxonomySession(); var termStore = taxSession.GetDefaultKeywordsTermStore(); try { taxItem = termStore.GetTermSet(TaxonomyItemId.Id); } catch { try { taxItem = termStore.GetTerm(TaxonomyItemId.Id); } catch { throw new Exception(string.Format("Taxonomy Item with Id {0} not found", TaxonomyItemId.Id)); } } taxItem.EnsureProperty(t => t.Id); } Guid id = Id.Id; if (id == Guid.Empty) { id = Guid.NewGuid(); } TaxonomyFieldCreationInformation fieldCI = new TaxonomyFieldCreationInformation() { Id = id, InternalName = InternalName, DisplayName = DisplayName, Group = Group, TaxonomyItem = taxItem, MultiValue = MultiValue, Required = Required, AddToDefaultView = AddToDefaultView }; if (List != null) { var list = List.GetList(SelectedWeb); field = list.CreateTaxonomyField(fieldCI); } else { field = SelectedWeb.CreateTaxonomyField(fieldCI); } WriteObject(field); }
public static Field CreateTaxonomyField(this List list, Guid id, string internalName, string displayName, string group, Term anchorTerm, bool multiValue = false) { TaxonomyFieldCreationInformation fieldCI = new TaxonomyFieldCreationInformation() { Id = id, InternalName = internalName, DisplayName = displayName, Required = false, Group = group, MultiValue = multiValue, TaxonomyItem = anchorTerm }; return list.CreateTaxonomyField(fieldCI); }
public static Field CreateTaxonomyField(this List list, Guid id, string internalName, string displayName, string group, string mmsGroupName, string mmsTermSetName, bool multiValue = false) { id.ValidateNotNullOrEmpty("id"); internalName.ValidateNotNullOrEmpty("internalName"); displayName.ValidateNotNullOrEmpty("displayName"); mmsGroupName.ValidateNotNullOrEmpty("mmsGroupName"); mmsTermSetName.ValidateNotNullOrEmpty("mmsTermSetName"); var clientContext = list.Context as ClientContext; TermStore termStore = clientContext.Site.GetDefaultSiteCollectionTermStore(); if (termStore == null) throw new NullReferenceException("The default term store is not available."); // get the term group and term set TermGroup termGroup = termStore.Groups.GetByName(mmsGroupName); TermSet termSet = termGroup.TermSets.GetByName(mmsTermSetName); list.Context.Load(termStore); list.Context.Load(termSet); list.Context.ExecuteQuery(); TaxonomyFieldCreationInformation fieldCI = new TaxonomyFieldCreationInformation() { Id = id, InternalName = internalName, DisplayName = displayName, Required = false, Group = group, MultiValue = multiValue, TaxonomyItem = termSet }; return list.CreateTaxonomyField(fieldCI); }
public static Field CreateTaxonomyField(this Web web, Guid id, string internalName, string displayName, string group, TermSet termSet, bool multiValue = false) { TaxonomyFieldCreationInformation fieldCI = new TaxonomyFieldCreationInformation() { Id = id, InternalName = internalName, DisplayName = displayName, Group = group, TaxonomyItem = termSet, MultiValue = multiValue }; return web.CreateTaxonomyField(fieldCI); }
public static Field CreateTaxonomyField(this Web web, Guid id, string internalName, string displayName, string group, string mmsGroupName, string mmsTermSetName, bool multiValue = false) { id.ValidateNotNullOrEmpty("id"); internalName.ValidateNotNullOrEmpty("internalName"); displayName.ValidateNotNullOrEmpty("displayName"); // Group can be emtpy mmsGroupName.ValidateNotNullOrEmpty("mmsGroupName"); mmsTermSetName.ValidateNotNullOrEmpty("mmsTermSetName"); TermStore termStore = GetDefaultTermStore(web); if (termStore == null) throw new NullReferenceException("The default term store is not available."); // get the term group and term set TermGroup termGroup = termStore.Groups.GetByName(mmsGroupName); TermSet termSet = termGroup.TermSets.GetByName(mmsTermSetName); web.Context.Load(termStore); web.Context.Load(termSet); web.Context.ExecuteQuery(); TaxonomyFieldCreationInformation fieldCI = new TaxonomyFieldCreationInformation() { Id = id, InternalName = internalName, DisplayName = displayName, Group = group, TaxonomyItem = termSet, MultiValue = multiValue }; return web.CreateTaxonomyField(fieldCI); }
public void CreateTaxonomyFieldLinkedToTermTest() { using (var clientContext = TestCommon.CreateClientContext()) { // Retrieve Termset and Term TaxonomySession session = TaxonomySession.GetTaxonomySession(clientContext); var termSet = session.GetDefaultSiteCollectionTermStore().GetTermSet(_termSetId); var anchorTerm = termSet.GetTerm(_termId); clientContext.Load(termSet); clientContext.Load(anchorTerm); clientContext.ExecuteQuery(); // Retrieve List var list = clientContext.Web.Lists.GetById(_listId); clientContext.Load(list); clientContext.ExecuteQuery(); // Create field var fieldId = Guid.NewGuid(); var fieldName = "Test_" + DateTime.Now.ToFileTime(); TaxonomyFieldCreationInformation fieldCI = new TaxonomyFieldCreationInformation() { Id = fieldId, DisplayName = fieldName, InternalName = fieldName, Group = "Test Fields Group", TaxonomyItem = anchorTerm }; var field = list.CreateTaxonomyField(fieldCI); Assert.AreEqual(fieldId, field.Id, "Field IDs do not match."); Assert.AreEqual(fieldName, field.InternalName, "Field internal names do not match."); Assert.AreEqual("TaxonomyFieldType", field.TypeAsString, "Failed to create a TaxonomyField object."); } }
public void SetTaxonomyFieldValueTest() { var fieldName = "Test2_" + DateTime.Now.ToFileTime(); var fieldId = Guid.NewGuid(); using (var clientContext = TestCommon.CreateClientContext()) { // Retrieve list var list = clientContext.Web.Lists.GetById(_listId); clientContext.Load(list); clientContext.ExecuteQuery(); // Retrieve Termset TaxonomySession session = TaxonomySession.GetTaxonomySession(clientContext); var termSet = session.GetDefaultSiteCollectionTermStore().GetTermSet(_termSetId); clientContext.Load(termSet); clientContext.ExecuteQuery(); // Create taxonomyfield first TaxonomyFieldCreationInformation fieldCI = new TaxonomyFieldCreationInformation() { Id = fieldId, DisplayName = fieldName, InternalName = fieldName, Group = "Test Fields Group", TaxonomyItem = termSet }; var field = list.CreateTaxonomyField(fieldCI); // Create Item ListItemCreationInformation itemCi = new ListItemCreationInformation(); var item = list.AddItem(itemCi); item.Update(); clientContext.Load(item); clientContext.ExecuteQuery(); item.SetTaxonomyFieldValue(fieldId, _termName, _termId); clientContext.Load(item, i => i[fieldName]); clientContext.ExecuteQuery(); var value = item[fieldName] as TaxonomyFieldValue; Assert.AreEqual(_termId.ToString(), value.TermGuid, "Term not set correctly"); } }
public void CreateTaxonomyFieldMultiValueTest() { using (var clientContext = TestCommon.CreateClientContext()) { // Retrieve Termset TaxonomySession session = TaxonomySession.GetTaxonomySession(clientContext); var termSet = session.GetDefaultSiteCollectionTermStore().GetTermSet(_termSetId); clientContext.Load(termSet); clientContext.ExecuteQuery(); // Get Test TermSet var web = clientContext.Web; var fieldName = "Test_" + DateTime.Now.ToFileTime(); var fieldId = Guid.NewGuid(); TaxonomyFieldCreationInformation fieldCI = new TaxonomyFieldCreationInformation() { Id = fieldId, DisplayName = fieldName, InternalName = fieldName, Group = "Test Fields Group", TaxonomyItem = termSet, MultiValue = true }; var field = web.CreateTaxonomyField(fieldCI); Assert.AreEqual(fieldId, field.Id, "Field IDs do not match."); Assert.AreEqual(fieldName, field.InternalName, "Field internal names do not match."); Assert.AreEqual("TaxonomyFieldTypeMulti", field.TypeAsString, "Failed to create a TaxonomyField object."); } }
private static void CleanupTaxonomyHiddenField(Web web, FieldCollection fields, TaxonomyFieldCreationInformation fieldCreationInformation) { // if the Guid is empty then we'll have no issue if (fieldCreationInformation.Id != Guid.Empty) { FieldCollection _fields = fields; web.Context.Load(_fields, fc => fc.Include(f => f.Id, f => f.InternalName, f => f.Hidden)); web.Context.ExecuteQueryRetry(); var _field = _fields.FirstOrDefault(f => f.InternalName.Equals(fieldCreationInformation.InternalName)); // if the field does not exist we assume the possiblity that it was created earlier then deleted and the hidden field was left behind // if the field does exist then return and let the calling process exception out when attempting to create it // this does not appear to be an issue with lists, just site columns, but it doesnt hurt to check if (_field == null) { // The hidden field format is the id of the field itself with hyphens removed and the first character replaced // with a random character, so get everything to the right of the first character and remove hyphens var _hiddenField = fieldCreationInformation.Id.ToString().Replace("-", "").Substring(1); _field = _fields.FirstOrDefault(f => f.InternalName.EndsWith(_hiddenField)); if (_field != null) { if (_field.Hidden) { // just in case the field itself is hidden, make sure it is not because depending on the current CU hidden fields may not be deletable _field.Hidden = false; _field.Update(); } _field.DeleteObject(); web.Context.ExecuteQueryRetry(); } } } }
/// <summary> /// Can be used to create taxonomy field remotely in a list. /// </summary> /// <param name="list">List to be processed</param> /// <param name="fieldCreationInformation">Creation information of the field</param> /// <returns>New taxonomy field</returns> public static Field CreateTaxonomyField(this List list, TaxonomyFieldCreationInformation fieldCreationInformation) { fieldCreationInformation.InternalName.ValidateNotNullOrEmpty("internalName"); fieldCreationInformation.DisplayName.ValidateNotNullOrEmpty("displayName"); fieldCreationInformation.TaxonomyItem.ValidateNotNullOrEmpty("taxonomyItem"); if (fieldCreationInformation.Id == Guid.Empty) { fieldCreationInformation.Id = Guid.NewGuid(); } try { List<KeyValuePair<string, string>> additionalAttributes = new List<KeyValuePair<string, string>>(); additionalAttributes.Add(new KeyValuePair<string, string>("ShowField", "Term1033")); var _field = list.CreateField(fieldCreationInformation); WireUpTaxonomyFieldInternal(_field, fieldCreationInformation.TaxonomyItem, fieldCreationInformation.MultiValue); _field.Update(); list.Context.ExecuteQuery(); return _field; } catch (Exception) { ///If there is an exception the hidden field might be present FieldCollection _fields = list.Fields; list.Context.Load(_fields, fc => fc.Include(f => f.Id, f => f.InternalName)); list.Context.ExecuteQuery(); var _hiddenField = fieldCreationInformation.Id.ToString().Replace("-", ""); var _field = _fields.FirstOrDefault(f => f.InternalName == _hiddenField); if (_field != null) { _field.Hidden = false; // Cannot delete a hidden column _field.Update(); _field.DeleteObject(); list.Context.ExecuteQuery(); } throw; } }