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





Hi, Ryan!
How i can get folder filters ids at freedom UI for use it at some module or process?
If you’re referring to getting the selected filters for a Freedom UI list, I’m not sure, but I am pretty sure there is likely an attribute for those that you could find via debugging.
When adapting this code, why might I get the below error pointing at UserConnection.EntitySchemaManager in the line `var folderSchema = UserConnection.EntitySchemaManager.GetInstanceByName(sectionName + “Folder”);`
Error: An object reference is required for the non-static field, method, or property ‘UserConnection.EntitySchemaManager’
Has something changed around UserConnection and its properties? Or does my C# class have to inherit from something it doesn’t currently inherit from?
It sounds like the UserConnection is null. Where are you using this code? A configuration web service, an anonymous service, or process script task, and if so, running from a timer? How is the UserConnection being retrieved?
Ryan
I was trying to use it in some general C# class, but I see now that this way of getting an EntitySchemaManager is actually using a Property called UserConnection defined on the class rather than accessing a static property of the UserConnection class. Do you have any ways of getting a UserConnection/SystemUserConnection/AppConnection in a general C# class? Rather than having to have it passed in some way such as through a constructor or method’s parameters?
In my current use case, I need to run some server ESQ when a class is initialised. The class inherits from the base class BaseEntityEventListener.
For a C# class, you’d typically have a constructor that has a param for a UserConnection that you’d pass into the class when you use the class. However, for an Entity event listener, the UserConnection is passed in via the EventArgs argument to the method you’re overriding.
Essentially, something like this:
public override void OnDeleted(object sender, EntityAfterEventArgs e) { base.OnDeleted(sender, e); var entity = (Entity)sender; var userConnection = entity.UserConnection; }