none
How can I set the default value of "TaxonomyField myTaxoField.DefaultValue="WssID" if the WssIds aren't created?

    Question

  • In other words, I've created a TermStore, created a DocumentLibrary that refers to the termStore and I want to define the default value for the TaxonomieField, which is easy to do on the interface, but programmaticaly impossible since I can't find the WssIDs.

    Do you have any suggestions on how to do this.

    Thxs

    Thomas

    Thursday, August 12, 2010 3:35 PM

Answers

  • Hi Gougouk,

    I faced the same problem. After some reflactoring of how the UI does it i came up with the following code. The trick is to set the WssId = -1

    SPWeb web = properties.Feature.Parent as SPWeb;

    SPList list = web.Lists["Knoledgedocuments"];

    TaxonomySession sessions = new TaxonomySession(web.Site);

    TermStore store = sessions.TermStores["Metadata Service Application Proxy"];

    Group group = store.Groups["FillInTermStoreGroup"];

    TermSet productenTermSet = group.TermSets["Products"];

    //Select a term for the default value

    TermCollection terms = productenTermSet.GetTerms("Finance product", true);

    Term defaultValue = terms[0];

    //Check if the Taxonomy List Field exists

    if (list.Fields.ContainsField("Products"))

    {

    //Get the default value coming from the SiteColumn (ListColumn is a copied version of the SiteColumn)

    SPField field = list.Fields["Products"];

    //Cast the fieldtype to a TaxonomyFieldType

    TaxonomyField taxField = (TaxonomyField)field;

    //Get the default value coming from the SiteColumn Taxonomy Site Column

    TaxonomyFieldValue fieldValue = (TaxonomyFieldValue)taxField.DefaultValueTyped;

    //Compose the fieldvalue (label | termguid)

    string text = defaultValue.Labels[0].Value + TaxonomyField.TaxonomyGuidLabelDelimiter + defaultValue.Id.ToString();

    fieldValue.PopulateFromLabelGuidPair(text);

    //Check if the taxonomy term is not allready in the TaxonomyHiddenList of the rootsitecollection

    int[] ids = TaxonomyField.GetWssIdsOfTerm(web.Site, store.Id, productenTermSet.Id, defaultValue.Id, true, 1);

    if (ids.Length == 0)

    {

    //Set the WssId (TaxonomyHiddenList ID) to -1 so that it is added to the TaxonomyHiddenList

    fieldValue.WssId = -1;

    }

    //Set the default value 

    taxField.DefaultValue = fieldValue.ValidatedString;

    taxField.UserCreated = false;

    taxField.SspId = store.Id;

    taxField.TermSetId = productenTermSet.Id;

    taxField.AnchorId = new Guid(Guid.Empty.ToString());

    taxField.Update();

     

    Hope this helps...

    Donald

     
    dhessing
    Thursday, August 19, 2010 8:46 PM
  • Steve - Not that the hidden list isn't found, but that the default item that you want to set isn't in the hidden list. (I've been meaning to write up a blog post on how the taxonomy field is implemented.  Soon - soon.)

    Thomas - Here's some code from our dev that's been compiled in his head.  In other words, you might need to make some minor changes:

    "

    GetValidatedString is a public method.  Basically, what the person will need to do is run code similar to this:

    TaxonomyFieldValue taxValue = new TaxonomyFieldValue(taxField);

          taxValue.Label = term.GetDefaultLabel(1033);

          taxValue.TermGuid = term.Id;

          string taxValueWithId = taxField.GetValidatedString(taxValue);

     

    I just typed this up though, I didn’t test it… if you want it tested let me know and I’ll make sure it actually works.

    Now, the tricky part here is that you need to have the field defined before you can create an associated TaxonomyFieldValue (a bunch of the parameters are pulled from the field definition, like the service ID, the termset, etc.)  What you'll likely need to do is take two passes - one to define the field, then run the above code to get the WSSID, then update the field to have the default value.

    Hope that gets you unblocked.

    Pat.

    Tuesday, August 17, 2010 5:18 PM

All replies

  • If what you are wanting to do is set the field value through code, you'll want to follow this:

    http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.taxonomy.taxonomyfield.aspx

    Specifically, the TestTaxonomyFieldUpdateFieldItem example method.

    Pat.

    Friday, August 13, 2010 8:55 PM
  • Thanks for your reply,

    But I want to set the defaut value of the column, not an Item. Like I set the default value of a bool at true or false. I want to set the default with a specific Term. I create the library, add my specific taxonomyfield, And I want to set its default value (from the term store).
    To set this default value I need the Term Wssid. And it seems not to be created when no Item has been added to the list using the term.

    I saw that on this post http://social.technet.microsoft.com/Forums/en/sharepoint2010programming/thread/9b8cb71c-ff56-4bb0-aaa7-ab4d16d99a8a

    Thxs

    Thomas

    Monday, August 16, 2010 3:48 PM
  • I am not having any problems with the code below setting the default value.

    public static void SetDefaultValueMetaDataColumn()
    {
    
          using (SPSite site = new SPSite("http://basesmc2008"))
          {
    
            using (SPWeb web = site.OpenWeb())
            {
              TermSet termSet = null;
              TaxonomySession txs = new TaxonomySession(site);
              SPList list = web.Lists["Shared Documents"] as SPList;
              
              TaxonomyField field = list.Fields["multimanaged"] as TaxonomyField;
              
              if (field != null)
              {
                termSet = txs.DefaultSiteCollectionTermStore.GetTermSet(field.TermSetId);
                Term term = termSet.GetTerms("cts", false).FirstOrDefault();
                var result = new TaxonomyFieldValue(field);
                result.TermGuid = term.Id.ToString();
                result.Label = term.GetPath();
                result.WssId = TaxonomyField.GetWssIdsOfTerm(site, txs.DefaultSiteCollectionTermStore.Id, termSet.Id, term.Id, false, 1).First();
                field.DefaultValue = result.ValidatedString;
    
                field.Update();
    
              }
    
            }
    
          }
    
    }
    

    certdev.com
    Monday, August 16, 2010 9:31 PM
  • Hello Steve,

    Thanks for your answer. The code you're giving works properly and looks quite like mine.
    Although it only works if the default Term as already being used in a a library.
    I'll try to be more clear about my problem.

    In my case, we're deploying a site from scratch:
     New Site Col
     upload of my Termset
     New library with a specific column referring my Termset

     At this point no file has ever been uploaded in my site using my specific terms.
     
     So the following line : "result.WssId = TaxonomyField.GetWssIdsOfTerm(site, txs.DefaultSiteCollectionTermStore.Id, termSet.Id, term.Id, false, 1).First();"
      will return nothing
     
      If I upload a document using my default term and I re do : "result.WssId = TaxonomyField.GetWssIdsOfTerm(site, txs.DefaultSiteCollectionTermStore.Id, termSet.Id, term.Id, false, 1).First();"
      it will return "1", and the defaultvalue can be set.
     
     
      I hope I'm making myself more clear this time. In my case, this means I can't deploy my solution without having to use at least once my defaultTerms in order to get their WssID.
     
      Thxs
     
      Thomas

    Tuesday, August 17, 2010 9:10 AM
  • Go to central administration --> Monitoring-->Configure Diagnostic Logging, select SharePoint Server and check Taxonomy, then set the least critical trace event to Verbose, do the same for the event log. Then go through the scenario you just described. Check the ULS Logs to see what message is being logged. See if "Taxonomy guid not found" or "Hidden list did not exist when trying to get the WssId for the term" is logged.  The problem could be privileges. In order to see the hidden list the process account must have access to the hidden list, this will only occur if the term has not been cached yet. The term gets cached after you upload a document and use the term, so thus no access to the hidden list is needed.

    So are you deploying this as a feature, and trying to create the default value at time of feature activation? If so, then it is probably a privilege issue. Not sure what privileges are needed to access the hidden list, probably Site Administrator or possibly Farm administrator. If you are activating the feature are you logged in as a Site Administrator?


    certdev.com
    Tuesday, August 17, 2010 2:18 PM
  • Hey Steve / Thomas.  I think Thomas is right, in that if the term doesn't exist in the hidden list, then GetWssIdsOfTerm will not return anything.

    I'm looking through the code to see if there is a way to get this to work.  It looks like a case where we may be missing the required public OM, but there might be a way to trigger it through another codepath.

    Pat.

    Tuesday, August 17, 2010 2:31 PM
  • If the hidden list is not found it is created according to the private method TaxonomyField.GetLookupListHelper. So there is no possibility that the list would not be there.
    certdev.com
    Tuesday, August 17, 2010 2:58 PM
  • Steve - Not that the hidden list isn't found, but that the default item that you want to set isn't in the hidden list. (I've been meaning to write up a blog post on how the taxonomy field is implemented.  Soon - soon.)

    Thomas - Here's some code from our dev that's been compiled in his head.  In other words, you might need to make some minor changes:

    "

    GetValidatedString is a public method.  Basically, what the person will need to do is run code similar to this:

    TaxonomyFieldValue taxValue = new TaxonomyFieldValue(taxField);

          taxValue.Label = term.GetDefaultLabel(1033);

          taxValue.TermGuid = term.Id;

          string taxValueWithId = taxField.GetValidatedString(taxValue);

     

    I just typed this up though, I didn’t test it… if you want it tested let me know and I’ll make sure it actually works.

    Now, the tricky part here is that you need to have the field defined before you can create an associated TaxonomyFieldValue (a bunch of the parameters are pulled from the field definition, like the service ID, the termset, etc.)  What you'll likely need to do is take two passes - one to define the field, then run the above code to get the WSSID, then update the field to have the default value.

    Hope that gets you unblocked.

    Pat.

    Tuesday, August 17, 2010 5:18 PM
  • Hi Gougouk,

    I faced the same problem. After some reflactoring of how the UI does it i came up with the following code. The trick is to set the WssId = -1

    SPWeb web = properties.Feature.Parent as SPWeb;

    SPList list = web.Lists["Knoledgedocuments"];

    TaxonomySession sessions = new TaxonomySession(web.Site);

    TermStore store = sessions.TermStores["Metadata Service Application Proxy"];

    Group group = store.Groups["FillInTermStoreGroup"];

    TermSet productenTermSet = group.TermSets["Products"];

    //Select a term for the default value

    TermCollection terms = productenTermSet.GetTerms("Finance product", true);

    Term defaultValue = terms[0];

    //Check if the Taxonomy List Field exists

    if (list.Fields.ContainsField("Products"))

    {

    //Get the default value coming from the SiteColumn (ListColumn is a copied version of the SiteColumn)

    SPField field = list.Fields["Products"];

    //Cast the fieldtype to a TaxonomyFieldType

    TaxonomyField taxField = (TaxonomyField)field;

    //Get the default value coming from the SiteColumn Taxonomy Site Column

    TaxonomyFieldValue fieldValue = (TaxonomyFieldValue)taxField.DefaultValueTyped;

    //Compose the fieldvalue (label | termguid)

    string text = defaultValue.Labels[0].Value + TaxonomyField.TaxonomyGuidLabelDelimiter + defaultValue.Id.ToString();

    fieldValue.PopulateFromLabelGuidPair(text);

    //Check if the taxonomy term is not allready in the TaxonomyHiddenList of the rootsitecollection

    int[] ids = TaxonomyField.GetWssIdsOfTerm(web.Site, store.Id, productenTermSet.Id, defaultValue.Id, true, 1);

    if (ids.Length == 0)

    {

    //Set the WssId (TaxonomyHiddenList ID) to -1 so that it is added to the TaxonomyHiddenList

    fieldValue.WssId = -1;

    }

    //Set the default value 

    taxField.DefaultValue = fieldValue.ValidatedString;

    taxField.UserCreated = false;

    taxField.SspId = store.Id;

    taxField.TermSetId = productenTermSet.Id;

    taxField.AnchorId = new Guid(Guid.Empty.ToString());

    taxField.Update();

     

    Hope this helps...

    Donald

     
    dhessing
    Thursday, August 19, 2010 8:46 PM