2

I'm currently encountering an issue with the Tridion Event System. Specifically, I've been attempting to assign a string value to a metadata field in a component but I've tried several methods none of them worked for my code.

ItemFields metaFields = new ItemFields(component.Metadata, component.MetadataSchema);
string value = (KeywordField)metaFields[TargetValue];
string metadataValue= ((TextField)metaFields[metadataField]).Value;

I am trying to assign "value" into "metadataValue" and assign it to metadataField.

I've tried to save the component that shows below but none of them worked.

args.ContextVariables[metadataField] = value;
component.Metadata.SetAttribute(metadataField, value);
component.Content = metaFields.ToXml();
component.Metadata = metaFields.ToXml();

Can someone suggest that how to assign this value to a metadata field?

I have updated with a actual method. So I am trying to combine metafieldOneValue and metafieldTwoValue to metafieldThreeValue.

 public void CombineFields(Component component, SaveEventArgs saveArgs = null,
        RollbackEventArgs rollbackEventArgs = null, EventPhases phases = new EventPhases())
    {
        TcmEventArgs args = null;
        if (saveArgs != null)
        {
            args = saveArgs;
        }
        if (rollbackEventArgs != null)
            args = rollbackEventArgs;

        ItemFields metaFields = new ItemFields(component.Metadata, component.MetadataSchema);


        TextField metafieldOne= (TextField)metaFields[Constants.METAONE];
        string metafieldOneValue= metafieldOne.Value;

        KeywordField metafieldTwo= (KeywordField)metaFields[Constants.METATWO];
        string metafieldTwoValue = metafieldTwo.Value.Key.ToString();

        TextField metafieldThree= ((TextField)metaFields[Constants.METATHREE]);
        string metafieldThreeValue= metafieldThree.Value;

        metafieldThreeValue = metafieldOneValue + metafieldTwo;


        component.Metadata = metaFields.ToXml();
   }

Here is some additional information to consider:

  1. No error or warning logs are being generated.
  2. The XML is well-formed and in the correct order on the CMS (Content Management System). I logged out the xml and the value is correct to but in CMS the field doesn't update on Save.
  3. The component is saving whenever the "Save" button is clicked on the CMS (versions are updateing).
2
  • Can you share a full code block with relevant code, like which EventHandler you subscribe to and how you save the component? Also, are you sure 'value' is set?
    – Raimond
    Commented Jul 12, 2023 at 13:56
  • 1
    I have updated the questions with a full method. So I've tried to log the "metafieldThreeValue" and it appears correct. But when I save the component on Tridion CMS, it doesn't update the field. Commented Jul 12, 2023 at 15:37

2 Answers 2

2

Thanks all for the input! It is solved.

We needed to set metafieldThree.Value = metafieldOneValue + metafieldTwoValue first to update the XML and assign the right value to it.

We also logged the XML out and calling component.Metadata = metadataFields.ToXml() is updating the XML but not the component in CMS.

We Added component.Save() to save the component at the end and it worked.

1
  • 1
    If you change the xml in the event phase fired just before the save (can’t recall the phase name) then you won’t have to call Save() yourself.
    – Nuno Linhares
    Commented Jul 14, 2023 at 3:28
1

The TOM.NET API (which is what you're using in your example code) allows you to modify custom content/metadata either directly (using an XML DOM) or indirectly using the ItemFields object model.

In your example code, you are using the ItemFields object model to read some metadata field values, but you're not using it to change values. The Value property of an ItemField object can be set (or for multi-valued fields, you can modify the Values property).

Do realize that the ItemFields object model is a disconnected representation of the custom content/metadata; if you change anything in that object model, you are not changing the custom content/metadata (XML) itself yet. You should explicitly synchronize using

component.Metadata = metadataFields.ToXml()

1
  • Thank you Rick for your answer! I have updated my question with a full method. I used "component.Metadata = metadataFields.ToXml()" at the end of my method but when I try to save the component on CMS, it does save but the field isn't updating. Commented Jul 12, 2023 at 16:00

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.