public void CreateTaxonomyFieldLinkedToTermSetTest() { using (var clientContext = TestCommon.CreateClientContext()) { // Retrieve Termset TaxonomySession session = TaxonomySession.GetTaxonomySession(clientContext); var termSet = session.GetDefaultSiteCollectionTermStore().GetTermSet(_termSetId); clientContext.Load(termSet); clientContext.ExecuteQueryRetry(); var list = clientContext.Web.Lists.GetById(_listId); clientContext.Load(list); clientContext.ExecuteQueryRetry(); 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 }; 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."); } }
protected override void ExecuteCmdlet() { TaxonomyItem taxItem; 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); } catch { try { taxItem = termStore.GetTerm(TaxonomyItemId); } catch { throw new Exception($"Taxonomy Item with Id {TaxonomyItemId} not found"); } } taxItem.EnsureProperty(t => t.Id); } if (Id == Guid.Empty) { Id = Guid.NewGuid(); } var 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(CurrentWeb); field = list.CreateTaxonomyField(fieldCI); } else { field = CurrentWeb.CreateTaxonomyField(fieldCI); } WriteObject(ClientContext.CastTo <TaxonomyField>(field)); }
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.ExecuteQueryRetry(); // Retrieve Termset TaxonomySession session = TaxonomySession.GetTaxonomySession(clientContext); var termSet = session.GetDefaultSiteCollectionTermStore().GetTermSet(_termSetId); clientContext.Load(termSet); clientContext.ExecuteQueryRetry(); // 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.ExecuteQueryRetry(); item.SetTaxonomyFieldValue(fieldId, _termName, _termId); clientContext.Load(item, i => i[fieldName]); clientContext.ExecuteQueryRetry(); var value = item[fieldName] as TaxonomyFieldValue; Assert.IsNotNull(value); Assert.IsTrue(value.WssId > 0, "Term WSS ID not set correctly"); Assert.AreEqual(_termName, value.Label, "Term label not set correctly"); Assert.AreEqual(_termId.ToString(), value.TermGuid, "Term GUID not set correctly"); } }
public static Field CreateTaxonomyField(this Web web, Guid id, string internalName, string displayName, string group, Term anchorTerm, bool multiValue = false) { TaxonomyFieldCreationInformation fieldCI = new TaxonomyFieldCreationInformation() { Id = id, InternalName = internalName, DisplayName = displayName, Group = group, TaxonomyItem = anchorTerm, MultiValue = multiValue }; return(web.CreateTaxonomyField(fieldCI)); }
/// <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); }
public static Field CreateTaxonomyField(this List list, Guid id, string internalName, string displayName, string group, TermSet termSet, bool multiValue = false) { TaxonomyFieldCreationInformation fieldCI = new TaxonomyFieldCreationInformation() { Id = id, InternalName = internalName, DisplayName = displayName, Required = false, Group = group, MultiValue = multiValue, TaxonomyItem = termSet }; return(list.CreateTaxonomyField(fieldCI)); }
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); }
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)); }
protected override void ExecuteCmdlet() { Field field; 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 = SelectedWeb.GetList(List); field = list.CreateTaxonomyField(fieldCI); } else { field = SelectedWeb.CreateTaxonomyField(fieldCI); } WriteObject(field); }
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 static void setupTaxonomyFields(ClientContext ctx) { Web root = ctx.Site.RootWeb; ///////////////////////////////////// ////////// using xml /////////////// //////////////////////////////////// //string pathToXML = AppDomain.CurrentDomain.BaseDirectory + "complexFields.xml"; //root.CreateFieldsFromXMLFile(pathToXML); ///////////////////////////////////// ////////// Using code /////////////// //////////////////////////////////// TaxonomyFieldCreationInformation taxInfo = new TaxonomyFieldCreationInformation(); taxInfo.Id = "{FB53E9CD-CC30-432C-95F1-E64CE1B4F2A1}".ToGuid(); taxInfo.InternalName = "OD1_TaxCode"; taxInfo.DisplayName = "Book Cat Code"; taxInfo.TaxonomyItem = getTermSet(ctx); root.CreateTaxonomyField(taxInfo); }
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 }
static void CreateTaxonomyField(ClientContext ctx) { //ctx.Web.GetFieldById("{4306F426-A772-4D1A-91D1-07F4CAA8884D}".ToGuid()).DeleteObject(); TermStore store = ctx.Site.GetDefaultSiteCollectionTermStore(); //guid is from the termset i created below Microsoft.SharePoint.Client.Taxonomy.TermSet term = store.GetTermSet("{FCB857B8-8F82-4EDD-B49A-5A5A5D492174}".ToGuid()); ctx.Load(term); ctx.ExecuteQuery(); TaxonomyFieldCreationInformation info = new TaxonomyFieldCreationInformation(); info.DisplayName = "Animal"; //field term id. new guid info.Id = "{4306F426-A772-4D1A-91D1-07F4CAA8884D}".ToGuid(); info.InternalName = "TIM_TaxAnimal"; //connect it to the termset we created below info.TaxonomyItem = term; info.Group = "Tims Fields"; ctx.Web.CreateTaxonomyField(info); }
public void SetBlankTaxonomyFieldValueTest() { 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.ExecuteQueryRetry(); // Retrieve Termset TaxonomySession session = TaxonomySession.GetTaxonomySession(clientContext); var termSet = session.GetDefaultSiteCollectionTermStore().GetTermSet(_termSetId); clientContext.Load(termSet); clientContext.ExecuteQueryRetry(); // Create taxonomyfield first TaxonomyFieldCreationInformation fieldCI = new TaxonomyFieldCreationInformation() { Id = fieldId, DisplayName = fieldName, InternalName = fieldName, Group = "Test Fields Group", TaxonomyItem = termSet }; //Add Enterprise keywords field so that we have at least two taxonomy fields in the list var keyworkdField = clientContext.Web.Fields.GetByInternalNameOrTitle("TaxKeyword"); clientContext.Load(keyworkdField, f => f.Id); list.Fields.Add(keyworkdField); var field = list.CreateTaxonomyField(fieldCI); // Create Item ListItemCreationInformation itemCi = new ListItemCreationInformation(); var item = list.AddItem(itemCi); item.Update(); clientContext.Load(item); clientContext.ExecuteQueryRetry(); //First set a valid value in at least two taxonomy fields (same term can be used if one field is the keyword field) item.SetTaxonomyFieldValue(fieldId, _termName, _termId); item.SetTaxonomyFieldValue(keyworkdField.Id, _termName, _termId); clientContext.Load(item, i => i[fieldName], i => i["TaxCatchAll"]); clientContext.ExecuteQueryRetry(); Assert.AreEqual(2, (item["TaxCatchAll"] as FieldLookupValue[]).Length, "TaxCatchAll does not have 2 entries"); var value = item[fieldName] as TaxonomyFieldValue; Assert.AreEqual(_termId.ToString(), value.TermGuid, "Term not set correctly"); //Set a blank value in one of the taxonomy fields. item.SetTaxonomyFieldValue(fieldId, string.Empty, Guid.Empty); var taxonomyField = clientContext.CastTo <TaxonomyField>(field); clientContext.Load(taxonomyField, t => t.TextField); clientContext.Load(item, i => i[fieldName], i => i["TaxCatchAll"]); clientContext.ExecuteQueryRetry(); var hiddenField = list.Fields.GetById(taxonomyField.TextField); clientContext.Load(hiddenField, f => f.InternalName); clientContext.ExecuteQueryRetry(); Assert.AreEqual(1, (item["TaxCatchAll"] as FieldLookupValue[]).Length, "TaxCatchAll does not have 1 entry"); object taxonomyFieldValue = item[fieldName]; object hiddenFieldValue = item[hiddenField.InternalName]; Assert.IsNull(taxonomyFieldValue, "taxonomyFieldValue is not null"); Assert.IsNull(hiddenFieldValue, "hiddenFieldValue is not null"); } }
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 }