3

I am working on event code 2011. I want to access a component attached to the page and update a field in that component. The component has two fields (Body and BodyImagePublish), both are text field. I know the code to read the component and acess its field but i don't know why the field shows null even if the component's [Body] field has the value(value is basically a xml)

The code is as below

      //Getting the component attached to this page
            page2 = (Tridion.ContentManager.CommunicationManagement.Page)publication.GetObject(uRI);
            //Getting the component as i want to read the [Body] field. Body has the xml  
            compToUpdate = page2.ComponentPresentations[0].Component;
            //I checked in the log generated by below statement that i am getting the tcm id of the component which has the values
            EventHelper.WriteEventToHTMLLog("comtoUpdate string: " + compToUpdate.ToString());
            //But the below condition does not satisfy, i opened the component and am sure that there is value in the body field
            if (compToUpdate.Content["Body"].Value != null)
            {
                //this condition does not  get satisfied which means that the component is null
            }
            //I also tried the below code from TOM.NET API chm file, but it throws error
            ItemFields contentFields = new ItemFields(compToUpdate.Content, compToUpdate.Schema);
            TextField myTextField = (TextField)contentFields["Body"];
            string myTextFieldValue = myTextField.Value;
            EventHelper.WriteEventToHTMLLog("comtoUpdate body field: " + myTextFieldValue);

Please help me.

  • 1
    What is the value of compToUpdate.Content? It should return an XML document. – robrtc Mar 10 '14 at 13:26
  • Perhaps the XML name of the field is something other than "Body"? – Nickoli Roussakov Mar 10 '14 at 13:28
  • @robrtc - The value of content comes as System.Xml.XmlElement and comptoUpdate.Metadata comes as blank. – Varun Doharey Mar 10 '14 at 13:49
  • @Nickoli- The name of the field is Body only. – Varun Doharey Mar 10 '14 at 13:49
  • 1
    No, indeed. You need to do Component.Content = contentFields.ToXml() after all your changes. Content is a "disconnected" collection – Nuno Linhares Mar 10 '14 at 15:19
3

answering the last comment to Nuno:

you can try below code, can adjust accordingly for checkout/checkin of the component. I havn't tested it so adjustments could be needed.

XDocument doc;
XElement xmlData;

string fieldName = "Body";
string fieldValue = "New Body Value";
doc = XDocument.Parse(compToUpdate.Content.OuterXml);
xmlData = doc.Root;
var newDoc = new XmlDocument();
XNamespace ns = xmlData.GetDefaultNamespace();
if (xmlData.Descendants(ns + fieldName).Count() > 0)
{
    xmlData.Descendants(ns + fieldName).SingleOrDefault().Value = fieldValue;

    newDoc.LoadXml(doc.ToString());
    compToUpdate.Content = newDoc.DocumentElement;
}

compToUpdate.Save();

EDIT by Nuno

Or you could acheve the same with the ItemFields class:

ItemFields content = new ItemFields(compToUpdate.Content, compToUpdate.Schema);
XhtmlField body = (XhtmlField) content["Body"];
body.Value = "some new value";
compToUpdate.Content = content.ToXml();

Since this is in an Event, I would not call Save(), and instead just modify the component in the Initiated phase and let Tridion do the save as normal.

| improve this answer | |

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.