Skip to main content
Version: 8.1

Update a record in the section using DataService

Level: advanced
Example

Create a console application that used DataService to update the "John Smith" record added in the example. Add "j.smith@creatio.com" as the value in the Email column of this record.

Example implementation algorithm

1. Create and set up a C# application project

Using the Microsoft Visual Studio development environment (version 2017 and up), create a Visual C# console application project and specify project name, for example, DataServiceUpdateExample. Set ".NET Framework 4.7" for the project property Target framework.

In the References section of the project, add dependencies from the following libraries:

  • System.Web.Extensions.dll – class library included in .NET Framework;
  • Terrasoft.Core.dll – library of base Creatio server core classes. It can be found using the following path: [Creatio setup catalog]\Terrasoft.WebApp\bin\Terrasoft.Core.dll;
  • Terrasoft.Nui.ServiceModel.dll – application service class library. It can be found using the following path: [Creatio setup catalog]\Terrasoft.WebApp\bin\Terrasoft.Nui.ServiceModel.dll;
  • Terrasoft.Common.dll – library of base Creatio server core classes. It can be found using the following path: [Creatio setup catalog]\Terrasoft.WebApp\bin\Terrasoft.Common.dll.

Add the using directives to the application source code file:

Adding the using directives
using System;
using System.Text;
using System.IO;
using System.Net;
using System.Collections.Generic;
using Terrasoft.Nui.ServiceModel.DataContract;
using Terrasoft.Core.Entities;
using System.Web.Script.Serialization;
using Terrasoft.Common;

2. Add fields and constants and field declarations to the source code

To access DataService features, add the fields and constants to the application source code.

Adding the fields and constants
// Primary URL of Creatio application. Must be replaced with a custom one.
private const string baseUri = @"https://example.creatio.com";
// Request string to the Login methid of the AuthService.svc service.
private const string authServiceUri = baseUri + @"/ServiceModel/AuthService.svc/Login";
// Path string for the UpdateQuery.
private const string updateQueryUri = baseUri + @"/0/DataService/json/reply/UpdateQuery";
// Creatio authentication cookie.
private static CookieContainer AuthCookie = new CookieContainer();

Here, three string fields are declared. These fields will be used to form authentication query and read data queries execution paths. Authentication data will be saved in the AuthCookie field.

3. Add method that performs Creatio application authentication

Authentication is required to enable access to the DataService for the created application.

4. Add implementation of the record add query

Because the updateQueryUri constant declared earlier contains a path for sending data in the JSON format, sent data must be configured beforehand as a string that contains a JSON object that corresponds to the UpdateQuery data contract. This can be done directly in a string variable, although a much more secure and convenient way of doing this would be to create an instance of the UpdateQuery class, fill out its properties and then serialize it to a string.

Implementation the request for adding a record
// Instance of the request class.
var updateQuery = new UpdateQuery()
{
// Root schema name.
RootSchemaName = "Contact",
// New column values.
ColumnValues = new ColumnValues()
{
// Key-value collection.
Items = new Dictionary<string, ColumnExpression>()
{
// [Email] column.
{
// key.
"Email",
// Value – instance of object schema request class.
// Configuration of [Email] column.
new ColumnExpression()
{
// Type of expression of obkect schema query – parameter.
ExpressionType = EntitySchemaQueryExpressionType.Parameter,
// Query expression parameter.
Parameter = new Parameter()
{
// Parameter value.
Value = "j.smith@creatio.com",
// Parameter data type – string.
DataValueType = DataValueType.Text
}
}
}
}
},
// Query filters.
Filters = new Filters()
{
// Filter type – group.
FilterType = Terrasoft.Nui.ServiceModel.DataContract.FilterType.FilterGroup,
// Filter collection.
Items = new Dictionary<string, Filter>()
{
// Filter by name.
{
// Key.
"FilterByName",
// Value.
new Filter
{
// Filter type – comparison filter.
FilterType = Terrasoft.Nui.ServiceModel.DataContract.FilterType.CompareFilter,
// Comparison type – starts with expression.
ComparisonType = FilterComparisonType.Equal,
// Expression to check.
LeftExpression = new BaseExpression()
{
// Expression type - schema column.
ExpressionType = EntitySchemaQueryExpressionType.SchemaColumn,
// Path to column.
ColumnPath = "Name"
},
// Filtering expression.
RightExpression = new BaseExpression()
{
// Expression type - parameter.
ExpressionType = EntitySchemaQueryExpressionType.Parameter,
// Expression parameter.
Parameter = new Parameter()
{
// Parameter data type - text.
DataValueType = DataValueType.Text,
// Parameter value.
Value = "John Smith"
}
}
}
}
}
}
};
// Serialization of update query class instance in a JSON string.
var json = new JavaScriptSerializer().Serialize(updateQuery);

Here, an instance of the UpdateQuery class is created. In the ColumnValues property, the "j.smith@creatio.com" value is set for the Email column. To apply this value to a specific record or group of records, specify a link to a correctly initialized Filters class in the Filters property. In this case, a single filter is added to the filters collection to select only records that have the "John Smith" value in the Full name column.

The next step is to execute DataService POST-query. To do this, create an instance of the [HttpWebRequest](https://msdn.microsoft.com/en-us/library/system.net.httpwebrequest(v=vs.110).aspx) class, fill its properties and connect the string with the JSON object created earlier then execute the DataService query and process its result. To do this, add the following source code:

Request implementation
// Converting a JSON object string to a byte array.
byte[] jsonArray = Encoding.UTF8.GetBytes(json);
// Creating an instance of HTTP request.
var updateRequest = HttpWebRequest.Create(updateQueryUri) as HttpWebRequest;
// Defining a request method.
updateRequest.Method = "POST";
// Determining type of request content.
updateRequest.ContentType = "application/json";
// Adding authentication cookie received earlier to a request.
updateRequest.CookieContainer = AuthCookie;
// Set length for request content.
updateRequest.ContentLength = jsonArray.Length;

// Putting BPMCSRF token to the request header.
CookieCollection cookieCollection = AuthCookie.GetCookies(new Uri(authServiceUri));
string csrfToken = cookieCollection["BPMCSRF"].Value;
updateRequest.Headers.Add("BPMCSRF", csrfToken);

// Plase a JSON-object to request content.
using (var requestStream = updateRequest.GetRequestStream())
{
requestStream.Write(jsonArray, 0, jsonArray.Length);
}
// Executing HTTP request and getting a response from server.
using (var response = (HttpWebResponse)updateRequest.GetResponse())
{
// Displaying response in console.
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
Console.WriteLine(reader.ReadToEnd());
}
}

Resources

Example implementation (GitHub)