0

How can i set an taxonomyfield during an itemAdding event?

i've got a TaxonomyFieldValue created from my propertybag.

public override void ItemAdding(SPItemEventProperties properties)
{
     //Get site and list we are working on
     SPSite CurrentSite = properties.OpenSite();
     using (SPWeb web = properties.OpenWeb())
     {
         TaxonomySession taxonomySession = new TaxonomySession(CurrentSite);
         TaxonomyFieldValue taxvalue= new TaxonomyFieldValue(probertybagValue.ToString());
         properties.AfterProperties["MyField"] = taxvalue.ToString();
     }
     base.ItemAdding(properties);
}

Normaly i would use Taxonomy SetFieldValue option, but this can only use with an SPItem object, and we haven't that object during itemAdding

2 Answers 2

1

i havent done this myself but why dont you implement the taxonomy:

    private void SetTaxononmyField(SPListItem newItem, TaxonomyFieldValue value, string fieldname)
    {
        TaxonomyField field = newItem.Fields.GetFieldByInternalName(fieldname) as TaxonomyField;
        if (field != null)
        {
            field.SetFieldValue(newItem, value);//Throws "Specified method is not supported" exception here
        }
    }

TaxonomyField.SetFieldValue throws "Specified method is not supported" Exception

hope it helps :)

2
  • The reason why i didn't implement that is that there is no SPListItem object in the ItemAdding Event.
    – Active_t
    Commented Jan 21, 2013 at 10:14
  • ? but you can! using (SPWeb web = properties.Web) { SPList list = web.Lists["ListName"]; SPListItemCollection listItemCollection = list.Items; foreach (SPListItem listItem in listItemCollection) { }} from that you call the mthod that i stated ;)
    – Ali Jafer
    Commented Jan 21, 2013 at 10:46
1

I had exactly the same requirement. What I did was use the ItemAdded event and set it to run Synchronous.

Hope this helps, Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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