Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically set a TaxonomyField on a list item

The situation:

I have a bunch of Terms in the Term Store and a list that uses them.

A lot of the terms have not been used yet, and are not available yet in the TaxonomyHiddenList. If they are not there yet they don't have an ID, and I can not add them to a list item.

There is a method GetWSSIdOfTerm on Microsoft.SharePoint.Taxonomy.TaxonomyField that's supposed to return the ID of a term for a specific site.

This gives back IDs if the term has already been used and is present in the TaxonomyHiddenList, but if it's not then 0 is returned.

Is there any way to programmatically add terms to the TaxonomyHiddenList or force it happening?

like image 417
Mel Gerats Avatar asked Apr 30 '10 15:04

Mel Gerats


3 Answers

Don't use

TaxonomyFieldValue tagValue = new TaxonomyFieldValue(termString);
myItem[tagsFieldName] = tagValue;"

because you will have errors when you want to crawl this item.

For setting value in a taxonomy field, you have just to use :

tagsField.SetFieldValue(myItem , myTerm);
myItem.Update();"

Regards

like image 168
Fab Avatar answered Oct 18 '22 08:10

Fab


In case of usage

string termString = String.Concat(myTerm.GetDefaultLabel(1033),
                                            TaxonomyField.TaxonomyGuidLabelDelimiter, myTerm.Id);  

then during instantiation TaxonomyFieldValue

TaxonomyFieldValue tagValue = new TaxonomyFieldValue(termString);

exception will be thrown with message

Value does not fall within the expected range

You have additionally provide WssId to construct term string like shown below

// We don't know the WssId so default to -1
       string termString = String.Concat("-1;#",myTerm.GetDefaultLabel(1033),
                                                    TaxonomyField.TaxonomyGuidLabelDelimiter, myTerm.Id);  
like image 7
Vadim Gremyachev Avatar answered Oct 18 '22 08:10

Vadim Gremyachev


On MSDN you can find how to create a Term and add it to TermSet. Sample is provided from TermSetItem class description. TermSet should have a method CreateTerm(name, lcid) inherited from TermSetItem. Therefore you can use it in the sample below int catch statement ie:

catch(...)
{
   myTerm = termSet.CreateTerm(myTerm, 1030);
   termStore.CommitAll();
}

As for assigning term to list, this code should work (i'm not sure about the name of the field "Tags", however it's easy to find out the proper internal name of the taxonomy field):

using (SPSite site = new SPSite("http://myUrl")) 
{
    using (SPWeb web = site.OpenWeb())
    {
        string tagsFieldName = "Tags";
        string myListName = "MyList";
        string myTermName = "myTerm";

        SPListItem myItem = web.Lists[myListName].GetItemById(1);
        TaxonomyField tagsField = (TaxonomyField) myList.Fields[tagsFieldName];
        TaxonomySession session = new TaxonomySession(site);
        TermStore termStore = session.TermStores[tagsField.SspId];
        TermSet termSet = termStore.GetTermSet(tagsField.TermSetId);
        Term myTerm = null;

        try
        {
            myTerm = termSet.Terms[myTermName];
        }
        catch (ArgumentOutOfRangeException)
        {
            // ?
        }

        string termString = String.Concat(myTerm.GetDefaultLabel(1033),
                                            TaxonomyField.TaxonomyGuidLabelDelimiter, myTerm.Id);

        if (tagsField.AllowMultipleValues)
        {
            TaxonomyFieldValueCollection tagsValues = new TaxonomyFieldValueCollection(tagsField);
            tagsValues.PopulateFromLabelGuidPairs(
                String.Join(TaxonomyField.TaxonomyMultipleTermDelimiter.ToString(),
                            new[] { termString }));
            myItem[tagsFieldName] = tagsValues;

        }
        else
        {
            TaxonomyFieldValue tagValue = new TaxonomyFieldValue(termString);
            myItem[tagsFieldName] = tagValue;
        }
        myItem.Update();
    }
}
like image 5
Gutek Avatar answered Oct 18 '22 07:10

Gutek