How can I add an item to a repeatable property programmatically in c#:
let’s say I have a node (node id 1234) and in it companies list property, where each item has comapny name and image (media picker).
how do I add an item programmtically ?
Here’s what I have so far:
XPathNodeIterator xpathIterator = umbraco.library.GetXmlNodeById(NodeId.ToString());
XElement node = XElement.Parse(xpathIterator.Current.OuterXml);
var list = node.Descendants(propertyAlias).FirstOrDefault();
// how do I add items here ? something like:
list.Descendants().Add(...)
thanks.
The package I’m referring to is:
Repeatable Custom Content
update:
I think the solution is to update the xml in umbraco.config.
I have the following xml in umbraco.config:
<Companies id="1176" parentID="1447" ...>
<umbracoNaviHide>0</umbracoNaviHide>
<companyList>
<item>
<data alias="title">Company1</data>
<data alias="image" />
<data alias="text" />
<data alias="date" />
</item>
<item>
<data alias="title">Company2</data>
<data alias="image">1943</data>
<data alias="text" />
<data alias="date" />
</item>
</items>
</companyList>
</Companies>
I am able to update umbraco.config programmatically, but the results are not updated in the backend, so that when I publish the companies node again, the changes are deleted. How can I update the umbraco.config and publish the node ?
Maybe I sh should update the database directly instead ?
My code:
Document companiesDoc = new Document(COMPANIESNODEID);
XmlDocument document = content.Instance.XmlContent;
XmlNode n = document.SelectSingleNode("//Companies[@id=" + COMPANIESNODEID.ToString() + "]").SelectSingleNode("//items");
XmlNode newItem = document.CreateNode(XmlNodeType.Element, "item", null);
XmlNode dName = document.CreateNode(XmlNodeType.Element, "data", null);
XmlAttribute xn = document.CreateAttribute("alias");
xn.Value = "title";
dName.Attributes.Append(xn);
dName.InnerText = companyName;
XmlNode dImage = document.CreateNode(XmlNodeType.Element, "data", null);
XmlAttribute xi = document.CreateAttribute("alias");
xi.Value = "image";
dImage.Attributes.Append(xi);
dImage.InnerText = companyImage;
XmlNode dText = document.CreateNode(XmlNodeType.Element, "data", null);
XmlAttribute xt = document.CreateAttribute("alias");
xt.Value = "text";
dText.Attributes.Append(xt);
XmlNode dDate = document.CreateNode(XmlNodeType.Element, "data", null);
XmlAttribute xd = document.CreateAttribute("alias");
xd.Value = "date";
dDate.Attributes.Append(xd);
newItem.AppendChild(dName);
newItem.AppendChild(dImage);
newItem.AppendChild(dText);
newItem.AppendChild(dDate);
n.AppendChild(newItem);
I managed to solve the problem using this code: