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:
- No error or warning logs are being generated.
- 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.
- The component is saving whenever the "Save" button is clicked on the CMS (versions are updateing).