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

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

share|improve this question

2 Answers 2

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 :)

share|improve this answer
    
The reason why i didn't implement that is that there is no SPListItem object in the ItemAdding Event. –  Active_t Jan 21 '13 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 Sharepoint Jan 21 '13 at 10:46

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

Hope this helps, Thanks

share|improve this answer

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.