About a year ago I wrote about TaxonomyFieldValue and its WssId property. In a recent comment I got the question how to get the WssId for terms that are not referenced by the list items on the site.
I tried to answer the question there, but to provide a full example, I decided to post a code sample as well.
As described in the original post, the lookup item for the term and the corresponding WssId is created on the site through the private AddTaxonomyGuidToWss method.
One could call that method directly using reflection to generate the WssId for a term not yet in use on the site.
The following method – having the same name and signature as the original one – illustrates this call:
- private int AddTaxonomyGuidToWss(SPSite site, Term term, bool isKeywordField)
- {
- int result = -1;
- Type taxonomyFieldType = typeof(TaxonomyField);
- MethodInfo mi_AddTaxonomyGuidToWss = taxonomyFieldType.GetMethod("AddTaxonomyGuidToWss",
- BindingFlags.NonPublic | BindingFlags.Static, null,
- new Type[3] { typeof(SPSite), typeof(Term), typeof(bool) },
- null
- );
- if (mi_AddTaxonomyGuidToWss != null)
- {
- result = (int)mi_AddTaxonomyGuidToWss.Invoke(null, new object[3] { site, term, isKeywordField });
- }
- return result;
- }
The original AddTaxonomyGuidToWss method returns the WssId for the lookup item created for the term on the site or –1 if the creation is failed for some reason. Our wrapper method also returns –1 as the default value.
Let’s see a sample for its usage:
- private void CreateTermWssIdTest(SPSite site)
- {
- TaxonomySession session = new TaxonomySession(site);
- TermStore termStore = session.TermStores["Managed Metadata Service"];
- Group group = termStore.Groups["Test group"];
- TermSet termSet = group.TermSets["Colors"];
- Term term = termSet.Terms["Blue"];
- Console.WriteLine("Before creating local lookup item for term '{0}'", term.Name);
- DisplayTermWssIdInfo(site, term);
- AddTaxonomyGuidToWss(site, term, false);
- Console.WriteLine("After creating local lookup item for term '{0}'", term.Name);
- DisplayTermWssIdInfo(site, term);
- }
- private void DisplayTermWssIdInfo(SPSite site, Term term)
- {
- int[] wssIds = TaxonomyField.GetWssIdsOfTerm(site, term.TermStore.Id, term.TermSet.Id, term.Id, false, 1);
- Console.WriteLine("Lookup for term '{0}' using GetWssIdsOfTerm. WssId={1}", term.Name, wssIds.Length > 0 ? wssIds[0].ToString() : "Not found!");
- Console.WriteLine("Lookup for term '{0}' using GetWssIdByTermId. WssId={1}", term.Name, GetWssIdByTermId(site.RootWeb, term.Id));
- }
First we check for the WssId of a term called “Blue” (assumed not in use on the site), then create the WssId and then try to display again the value of the WssId. For the definition of the GetWssIdByTermId method see the original post.
On a test run we should see something similar:
Before creating local lookup item for term ‘Blue’
Lookup for term ‘Blue’ using GetWssIdsOfTerm. WssId=Not found!
Lookup for term ‘Blue’ using GetWssIdByTermId. WssId=-1
After creating local lookup item for term ‘Blue’
Lookup for term ‘Blue’ using GetWssIdsOfTerm. WssId=1
Lookup for term ‘Blue’ using GetWssIdByTermId. WssId=1
Before creating the local lookup item, the static GetWssIdsOfTerm method of the TaxonomyField class returns an empty array, that is reported as Not found! by the DisplayTermWssIdInfo method. Our helper method GetWssIdByTermId returns the default –1 value.
As I’ve tested the code on a site just created for this test, this is the first lookup item in the hidden lookup list in the site root web. That is why the term added is reported as having value 1 in the WssId.
I hope this simple example helps to better understand the concept of WssId.