Developer Forum »
How to dublicate children (and expose them in OData feed)
31 posts

I have created a content children property "OdataChildren" which I want to use to expose the children in the OData feed. In order to do that I have to duplicate the contents of the Children property. The problem is that I have to run UpdateChanges() after I have copied the children, but this will again execute OnBeforeUpdate(), so I end up with an endless loop. I thought I would create a hidden integer property to use as a counter to make sure the method only runs once. But is there a simpler/better way to accomplish this?

 

public override void OnBeforeUpdate()
{
	base.OnBeforeUpdate();

	if (this.Children.Count != this.OdataChildren.Count)
	{
		this.OdataChildren.RemoveAll();

		var children = this.Children.Get();
		foreach (var child in children)
		{
			this.OdataChildren.Add(child.NodeId);
		}
		//this.UpdateChanges(); //Creates a loop
	}
}
120 posts

There are overloads on the "UpdateChanges" method that avoids triggering events. 

 

Without knowing the details of what you are trying to do, can you not use other events, like "OnAfterUpdate"

Ole

31 posts

OnAfterUpdate works, I have updated the code below. Ok, so that eliminates the loop issue. But there is still the issue of duplicating the children. The aim of the new property "OdataChildren" is to act as an exact copy of the "Children" property. They have to be in synch at all times, when children are added/removed and when child nodes change position (child node is moved up or down). The solution below is to clear all the contents of OdataChildren and then iterate through all the children and add each of them to OdataChildren. This might become a very heavy job as the number of children increases. So I was wondering if there is a better way to keep two relations in synch?

The only purpose for this is to expose the children in the OData feed. As far as I know this is the only way to do that without exposing the children of the other Hierarchical nodes.

 

public override void OnAfterUpdate()
{
	base.OnAfterUpdate();
	UpdateOdataChildren();
}

protected void UpdateOdataChildren()
{
	this.OdataChildren.RemoveAll();

	var children = this.Children.Get();
	foreach (var child in children)
	{
		this.OdataChildren.Add(child.NodeId);
	}

	this.UpdateChanges();
}

 

120 posts

Why are you calling UpdateChanges in the earlier example when you are using "onBeforeUpdate" ?

(it will be called by the system later)

31 posts

I tried running it without UpdateChanges, but that didn't work. The OdataChildren property was still empty afterwards.

120 posts

Are you sure, this should work, do you have example code?

31 posts

Ok, so I did a few more tests. It does seem to work, as long as the code is inside the override method, like below. I have no idea why it didn't work previously.

public override void OnBeforeUpdate()
{
    base.OnBeforeUpdate();
    
    // Update OdataChildren
    this.OdataChildren.RemoveAll();
    var children = this.Children.Get();
    foreach (var child in children)
    {
        this.OdataChildren.Add(child.NodeId);
    }
}


Edit:
I changed this.Children.Get() to this.Children.Get(true, true) in order to include unpublished nodes. Otherwise deactivated children will not be added later on when they are activated.

1