Quantcast
Channel: SharePoint 2010 - Development and Programming forum
Viewing all articles
Browse latest Browse all 11508

Unable to set Managed Metadata field value in ItemAdding event receiver

$
0
0

 Hi All,

I have a document library with several Managed Metadata columns.
The requirement is to pre-set the "Asset Type" Column based on file extension once the file is uploaded.
for example, .gif, .jpg kinds of files will need to mapped to "Graphic", one of the Matadata values of Asset Type.


I am able to use  properties.AfterProperties to pre-set all other kinds of columns and works fine with the code below, but unluckily for Managed Metadata kind of Columns.
in debug mode, I can see the properties.AfterProperties[metaInternalName] is set, but the field is still in blank.
any ideas?

     public override void ItemAdding(SPItemEventProperties properties)
       {
           base.ItemAdding(properties);
           string metaInternalName = "";

           //Instantiates a new TaxonomySession for the current site.
           TaxonomySession session = new TaxonomySession(properties.Web.Site);
           //Instantiates the connection named "Managed Metadata Service Connection" for the current session.
           TermStore termStore = session.TermStores["Managed Metadata Service"];
           // Creates and commits a Group object named Group1, a TermSet object named termSet1, and several Term objects.
           Group group1 = termStore.Groups["MYMetaGroup"];
           TermSet termSet1 = group1.TermSets["Asset Type"];
           Term term1 = termSet1.Terms["Graphic"];
           TaxonomyFieldValue tfv;
           using (SPWeb web = properties.OpenWeb())
           {
               metaInternalName = web.Lists[properties.ListId].Fields["Asset Type"].InternalName;
               tfv = new TaxonomyFieldValue(web.Lists[properties.ListId].Fields["Asset Type"]);
               tfv.TermGuid = term1.Id.ToString();
               properties.AfterProperties[metaInternalName] = tfv;
           }

       }

Thanks!

David

 

 

 


Viewing all articles
Browse latest Browse all 11508

Trending Articles