Creating a user configuration service
Glossary Item Box
Case description
Create a custom configuration service that returns Id of the contact of the given name. If there are several contacts found, return the Id of the first contact only. If no contacts are found, the service should return an empty string.
Source code
You can download the package with case implementation using the following link.
Case implementation algorithm
1. Creating a [Source code] schema
Perform the [Add] – [Source code] action on the [Schemas] tab of the [Configuration] section.
Fig. 1. Adding the [Source Code] schema
Sett following properties for the schema:
- [Name] – UsrCustomConfigurationService.
- [Title] – UsrCustomConfigurationService.
2. Create class of the service
On the [Source code] tab:
- The namespace nested in the Terrasoft.Configuration. The name can be random, for example, the UsrCustomConfigurationService.
- Namespaces whose data types will be used in your class. For this, use the using directive. A complete list of namespaces is provided in the source code below.
- Class, for example, the UsrCustomConfigurationService class inherited from the Terrasoft.Nui.ServiceModel.WebService.BaseService. Mark the class with the [ServiceContract] and [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)] attributes.
The source code with a class declaration is available below:
3. Implement the methods that match the service endpoints
To implement the endpoint of the return of the contact Id by its name, add the GetContactIdByName(string Name) public string to the class. The Name parameter should receive the name of the contact. After accessing the database via the EntitySchemaQuery the method will return the Id of the first found contact (or empty string) casted to string.
Full source code with the implementation of the service:
namespace Terrasoft.Configuration.UsrCustomNamespace { using System; using System.ServiceModel; using System.ServiceModel.Web; using System.ServiceModel.Activation; using Terrasoft.Core; using Terrasoft.Web.Common; using Terrasoft.Core.Entities; [ServiceContract] [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)] public class UsrCustomConfigurationService: BaseService { // Method returning contact identifier by name. [OperationContract] [WebInvoke(Method = "GET", RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json)] public string GetContactIdByName(string Name) { // Default result. var result = ""; // The EntitySchemaQuery instance, addressing the Contact database table. var esq = new EntitySchemaQuery(UserConnection.EntitySchemaManager, "Contact"); // Adding columns to query. var colId = esq.AddColumn("Id"); var colName = esq.AddColumn("Name"); // Filtering query data. var esqFilter = esq.CreateFilterWithParameters(FilterComparisonType.Equal, "Name", Name); esq.Filters.Add(esqFilter); // Receiving query results. var entities = esq.GetEntityCollection(UserConnection); // If the data are received. if (entities.Count > 0) { // Return the "Id" column value of the first record of the query result. result = entities[0].GetColumnValue(colId.Name).ToString(); // You can also use the below variant: // result = entities[0].GetTypedColumnValue<string>(colId.Name); } // Return result. return result; } } }
After making changes, save and publish the schema.
As a result, the new configuration service UsrCustomConfigurationService will be available in the Creatio. When the GetContactIdByName endpoint of this service is called, for example, out of web browser, the contact Id (Fig. 2) or the empty string (“”) value (Fig. 3) will be returned.
Pay attention to the format of the call result. In the server response, the object that contains property with the name that is a combination of the name of the called method and the “Return” suffix, will be passed. The value of the object property contains the contact Id (or an empty string) returned by the service.
Fig. 2. Request result: The contact Id is found.
Fig. 3. Request result: The contact Id is not found.
If the service is called without logging to the application, the authorization error will be displayed (Fig. 4).
Fig. 4. Request result No authorization
See also: