API
Version 7.x > API > How to call and apply transformation using API on custom data table row View modes: 
User avatar
Member
Member
webdeveloper-avastonetech - 1/31/2014 10:59:56 AM
   
How to call and apply transformation using API on custom data table row
Could use some help, in the code behind using the API, how can i take an either IDataClass or DataRow of a custom data table and apply a transformation on it? I want to pass the object through some predefined transformations to format the output in some custom items. Thanks so much, stumped on this one.

User avatar
Kentico Legend
Kentico Legend
Brenden Kehren - 2/6/2014 12:00:55 PM
   
RE:How to call and apply transformation using API on custom data table row
You wouldn't apply the transformation to the dataset at all, you'd assign it to a control that will render the data. For instance, put a repeater on the page and assign the datasource to be your IDataClass. Then you set ItemTemplate like so:
repeater.ItemTemplate = CMSDataProperties.LoadTransformation(this, "your.item.transformationname", true);

User avatar
Kentico MVP
Kentico MVP
tfayas-avastonetech - 2/6/2014 12:41:37 PM
   
RE:How to call and apply transformation using API on custom data table row
I was more looking for back end transformation to get a string back, however i found a work around that is working quite nicely, i'll share!

This uses MacroResolvers, which basically you can put your IDataClass as a Macro Resolver and then feed the "Template" to the MacroResolver to render. I stored the template strings in a database that the customer (or myself) can modify without needing to touch back end code.

Step 1: Create the Macro Resolver, filling it with the IDataClass sets
    public static ContextResolver getContextResolver(int ItemID, int OtherItemID)
{
ContextResolver resolver = CMSContext.CurrentResolver.CreateContextChild();
// Get any IDataClass sets you want to "Load" into your resolver
IDataClass MyDatabaseObject = Custom.GetTheObject(ItemID);
IDataClass MyOtherDatabaseObject = Custom.GetTheOtherObject(OtherItemID)

// set the macro callers up with the object data
resolver.SetNamedSourceData("MainInfo", MyDatabaseObject);
resolver.SetNamedSourceData("OtherInfo", MyOtherDatabaseObject);

// Now if a macro is passed to the resolver, like "Hello {% MainInfo.Name %}" it will replace the
// MainInfo.Name with the Name column of that object loaded into it, in this case the MyDatabaseObject
return resolver;
}

Step 2: Grab the template string, i did from a database that has Name, Template
    public static string getOtherTemplate(ref ContextResolver objectResolver, string templateName)
{
// Fill the resolver with custom data
// resolver.SourceParameters = new object[,] { { "MySimpleValue", "RESOLVED Simple value!" }, { "MySecondSimpleValue", "RESOLVED Second simple value!" } };
// Use the resolver to resolve macros in text
return objectResolver.ResolveMacros(getOtherTemplateMacro(templateName));
}

public static string getOtherTemplateMacro(string templateName)
{
QueryDataParameters parameters = new QueryDataParameters();
parameters.Add("@TemplateName", templateName);
DataSet ds = SqlHelperClass.ExecuteQuery("custom.templates.SelectTemplateByName", parameters, null, null);
if (ds.Tables[0].Rows.Count == 0) return "No template found with name " + templateName;
else return ds.Tables[0].Rows[0]["Template"].ToString();
}

To execute, simply use the following then
ContextResolver myResolver = Custom.getResolver(1, 2);
String results = Custom.getOtherTemplate(ref MyResolver, "SomeTemplateNameThatMatchesTheNameColumnInDatabase");

Then just use K# syntax in the Database template field and you're set! Works quite well actually...

User avatar
Certified Developer 13
Certified Developer 13
kentico_josefd - 2/12/2014 10:29:42 AM
   
RE:How to call and apply transformation using API on custom data table row
Hello,

As FroggEye said, Kentico do not offer the possibility of applying ASCX transformations directly on data, they are used exclusively for output controls such as Repeaters.

The solution you have found, using macros in text transformations, is the only way to achieve functionality similar to formatting input data using an ASCX transformation.

Regards,
Josef Dvorak