Progress will discontinue Telerik Platform on May 10th, 2018. Learn more

Renaming the Expanded Field

Renaming the Expanded Field

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

When expanding fields, the default behavior is to use the content type field name for the expanded field in the result.

You can rename the return field by passing a second parameter to the ExpandDefinition constructor. You can use it regardless of whether the relation field is held in the queried content type or the related content type. It is designed to give the option to return a completely different object when expanding a field. For example in a .NET environment, the field return type will be changed from a "Guid" to an "object" and will break the deserialization process. And since that is what the expand expression do - return an object instead of an "Id", there is an option to rename the property of the result. Let's take a standard activity (from the Telerik Platform social app example):

{
    Activity: {
        "Id": "activityId",
        "Likes": [
            "user1Id",
            "user2Id"
        ]
    }
}

where "Likes" is a multiple relation field. In C# this field will be deserialized to IEnumerable<Guid>(). Adding a simple expand expression like:

ExpandDefinition expandDefinition = new ExpandDefinition("Likes");
EverliveApp.WorkWith().Data<Activity>().Get().Expand(expandDefinition).ExecuteSync();

This will break any existing application, since Telerik Platform will return something like:

{
    Activity: {
        "Id": "activityId",
        "Likes": [{
            User1Object...
        }, {
            User2Object...
        }]
    }
}

which cannot be deserialized to a collection of Guids. Instead ReturnAs configuration option can be used to return the result in "UsersWhoLikeTheActivity" and add a new property of type IEnumerable<User> in which the result will be deserialized.

To preserve the data integrity, these renamed fields should not be returned back to the server, since this will add the new field in the database with the nested object inside (which usually is not the desired behavior). In order to restrict a specific field from writing to the server the expanded field should be deleted from the update (create) object. In order to restrict a specific property from writing to the server, take a look at .NET object model help article.

ExpandDefinition expandDefinition = new ExpandDefinition("Likes", "UsersWhoLikeThisActivity");
EverliveApp.WorkWith().Data<Activity>().Get().Expand(expandDefinition).ExecuteSync();

which will result in:

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