Portal Engine Questions on portal engine and web parts.
Version 5.x > Portal Engine > Custom functions in Where condition View modes: 
User avatar
Member
Member
Oli Jeffery - 12/13/2010 7:09:40 AM
   
Custom functions in Where condition
Hi,

How can I call a custom function in the WhereCondition of the CMS repeater? Is it even possible?

My function is <%# BishopFlemingFunctions.getWhereSectors(""+ Eval("i_Interests") +"") %>

Thanks, Oli.

User avatar
Kentico Support
Kentico Support
kentico_zdenekc - 12/13/2010 7:44:34 AM
   
RE:Custom functions in Where condition
Hello,

If you want to call custom code to specify WhereCondition property of the CMS Repeater, you can do it e.g. through custom macro placed directly in the Properties dialog of the Repeater webpart.
In the code of the macro, you could call your code.

However, it seems that you're using Eval function to pass a parameter to your function, which suggests placement in the transformation... is that correct?
Are you using nested repeater in transformation, or do you want to set the main repeater's where condition?

In case of nested repeater, you would need to use code-behind script in transformation to reload its data, so that the transformation could look like:


.....
<cc1:CMSRepeater runat="server" ID="NewsRepeater" ClassNames="CMS.News"
TransformationName="CMS.News.NewsWithSummary"
WhereCondition='<%# YourFunction(Eval("somedynamicvalue")) %>' />
......

<script runat="server">
protected void Page_PreRender(object sender, EventArgs e)
{
NewsRepeater.ReloadData(true);
}
</script>

You can also set the WhereCondition in the code-behind <script> block...

Would this be helpful for your aim?

Regards,
Zdenek

User avatar
Member
Member
Oli Jeffery - 12/13/2010 9:12:37 AM
   
RE:Custom functions in Where condition
Hi,

Thanks for your response.

I’ve tried doing it a couple of different ways, but at the minute I’m going with a normal cms repeater (i.e. setting the value in the main repeater).

I have a one document (Investor) which has a multi value i_Interests field. I’m running a repeater which looks for another document type (Targets) which has a single value t_Sector field. Currently my WhereCondition is '{%i_Interests%}' LIKE t_Sector which works fine as long as i_Interests only has one value – but if it has multiple values doesn’t work.

I therefore tried a custom function to split the i_Interests field down more sensibly:

public static string getWhereSectors(string FieldToSplit) {
string CorrectString = "t_Sector LIKE '%" + FieldToSplit.Replace("|", "%' OR t_Sector LIKE '%") + "%'";
return CorrectString.ToString();
}

How would I call that from a Macro using {%i_Interests%}?

Thanks, Oli.

User avatar
Kentico Support
Kentico Support
kentico_zdenekc - 12/14/2010 5:51:49 AM
   
RE:Custom functions in Where condition
Hi,

To summarize our parallel conversation and provide information for others:

It is possible to use macro with parameters or the custom function.
Custom macro code could pick the document's custom field value (i_Interests) from the context... e.g. using:
...
string FieldToSplit = ValidationHelper.GetString(CurrentDocument.GetValue("i_Interests"),"");
// do whatever you need
// set result= corrected expression
....

However, if the key trick is the replace method, then in Kentico CMS version 5.5 or higher, it could be performed also through macro parameter (replace), please see details at:

devnet.kentico.com/Blogs/Martin-Hejtmanek/June-2010/New-in-5-5--Macro-parameters.aspx

Then, your where condition could be something like:

t_Sector LIKE '%{%i_Interests|(replace)\|(with)%' OR t_Sector LIKE '%%}%'


What might be a problem is that our webparts use SQL injection protection that unfortunately doubles the quotes in the wherecondition string.

Each web part should inherit from CMSAbstractWebPart class, like Repeater web part. This CMSAbstractWebPart class contains definition for SQLProperties (this property is intended for SQL injection prevention). So, if you are using CMS Repeater web part, please try to use following code in SetupControl method of that web part (CMSWebParts\Viewers\(Documents)\cmsrepeater.ascx.cs):

this.SQLProperties = "";


Similar issue was recently discussed in our forums here, the above text quotes Miro's explanation.

Please also note, we would recommend to use cloned version of the repeater, so that you know that a the particular one has SQL injection protection disabled, and you won't loose the change during future upgrades.

Regards,
Zdenek