private static TaxonomyFieldValueCollection CreateSharePointTaxonomyFieldValue(
            TaxonomyField sharepointTaxonomyField,
            TaxonomyValueCollection dynamiteCollection,
            List <string> labelGuidPairsListOutParam)
        {
            if (labelGuidPairsListOutParam == null)
            {
                labelGuidPairsListOutParam = new List <string>();
            }

            TaxonomyFieldValueCollection sharePointValueCollection = null;

            foreach (var TaxonomyValue in dynamiteCollection)
            {
                string labelGuidPair = TaxonomyItem.NormalizeName(TaxonomyValue.Term.Label) + TaxonomyField.TaxonomyGuidLabelDelimiter
                                       + TaxonomyValue.Term.Id.ToString().ToUpperInvariant();

                labelGuidPairsListOutParam.Add(labelGuidPair);
            }

            if (labelGuidPairsListOutParam.Count >= 1)
            {
                sharePointValueCollection = new TaxonomyFieldValueCollection(sharepointTaxonomyField);

                labelGuidPairsListOutParam.ForEach(labelGuidPair =>
                {
                    TaxonomyFieldValue taxoFieldValue = new TaxonomyFieldValue(sharepointTaxonomyField);
                    taxoFieldValue.PopulateFromLabelGuidPair(labelGuidPair);

                    sharePointValueCollection.Add(taxoFieldValue);
                });
            }

            return(sharePointValueCollection);
        }
Exemple #2
0
        private void OnTaxonomyMultiValueChanged(object sender, string fieldName)
        {
            TaxonomyFieldValueCollection collection = new TaxonomyFieldValueCollection("");

            foreach (Term term in (IEnumerable)sender)
            {
                TaxonomyFieldValue value = new TaxonomyFieldValue("");
                value.Label    = term.Name;
                value.TermGuid = term.Id.ToString();
                value.WssId    = term.EnsureWssId(this.Site, fieldName.Equals("TaxKeyword"));
                collection.Add(value);
            }
            this[fieldName] = collection.ToString();
            SetTaxonomyTextValue(fieldName, (IEnumerable <Term>)sender);
        }
        /// <summary>
        /// Updates a multi-value managed metatada column
        /// </summary>
        /// <param name="taxonomyTerms"></param>
        /// <param name="item"></param>
        /// <param name="fieldToUpdate"></param>
        public static void UpdateMultiMMField(string[] taxonomyTerms, SPListItem item, string fieldToUpdate)
        {
            //Get the metadata taxonomy field, a taxonomy session, the term store, and a collection of term sets in the store
            TaxonomyField   multipleManagedMetadataField = item.ParentList.Fields[fieldToUpdate] as TaxonomyField;
            Guid            tsId        = multipleManagedMetadataField.TermSetId;
            Guid            termStoreId = multipleManagedMetadataField.SspId;
            TaxonomySession tSession    = new TaxonomySession(item.ParentList.ParentWeb.Site);
            TermStore       tStore      = tSession.TermStores[termStoreId];
            TermSet         tSet        = tStore.GetTermSet(tsId);
            TaxonomyFieldValueCollection termCollection = new TaxonomyFieldValueCollection(multipleManagedMetadataField);

            //Loop through each value being added to the metadata field
            foreach (string t in taxonomyTerms)
            {
                TermCollection terms = tSet.GetTerms(t, false);
                Term           term  = null;
                //If there are no matching terms in the term store, create one
                if (terms.Count == 0)
                {
                    Console.WriteLine("Creating term in managed metadata, {0}", t);
                    term = tSet.CreateTerm(t, tStore.Languages[0]);
                    tStore.CommitAll();
                }
                else
                {
                    term = terms[0];
                }

                //Add the current term to a term collection
                TaxonomyFieldValue termValue = new TaxonomyFieldValue(multipleManagedMetadataField);
                termValue.TermGuid = term.Id.ToString();
                termValue.Label    = term.Name;
                termCollection.Add(termValue);
            }

            //Add the term collection to the metadata field
            multipleManagedMetadataField.SetFieldValue(item, termCollection);
            item.Update();
        }
        private TaxonomyFieldValueCollection GetTaxonomyFieldValueCollection(TaxonomyField fieldToSet)
        {
            byte someData = 0;

            // Create a temporary item to create the proper TaxonomyHiddenList items necessary to initialize the WSSids for the folder metadata defaults
            string tempPageName = string.Format(CultureInfo.InvariantCulture, "Temp-{0}.aspx", Guid.NewGuid().ToString());
            SPFile tempFile = ((SPDocumentLibrary)fieldToSet.ParentList).RootFolder.Files.Add(tempPageName, new byte[1] { someData });
            SPListItem tempItem = tempFile.Item;

            SPContentType contentTypeWithField = FindContentTypeWithField(fieldToSet.ParentList.ContentTypes, fieldToSet);
            SPContentTypeId contentTypeId = contentTypeWithField.Id;

            tempItem[SPBuiltInFieldId.ContentTypeId] = contentTypeId;
            tempItem.Update();

            // re-fetch temp item, now with proper content types
            tempItem = fieldToSet.ParentList.GetItemById(tempItem.ID);

            var itemField = tempItem.Fields[fieldToSet.Id] as TaxonomyField;

            TaxonomyFieldValueCollection fieldValues = new TaxonomyFieldValueCollection(fieldToSet);

            foreach (Term term in this.Terms)
            {
                TaxonomyFieldValue fieldValue = new TaxonomyFieldValue(fieldToSet);

                fieldValue.TermGuid = term.Id.ToString();
                fieldValue.Label = term.Name;
                fieldValues.Add(fieldValue);
            }

            // Force population of field values to hit the TaxonomyHiddenList and generate some WSSid's
            itemField.SetFieldValue(tempItem, fieldValues);

            // Those taxonomy field values in the collection don't have a proper ValidatedString, but their WSSid's have been populated
            TaxonomyFieldValueCollection finalValue = tempItem[itemField.InternalName] as TaxonomyFieldValueCollection;

            // Clean up the temporary item
            tempFile.Delete();

            return finalValue;
        }