1

I am using sharepoint 2010. I have a document library which is using some custom content type. This content type has 2 custom fields which are termstore fields.

These are the names of the fields:

Subcategorie
Categorie

I would like to get the parent of the selected term in the field "Subcategorie" and set it to the field "Categorie".

This is the code:

public override void ItemUpdated(SPItemEventProperties properties)
        {
            // SPWeb web = properties.Web;
            SPList list = properties.List;
            SPListItem listItem = properties.ListItem;

            SPSite CurrentSite = properties.OpenSite();
            using (SPWeb web = properties.OpenWeb())
            {
                // check if this is a document library
                if (list.BaseType == SPBaseType.DocumentLibrary)
                {
                    var beforeItemSubCategorie = properties.BeforeProperties["Subcategorie"];
                    var afterItemSubCategorie = properties.AfterProperties["Subcategorie"];

                    var beforeItemCategorie = properties.BeforeProperties["Categorie"];
                    var afterItemCategorie = properties.AfterProperties["Categorie"];

                    string subCategorieInternalName = web.Lists[properties.ListId].Fields["Subcategorie"].InternalName;
                    string categorieInternalName = web.Lists[properties.ListId].Fields["Categorie"].InternalName;

                    properties.ListItem[categorieInternalName] = // get the parent of the term from the field "subcategorie". But how???

                    EventFiringEnabled = false;
                    properties.ListItem.SystemUpdate();
                    EventFiringEnabled = true;
                }

            }

            base.ItemUpdated(properties);
        }
  • I am struggling with something similar and think it has something to do with the TaxonomyItemEventReceiver. I am trying to set a taxonomy field in the ItemUpdated event. It seems to be fine, but after the indexer runs at night, the field is cleared. You had mentioned that you had to come up with a way to re-trigger the update cycle, but only once, after adding the taxonomy field. Could you elaborate on how you did that? Thanks, Paul – Muggers Oct 1 '14 at 16:24
0

First of all, you will need a reference to Microsoft.SharePoint.Taxonomy. Then, you could try something like this:

if (properties.List.BaseType == SPBaseType.DocumentLibrary)
{
    // get the taxonomy field value from the list item
    // this will give you the GUID of the term
    TaxonomyFieldValue subCatValue = properties.ListItem["Subcategorie"] as TaxonomyFieldValue;

    // get the term set of the field so you can get the actual term object
    TaxonomyField subCatField = properties.ListItem.Fields["Subcategorie"] as TaxonomyField;
    TaxonomySession txSession = new TaxonomySession(properties.Web.Site);
    TermStore termStore = txSession.TermStores["Your Term Store Name"];
    Group termGroup = termStore.Groups["Your Term Group Name"];
    TermSet termSet = termGroup.TermSets[subCatField.TermSetId];

    // get the term object using the taxonomy field value
    // you will need to convert the TermGuid string into an actual GUID
    Term subCatTerm = termSet.GetTerm(new Guid(subCatValue.TermGuid));

    // get the parent
    Term categorieTerm = subCatTerm.Parent;

    // add the term to the field
    TaxonomyField categorieField = properties.ListItem.Fields["Categorie"] as TaxonomyField;
    categorieField.SetFieldValue(properties.ListItem, categorieTerm);

    EventFiringEnabled = false;
    properties.ListItem.SystemUpdate();
    EventFiringEnabled = true;
}

(Keep in mind this is untested.)

Also, when setting taxonomy fields through ItemUpdated event receivers, I have run into some odd behavior because there is a SharePoint event receiver (Microsoft.SharePoint.Taxonomy.TaxonomyItemEventReceiver) that runs on ItemAdd*ing* and ItemUpdat*ing*, so if you disable event firing (as you should) in order to make your change in ItemUpdat*ed*, the SharePoint code never gets run. The value you set is there, though, and things start working as expected after another update to the item. (For my solution I had to come up with a way to re-trigger the update cycle, but only once, after adding the taxonomy value, and skip the custom code on the re-triggered update.)

EDIT (to reply to your second comment / question):

I don't think you can get the term store or group name dynamically from the TaxonomyFieldValue object. Here is the documentation on TaxonomyFieldValue.

However, after looking more at TaxonomySession, it seems you can call GetTerm() directly on the session, so you wouldn't need to get the term store or group (or term set). Keep in mind though, Microsoft does warn that "If the current Term belongs to multiple TermSet objects, it will arbitrarily return the Term from one of the TermSet objects." So if your Subcategorie terms are in more than one term set, you might need to do some checking to make sure you have the right term set so you will get the correct parent. Here is the documentation on TaxonomySession.

But the code could look like this:

// get the taxonomy field value from the list item
// this will give you the GUID of the term
TaxonomyFieldValue subCatValue = properties.ListItem["Subcategorie"] as TaxonomyFieldValue;

// just start the taxonomy session
TaxonomySession txSession = new TaxonomySession(properties.Web.Site);

// get the term object using the taxonomy field value
// you will need to convert the TermGuid string into an actual GUID
Term subCatTerm = txSession.GetTerm(new Guid(subCatValue.TermGuid));

// get the parent
Term categorieTerm = subCatTerm.Parent;

// etc...
  • can I get the termstore name and group name dynamic by the subCatValue object? Otherwise I need to enter fixed names. – Ola Sep 10 '13 at 8:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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