Active Query Builder support area

Using events to add, modify and remove objects being loaded to the Metadata Container

Last modified:


One of the possible ways to alter metadata being loaded by Active Query Builder from a database is to define handlers for events of Metadata Container which will fire on loading child items for nodes of Metadata Container.

Below are the sample handlers of the MetadataContainer.ItemMetadataLoading and the MetadataContainer.ItemMetadataLoaded events that perform pre and post-processing on requesting child metadata for a Table metadata item.

Adding new objects programmatically

The ItemMetadataLoading event is fired when the Metadata Container is about to load child items for a node.

Note that adding child objects of a particular type will prevent the automatic loading of child objects of that type for this node.

The following sample defines two fields "Field 1" and "Field 2" for the "Demo Table" object.

queryBuilder1.MetadataContainer.ItemMetadataLoading += MetadataContainer_ItemMetadataLoading;

void MetadataContainer_ItemMetadataLoading(object sender, MetadataItem item, MetadataType loadTypes)
{
	if (item.Type == MetadataType.Table && item.Name == "Demo Table")
	{
		MetadataField pk = item.AddField("Field 1");
		pk.FieldTypeName = "int";
		pk.PrimaryKey = true;

		MetadataField f2 = item.AddField("Field 2");
		f2.FieldTypeName = "nvarchar";
		f2.Size = 30;

		item.AddField("Field 3").FieldTypeName = "datetime";
	}
}

Removing unwanted items, modifying properties of automatically loaded child objects

The ItemMetadataLoaded event is fired when child objects have just been loaded for a node. 

Note that in this event handler you must modify not the item itself, but its child items.

The following sample removes the "Unwanted field" field and defines alternate names for the rest of the loaded fields of the "Demo Table" object (see the user-friendly names for objects article for details). 

queryBuilder1.MetadataContainer.ItemMetadataLoaded += MetadataContainer_ItemMetadataLoaded;

private void MetadataContainer_ItemMetadataLoaded(object sender, MetadataItem item, MetadataType loadtypes)
{
    if (item.Type == MetadataType.Table && item.Name == "Demo Table")
    {

        var field = item.Items.Fields.FindItem("Unwanted Field");

        if (field != null) 
        {
            item.Items.Remove(field);
        }

        foreach (MetadataField field in item.Items.Fields)
        {
            field.AltName = field.Name.ToUpper();
        }
    }
}

Is this article helpful for you?