3

I have an ItemFields object which has got all the ItemFields of a Component.

I want to remove a particular ItemField from it and return the entire ItemFields collection, so the code should be as below:

public static void WriteAllNodes(this XmlWriter Xml, ItemFields itemFields, String ignoreFieldNames)
{
  if (!String.IsNullOrEmpty(ignoreFieldNames))
  {
    String[] strSplitFields = ignoreFieldNames.Split(',');
    foreach (String fieldName in strSplitFields)
    {
      if (itemFields != null && itemFields.Contains(fieldName))
      {
        itemFields = itemFields.RemoveItemField(fieldName); 
        //What could be the best logic for this RemoveItemField function?
      }         
    }
  }
}

But the API does not contain a method ItemFields.RemoveItemField(), what can I use to remove an ItemField from the ItemFields?

  • Have you tried removing the value of the itemfield? – Nuno Linhares Jul 18 '13 at 11:53
  • I think setting the value of field to string.empty will remove the field. TextField field = strSplitFields["YourFieldName"] as TextField; field.Value = string.empty; – AlokBhatt Jul 18 '13 at 12:02
  • I am not getting any option of removing value of ItemField? – Manoj Singh Jul 18 '13 at 12:06
  • @Alok but in this case I have to loop it for all the types and then do the above stuff – Manoj Singh Jul 18 '13 at 12:07
  • You need to first cast the ItemField to the appropriate Field type - SingleLineTextField, or XhtmlField, etc - then you can call field.Values.Clear() or something along those lines. – Nuno Linhares Jul 18 '13 at 12:08
3

I think there indeed isn't a direct method to remove an ItemField from a ItemFields collection, so you have to use the XML in the ItemFields collection (you can use the component.Content directly also of course):

// get itemfields collection from Component
ItemFields itemFields = new ItemFields(component.Content, component.Schema);
XmlElement content = itemFields.ToXml();

// remove field from itemsfields collection directly in XML
XmlNode nodeToRemove = content.SelectSingleNode("//path.to.node");
content.RemoveChild(nodeToRemove);

// sync changed content back to Component before saving
component.Content = content;
component.Save();

Please make sure you sync back the changed ItemFields collection to the Component before saving because this is a detached collection. Any changes you make in the ItemFields collection will have no direct effect on the Component (or item) it comes from.

update

If you don't want to update the Component, and just need an updated ItemFields collection, then create that as follows:

// your current itemFields variable as input
XmlElement content = itemFields.ToXml();

// remove field from itemsfields collection directly in XML
XmlNode nodeToRemove = content.SelectSingleNode("//path.to.node");
content.RemoveChild(nodeToRemove);

// update itemfields collection 
itemFields = new ItemFields(content, component.Schema);

update 2

As per @Nuno's comment, you can also keep using the ItemFields collection and clear all the values of a certain itemField in there:

((TextField)itemFields[fieldName]).Values.Clear();
| improve this answer | |
  • I don't want to save the component, I just need the updated ItemFields, which I will pass to my function to render the desired output – Manoj Singh Jul 18 '13 at 12:18
  • you should just create an xml document and remove the desired node from that. – AlokBhatt Jul 18 '13 at 12:20
1

Your best solution is to use the XML directly and not the TOM.NET API.

All content in Tridion is stored as XML underneath and the APIs in both TOM.NET and the Core Service expose this XML. Using TOM.NET you would use something like GetXML and then you can manipulate the XML to your needs, and as long as it validates against the Tridion Schema you're good to go.

Otherwise, if you're not in a template or event system I would suggest using the Core service and remove the undesired field from the XML and call the Update method of the Core Service client and send back the modified XML.

| 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.