Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I'm on a solution where I create a term with some subterms in the termstore. After that I create a sitecolumn and a listcolumn. When setting a value to the listcolumns with

 taxField.SetFieldValue(item, term, language);

taxonomyfied I get an argumentexception.

Microsoft.SharePoint.SPFieldValidationException: The given guid does not exist in the term store

The term is filled correctly and has the same guid as the element in the termstore (checked it with powershell).

What can I do to fill the newly created taxonomyfield or get around the excpetion.

[Update] It works with

taxField.SetFieldValue(item, term, language);

and the solution Stefan Bauer underneath this post. The problem is anywhere else (if you want to know read comments underneath stefan bauers post)

[Update #2] The problem was the taxonomy hidden list in the site. It was not up to date since I deleted a the field in the sitecolumns. Stefan Bauer pointed to his blogpost where the solution is first: Update your sharepoint and second: try TaxonomySession.SyncHiddenList(SPSite). Blogpost: http://www.n8d.at/blog/sharepoint-2010/anatomy-of-managed-metadata-fields/

share|improve this question
add comment

2 Answers 2

up vote 3 down vote accepted

To set a taxonomy field you should use TaxonomyFieldValue or TaxonomiFieldValueCollection

You first need to find the term in your term store:

            TaxonomyField taxonomyField = item.Fields[field] as TaxonomyField;
            TaxonomyFieldValue taxValues = new TaxonomyFieldValue(taxonomyField);
            TaxonomySession session = new TaxonomySession(site);
            TermStore termStore = session.TermStores[taxonomyField.SspId];
            TermSet termSet = termStore.GetTermSet(taxonomyField.TermSetId);
            TermCollection terms = termSet.GetTerms(termLabel);

If you retrieved the term from the term store you can then set the value as follows

        TaxonomyFieldValue taxonomyValue = new TaxonomyFieldValue(taxonomyField);
        taxonomyValue.TermGuid = term.Id.ToString();
        taxonomyValue.Label = term.Name;
share|improve this answer
    
Doesn't work. I get the same error. –  Steffen Schindler Jun 6 '12 at 8:58
    
This will only work if you first got the term from the Termstore. –  Stefan Bauer Jun 6 '12 at 9:03
    
I've got it 2 lines above from the termstore. –  Steffen Schindler Jun 6 '12 at 9:10
    
A little refactoring showed me that the function GetValidatedString() throws the error. But I just don't know why. –  Steffen Schindler Jun 6 '12 at 9:11
    
I added some code how to get the term you are looking for. You also need to loop over the termcollection. –  Stefan Bauer Jun 6 '12 at 9:12
show 5 more comments

For the error ...was not retrieved with all taxonomy fields

What others may find useful is that your Term Store field is starting at the wrong location within the Term Store. Hence the code cannot find your Term Store values as it only iterates below the Term Store field starting location which has been set.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.