Skip to main content
Version: 8.1

Delete a record in the section using DataService

Level: advanced
Example

Create a console application that, using the DataService service, will delete the "John Best" contact record added in the example.

Example implementation algorithm

1. Create and configure a C# console application project

Using the Microsoft Visual Studio (version 2017 and up) development environment, create a Visual C# console application project and name it DataServiceDeleteExample. The Target framework project property must be set to .NET Framework 4.7.

In the References section of the project you need to add dependencies of the following libraries:

  • System.Web.Extensions.dll is a class library included in the .NET Framework
  • Terrasoft.Core.dll is a main class library of the application server kernel. Can be found by the following path: [Directory with the installed application]\Terrasoft.WebApp\bin\Terrasoft.Core.dll
  • Terrasoft.Nui.ServiceModel.dll class library the application services. Can be found by the following path: [Directory with the application installed]\Terrasoft.WebApp\bin\Terrasoft.Nui.ServiceModel.dll.
  • Terrasoft.Common.dll is a main class library of the application server kernel. Can be found by the following path: [Directory with the installed application]\Terrasoft.WebApp\bin\Terrasoft.Common.dll.

Add using directives to the application source code file:

Adding 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 field declarations and constants to the application source code

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

Adding the fields and constants
// Main Creatio URL. Has to be changed to a custom one.
private const string baseUri = @"http://example.creatio.com";
// Query string to the Login method of the AuthService.svc service.
private const string authServiceUri = baseUri + @"/ServiceModel/AuthService.svc/Login";
// DeleteQuery query path string.
private const string deleteQueryUri = baseUri + @"/0/DataService/json/reply/DeleteQuery";
// Creatio cookie authentication.
private static CookieContainer AuthCookie = new CookieContainer();

Three string constant fields that are used to carry out the authentication requests and requests to read data are declared here. The authentication data will be stored in the AuthCookie field.

3. Add a method that performs authentication in the Creatio application

You need to authenticate the newly created application to access the DataService web service.

4. Implement a query to add a record

As the previously declared updateQueryUri constant contains the path for sending data in JSON format, the data sent must be pre-configured in the form of a string containing a description of the JSON object corresponding to the UpdateQuery data contract. This can be done directly in a lowercase variable but it is much easier and safer to create an instance of the UpdateQuery class, fill its properties, and then serialize it to a string.

Implementation the request to adding a record
// Query class instance.
var deleteQuery = new DeleteQuery()
{
// Root schema name.
RootSchemaName = "Contact",
// Query filters.
Filters = new Filters()
{
// Filter type – group.
FilterType = Terrasoft.Nui.ServiceModel.DataContract.FilterType.FilterGroup,
// Filter collection.
Items = new Dictionary<string, Filter>()
{
// Filtration by name.
{
// Key.
"FilterByName",
// Value.
new Filter
{
// Filter type – comparison filter.
FilterType = Terrasoft.Nui.ServiceModel.DataContract.FilterType.CompareFilter,
// Comparison type – starts with an expression.
ComparisonType = FilterComparisonType.Equal,
// Expression to be checked.
LeftExpression = new BaseExpression()
{
// Expression type – schema column.
ExpressionType = EntitySchemaQueryExpressionType.SchemaColumn,
// Column path.
ColumnPath = "Name"
},
// Filtration 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 Best"
}
}
}
}
}
}
};
// Class instance serialization of the JSON string adding query.
var json = new JavaScriptSerializer().Serialize(updateQuery);

This creates an instance of the DeleteQuery class. The Contact value is set in the RootSchemaName property. To delete a particular record or group of records, you need to set a link to the correctly initialized Filters class instance to the Filters property. In this case, a single filter that selects only records with the John Best value in the Full name column is added to the filter collection.

In the final step you must perform POST query to the DataService service. 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 in its properties, attach a previously created string with the JSON object to a request, and then execute and process the result of the query to the DataService service. 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 insrance 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

GitHub (example implementation)