A useful way to allow a user to control the filters for some server-side operation or process is to allow them to select a folder for your back end code to use for filtering the data. There’s a few things you need to do to retrieve the filters for a folder. This article will provide some reusable code for you to retrieve the filters for a section folder to use as filters for a new EntitySchemaQuery of your own.
To retrieve the filter conditions for a folder, you’ll need to get the folder’s binary SearchData column data, the encode to a string, then deserialize them, and finally convert the deserialized filters to valid EntitySchemaQuery filters. Here’s some reusable code you can use to do all of this. To use, change the first two lines to the section the folder is for and the Id of the folder itself (for example, if this is an Account section folder, the Id of the folder from the AccountFolder table). Also, make note that you’ll also need to include a using directive for the Terrasoft.Nui.ServiceModel.Extensions – which provides the BuildEsqFilter extension method we’re using in the C# code below. Note, all you should need to change are the first two lines of code, the rest can stay as-is (up until the comment with the —— that uses the filters as a sample)
// set these as needed for the section and the Id of the folder var sectionName = "Account"; var folderId = someAccountFolderId; // get folder SearchData var folderSchema = UserConnection.EntitySchemaManager.GetInstanceByName(sectionName + "Folder"); var esq = new EntitySchemaQuery(folderSchema); var dataCol = esq.AddColumn("SearchData").Name; var folderData = esq.GetEntity(UserConnection, folderId).GetBytesValue(dataCol); // convert filter data to esq filters var serializedFilters = System.Text.Encoding.UTF8.GetString(folderData, 0, folderData.Length); var dataSourceFilters = Terrasoft.Common.Json.Json.Deserialize<Terrasoft.Nui.ServiceModel.DataContract.Filters>(serializedFilters); // MUST INCLUDE using Terrasoft.Nui.ServiceModel.Extensions; var folderFilters = dataSourceFilters.BuildEsqFilter(UserConnection.EntitySchemaManager.GetInstanceByName(sectionName).UId, UserConnection); // --------------------------------------------------- // now can include folderFilters as filters in new esq var accountEsq = new EntitySchemaQuery(UserConnection.EntitySchemaManager, "Account"); accountEsq.AddColumn("Id"); accountEsq.Filters.Add(folderFilters); // using the filters from the folder var accounts = accountEsq.GetEntityCollection(UserConnection);
Note, the comment in the code about including a using directive for Terrasoft.Nui.ServiceModel.Extensions. If you’re using this in a source code schema, simply add the using directive. If you’re using this in a process, you’ll need to add this on the Methods tab of the process properties, as shown below