4

I would like to update an embedded Component field value when I check in my Page using TOM.NET Event System but the Component field value update is not happening.

I am not sure what am doing wrong here.

ItemFields componentFields = new ItemFields(component.Content, component.Schema);
if (componentFields.Contains("videoDetail"))
{
    EmbeddedSchemaField videoDetail = componentFields["videoDetail"] as EmbeddedSchemaField;
    if (videoDetail != null)
    {
        component.CheckOut();
        IList<ItemFields> videoDetailValues = videoDetail.Values;
        foreach (ItemFields innerFields in videoDetailValues)
        {
            if (innerFields.Contains("videoTitle"))
            {
                Headertext = innerFields["videoTitle"] as TextField;
            }
            if (innerFields.Contains("childPageName"))
            {
                XhtmlField childPageName = (XhtmlField)innerFields["childPageName"];
                childPageName.Value = component.Title;
            }
        }
    }
}
component.Content = componentFields.ToXml();
component.Save(true);
  • Can you confirm the foreach is being hit and that the conditions within them are hitting a true (i.e. via debugging). also (and I'm on a phone but).. looks to me like you're not actually updating the innerFields - can you write the debug value of the innerFields.ToXml() before you try to save? – Dylan .. Mark Saunders Oct 16 '17 at 21:33
  • Put Some Log in your code and trace it as @Dylan..MarkSaunders suggested. You are saving the component each and every time, is it required ? If not put it inside the condition. Also just for testing purpose try to check out the component before save and check in it after save. Let me know if it helps.. – Sayantan Basu Oct 17 '17 at 5:14
  • Thanks for the response. yes its going inside foreach and am getting component title value as expected but when I update the childPageName as component title it's not updating. Do I need to check out before update ? – Jayakumar Devaraj Oct 17 '17 at 15:17
  • Yes, a Component must be checked out before it can be modified, but I would expect the Save call to fail if the Component is not checked out. – Rick Pannekoek Oct 18 '17 at 17:57
4

I too wasn't able to update the embedded Component field using ItemFields class; however, I was able to update it by XML node manipulation. Something like this should definitely work:

var xDocument = XDocument.Parse(component.Content.OuterXml);
var namespace = xDocument.Root.GetDefaultNamespace();

// Check if videoDetail embedded field exists
if (xDocument.Descendants(namespace + "videoDetail").Count() > 0)
{
    component.TryCheckOut();

    // Get videoDetail embedded field values
    var videoDetailValues = from videoDetail in
                            xDocument.Descendants(namespace + "videoDetail")
                            select videoDetail;

    foreach (XElement innerFields in videoDetailValues)
    {
        if (innerFields.Elements(namespace + "videoTitle").Any())
        {
            string headerText = innerFields.Elements(namespace +"videoTitle").FirstOrDefault().Value;
        }

        // Set childPageName embedded field value to the title of the Component
        if (innerFields.Elements(namespace + "childPageName").Any())
        {
            innerFields.SetElementValue(namespace + "childPageName", component.Title);
        }
    }    
}

var updatedContent = new XmlDocument();
updatedContent.LoadXml(xDocument.ToString());
component.Content = updatedContent.DocumentElement;
component.Save(true);

May be experts here can answer as to how this can be achieved using ItemFields class. I've tested this by subscribing to a Page check-in event -- EventSystem.Subscribe<Page, CheckInEventArgs>(HandleForPageCheckInEvent, EventPhases.TransactionCommitted);

| improve this answer | |
  • Thanks sabarish. I have implemented using XML node manipulation – Jayakumar Devaraj Oct 30 '17 at 14:51
5

It looks like you are trying to save innerFields as Component content. But isn't componentFields actual content of Component?

Also, please make notice that you are trying to do this in Event System. This begs the question in which phase, and under which EventArgs.

It is possible that your Event System is not working correctly. If you want to debug Event System, you can do that "easily" using this article.

| improve this answer | |
  • yes your right it should be componentFields but still the component field is not updating. am trying remote event system debug. here is the event system call. EventSystem.SubscribeAsync<Page, CheckInEventArgs>(OnPageCheckInTransactionCommited, EventPhases.TransactionCommitted); – Jayakumar Devaraj Oct 17 '17 at 15: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.