Creatio development guide
PDF
This documentation is valid for Creatio version 7.16.0. We recommend using the newest version of Creatio documentation.

Retrieving data based on user permissions

Glossary Item Box

Examples of using EntitySchemaQuery to retrieve data

You can download the package with the configuration web service implementing the cases described below using the following link.

Example 1

Creating the EntitySchemaQuery instance
 
 
public string CreateESQ()
{
    var result = "";
    // Receiving an instance of the object schema manager.          
    EntitySchemaManager esqManager = SystemUserConnection.EntitySchemaManager;
    // Receiving an instance of the schema that will be the root schema for the created
    // EntitySchemaQuery instance.
    var rootEntitySchema = esqManager.GetInstanceByName("City") as EntitySchema;
    // Creating the EntitySchemaQuery instance that will have
    // rootEntitySchema as the root schema.
    var esqResult = new EntitySchemaQuery(rootEntitySchema);
    // Adding columns that will be selected in the result request.
    esqResult.AddColumn("Id");
    esqResult.AddColumn("Name");
    // Receiving the Select instance associated with the created EntitySchemaQuery request.
    Select selectEsq = esqResult.GetSelectQuery(SystemUserConnection);
    // Receiving the text of the resulting request of the created EntitySchemaQuery instance.
    result = selectEsq.GetSqlText();
    return result;
}

Example 2

Creating a clone of the EntitySchemaQuery instance
 
 
public string CreateESQClone()
{
    var result = "";
    EntitySchemaManager esqManager = SystemUserConnection.EntitySchemaManager;
    var esqSource = new EntitySchemaQuery(esqManager, "Contact");
    esqSource.AddColumn("Id");
    esqSource.AddColumn("Name");
    // Creating an EntitySchemaQuery instance that is a clone of the esqSource instance.
    var esqClone = new EntitySchemaQuery(esqSource);
    result = esqClone.GetSelectQuery(SystemUserConnection).GetSqlText();
    return result;
}

Example 3

Receiving the result of query execution
 
 
public string GetEntitiesExample()
{
    var result = "";
    // Creating a request to the City schema, adding the Name column to the request.
    var esqResult = new EntitySchemaQuery(UserConnection.EntitySchemaManager, "City");
    var colName = esqResult.AddColumn("Name");

    // Running the request to the database and receiving the resulting object collection.
    var entities = esqResult.GetEntityCollection(UserConnection);
    for (int i=0; i < entities.Length; i++) {
        result += entities[i].GetColumnValue(colName.Name).ToString();
        result += "\n";
    }
    
    // Running the request to the database and receiving an object with the specified identifier.
    var entity = esqResult.GetEntity(UserConnection, new Guid("100B6B13-E8BB-DF11-B00F-001D60E938C6"));
    result += "\n";
    result += entity.GetColumnValue(colName.Name).ToString();
    return result;
}

Example 4

Example of working with the EntitySchemaQuery cache
 
 
public Collection<string> UsingCacheExample()
{
    // Creating a request to the City schema, adding the Name column to the request.
    var esqResult = new EntitySchemaQuery(UserConnection.EntitySchemaManager, "City");
    esqResult.AddColumn("Name");

    // Identifying the key that will be used to store the request results in the cache.
    // Creatio cache of the local data cache session level acts as a cache (since the
    // Cache property of the object cannot be identified).
    esqResult.CacheItemName = "EsqResultItem";

    // Collection that will contain the request results.
    var esqCityNames = new Collection<string>();

    // Collection that will contain the cached request results.
    var cachedEsqCityNames = new Collection<string>();

    // Running the requst to the database and receiving the resulting object collection.
    // After running the operation the results will be cached.
    var entities = esqResult.GetEntityCollection(UserConnection);

    // Processing the request results and populating the esqCityNames collection.
    foreach (var entity in entities)
    {
        esqCityNames.Add(entity.GetTypedColumnValue<string>("Name"));
    }

    // Receiving a link to the cache of the esqResult request using the CacheItemName key as a data table in memory.
    var esqCacheStore = esqResult.Cache[esqResult.CacheItemName] as DataTable;

    // Populating the cachedEsqCityNames collection with the values of the request cache.
    if (esqCacheStore != null)
    {
        foreach (DataRow row in esqCacheStore.Rows)
        {
            cachedEsqCityNames.Add(row[0].ToString());
        }
    }
    return cachedEsqCityNames;
}

Example 5

Using additional settings of the query
 
 
public Collection<string> ESQOptionsExample()
{
    // Creating a request instance with the City root schema.
    var esqCities = new EntitySchemaQuery(UserConnection.EntitySchemaManager, "City");
    esqCities.AddColumn("Name");

    // Creating a request with the Country root schema.
    var esqCountries = new EntitySchemaQuery(UserConnection.EntitySchemaManager, "Country");
    esqCountries.AddColumn("Name");

    // Creating an instance of settings to return the first 5 strings by the request.
    var esqOptions = new EntitySchemaQueryOptions()
    {
        PageableDirection = PageableSelectDirection.First,
        PageableRowCount = 5,
        PageableConditionValues = new Dictionary<string, object>()
    };

    // Receiving a city collection that will contain the first 5 cities of the resulting data set.
    var cities = esqCities.GetEntityCollection(UserConnection, esqOptions);

    // Receiving a country collection that will contain the first 5 countries of the resulting data set.
    var countries = esqCountries.GetEntityCollection(UserConnection, esqOptions);
        var esqStringCollection = new Collection<string>();
    foreach (var entity in cities)
    {
        esqStringCollection.Add(entity.GetTypedColumnValue<string>("Name"));
    }
    foreach (var entity in countries)
    {
        esqStringCollection.Add(entity.GetTypedColumnValue<string>("Name"));
    }
    return esqStringCollection;
}

See also:

© Creatio 2002-2020.

Did you find this information useful?

How can we improve it?