Developer Forum »
OData - Getting nodes from relation properties
31 posts

Hi!

I'm having trouble retrieving the nodes from a property of type "Content children".

In order to retrieve the children of "MyClass", I created a property "MyChildren" which reflects the contents of "Children". The new property as well as both the classes in the relation are exposed to OData. I tested this by querying the OData directly like so:

http://fakedomain.com/odata/MyClass(2451)/MyChildren


In the code however, it's a different story. First of all, the OData stream is added as a service reference, and the OData is queried through the generated c# objects. When I browse the objects I can drill down to my class and the property I created is there, only there are two of them: MyChildren and MyChildrenCount. The interesting thing is that when I debug the code the first property is empty while the second one (MyChildrenCount) shows 8. You can see this in the screenshot (btw. the class and property names are different, I just changed them to simplify the example).
 

Contents odata = new Contents(new Uri("http://fakedomain.com/odata"));

var myClass = odata.MyClass.FirstOrDefault();
var MyChildren = myClass != null ? myClass.MyChildren : null;

if (MyChildren != null)
{
	var output = (from child in MyChildren
		select new
		{
			id = child.Id,
			title = HttpUtility.HtmlEncode(child.Name.Trim())
		}).AsEnumerable().Reverse();

	_json = new JsonFx.Json.JsonWriter().Write(output);
}
Attachments
181 posts

In order to load related objects in OData, you need to use the Expand keyword.

http://www.odata.org/documentation/odata-v2-documentation/uri-conventions/#46_Expand_System_Query_Option_expand

The example below fetches all the sites in a Webnodes installation, and fetches all the direct children of the site:

from site in Sites select new{ Id=site.Id, Name=site.Name, Children=site.Children.Select(x => new { Name = x.Name, Id = x.Id }),ChildrenCount = site.ChildrenCount }

This should get you going, but if you need a more in-depth example, I'd be happy to provide that.

31 posts

Thanks! That did the trick :)

1