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

Updating Data Items

Updating Data Items

The Backend Services .NET SDK allows you to update data items in existing content types programmatically.

Workflow

In general, updating an item requires calling the Update() Backend Services .NET SDK method with the instance that contains the changed data.

var activity = await app.WorkWith().Data<Activity>().GetById(activityId).ExecuteAsync();
activity.Text = "Changed text";

await app.WorkWith().Data<Activity>().Update(activity).ExecuteAsync();

The code above will send an update query to the server, only including the modified fields. However, you need to implement additional logic to tell the SDK what has changed. You have a couple of options:

  • Use the SDK-provided SetProperty method inside your setter. It checks the values for modifications automatically.

    private int someProperty;
    public int SomeProperty
    {
        get
        {
            return this.someProperty;
        }
        set
        {
            this.SetProperty(ref this.someProperty, value, "SomeProperty");
        }
    }
    
  • Call DataItem class's OnPropertyChanged method. It only marks the property as changed. It is up to you to set it and make any comparisons that you want. System properties (Owner, ModifiedBy) and dynamic properties (accessed as a dictionary) are updated automatically.

    public DateTime PublicationDate
    {
        get
        {
            return this.publicationDate;
        }
        set
        {
            this.publicationDate = value;
            this.OnPropertyChanged("PublicationDate");
        }
    }
    

After you have implemented the update logic, continue to the next section to learn how to use the Update() method.

Updating a Single Item by ID

To update a single item by ID, pass the item ID and the instance that contains the modified item to the Update() method.

public async Task UpdateActivityById(EverliveApp app, Guid activityId, Activity updatedActivity)
{
    await app.WorkWith().Data<Activity>().Update(activityId, updatedActivity).ExecuteAsync();
}

You can update system fields such as CreatedAt and CreatedBy. However, you can set them only after doing the following:

Updating Multiple Items

To update several items at once, build a filter that matches them. This way the updated value that you pass will be written to all matching items. Also pass the instance containing the modified data to the Update() method.

If you omit the filter, you will be updating all items in the specified content type.

The following example used a LINQ expression to specify the filter that searches for items whose UserId field matches a given value.

public async Task UpdateActivitiesByFilter(EverliveApp app, Guid userId)
{
    UpdateObject newValue = new UpdateObject();
    newValue.UpdatedFields.Add(new UpdatedField()    
    {
        FieldName = "Text",
        Value = "Updated Text"    
    });

    await this.App.WorkWith().Data<Activity>().Update(newValue).Where(a => a.UserId == userId).ExecuteAsync();
}

Instead of UpdateObject you can directly set the Text property of an instance of the same class (Activity) to "Updated Text". This is possible because both DataItem and UpdateObject implement IUpdatebleObject.


Be careful when updating fields marked as unique (IsUnique) by filter as it may match multiple items. In this case, Telerik Platform updates the first matching item if the update value does not duplicate an existing item, after which it stops executing the request and returns an error. The rest of the matching items are not updated.

See Also

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