Defining a Relation Expanding Expression

Defining a Relation Expanding Expression

The Backend Services .NET SDK provides a set of utility methods used to construct an expand expression and a helper class for creating expand expressions.

The ExpandDefinition helper class provides a convenient way to construct an expand expression compatible with Telerik Platform.

There are two ways to construct the expand expression depending on whether the queried content type holds the relation or not.

The Queried Content Type Holds the Relation

The following code demonstrates how to use the ExpandDefinition class to create a basic expand expression that expands the Likes field:

ExpandDefinition expandDefinition = new ExpandDefinition("Likes");
{ "Likes": true }

After you have defined the expand expression, adding it to a given read request is as simple as calling the Expand() method of the GetById or GetByFilter facades. The method's overloads accept any of the following as parameter:

  • An ExpandDefinition instance
  • An IEnumerable<ExpandDefinition> instance
  • The string representation of an expand expression

The next example passes an instance of ExpandDefinition to the Expand() method:

EverliveApp.WorkWith().Data<Activity>().Get().Expand(expandDefinition).ExecuteSync();

It returns this result (using the Telerik Platform sample social app):

{
    Count: 2,
    Result: [{
        "Likes": [{
            "Username": "seth",
            "DisplayName": "Seth Peterson",
            ...
        }],
        "Text": "It is finally time for graduation! Good job everyone, we made it."
            ...
    }, {
        "Likes": [],
        "Text": "The most amazing sunset I have ever seen at Phuket, Thailand",
        ...
    }]
}

The Queried Content Type Does Not Hold the Relation

To expand a relation field that is held in the related content type, you define the expand expression in a ContentType.RelationField format, specifying the related content type and its relation field that points to the queried item.

For example, in the same application as above, if we want to retrieve the Activities that were liked by a given user, we make the following request to the Users content type:

ExpandDefinition expandDefinition = new ExpandDefinition("Activities.Likes");
EverliveApp.WorkWith().Data<User>().GetById(userId).Expand(expandDefinition).ExecuteSync();

The result is a single User object with a property Activities.Likes containing an array of the Activities that have the user's ID in their Likes.

{
    Result: {
        Username: "seth",
    ...
        "Activities.Likes": [{
            "Text": "It is finally time for graduation! Good job everyone, we made it."
        }]
    }
}

The JSON representation of the result is used only for better readability. The actual result is a deserialized object according to the properties of the Activity and User classes. To have proper deserialization, the same properties of a matching type should be defined in the client model classes.

See Also

Start a free trial Request a demo
Contact us: +1-888-365-2779
sales@telerik.com
Copyright © 2016-2017, Progress Software Corporation and/or its subsidiaries or affiliates. All rights reserved.