Use existing SPView.Query and append to <Where>...</Where>

Apr 23, 2012 at 12:14 AM
Edited Apr 23, 2012 at 12:16 AM

Hi,

I would like to know if it is possible to use the value from an existing SPView.Query string value and dynamically append to the <Where>....</Where> (see #1) if there is one or create the appropriate <Where>...</Where> (see #2) if there is not one. To add clarity, I am looking to see if the Reverse Engineering feature(s) can be leveraged somehow or if there is another way.

Query #1: Original SPView.Query.Value

<OrderBy><FieldRef Name="crmOrderPriority" /><FieldRef Name="crmOrderRank" /></OrderBy>
<Where><Neq><FieldRef Name="crmOrderPositionStatus" /><Value Type="Text">Closed</Value></Neq></Where>

Query #1: New CAML generated dynamically based on expressions etc. which "include" the original Where expressions.

<OrderBy><FieldRef Name='crmOrderPriority' /><FieldRef Name='crmOrderRank' /></OrderBy>
<Where>
<And>
<And>
<Neq><FieldRef Name='crmOrderPositionStatus'/><Value Type='Text'>Closed</Value></Neq>
<Eq><FieldRef Name='crmOrderRank'/><Value Type='Text'>1</Value></Eq>
</And>
<Eq><FieldRef Name='crmOrderPositionStatus'/><Value Type='Text'>Covered</Value></Eq>
</And>
</Where>

 Query #2: Original SPView.Query.Value

<OrderBy><FieldRef Name="crmOrderPriority" /><FieldRef Name="crmOrderRank" /></OrderBy> 

Query #2: New CAML generated dynamically based on expressions etc.

<OrderBy><FieldRef Name="crmOrderPriority" /><FieldRef Name="crmOrderRank" /></OrderBy>
<
Where>
<And>
<And>
<Neq><FieldRef Name='crmOrderPositionStatus'/><Value Type='Text'>Closed</Value></Neq>
<Eq><FieldRef Name='crmOrderRank'/><Value Type='Text'>1</Value></Eq>
</And>
<Eq><FieldRef Name='crmOrderPositionStatus'/><Value Type='Text'>Covered</Value></Eq>
</And>
</Where>

 

Thank you in advance for any input and/or assistance. 

Coordinator
Apr 23, 2012 at 7:05 PM

Hi lawalker,

very interesting idea. Such trick is not supported currently. However I believe that it is possible to do with reverse engineering. If we will check implementation of ReQuery.ToExpression() method we will see that "where" part of query is translated separately:

 

public Expression ToExpression()
{
    var translator = this.translatorFactory.Create(input);
    var where = translator.TranslateWhere();
    var orderBy = translator.TranslateOrderBy();
    var groupBy = translator.TranslateGroupBy();
    var viewFields = translator.TranslateViewFields();

    var linker = this.linkerFactory.Create(translator);
    return linker.Link(where, orderBy, groupBy, viewFields);
}

it has type LamdbaExpression. You can try to access its Body property, cast it to appropriate type (if needed) and add additional conditions to it. Then pass it back to Camlex and get string representation. I will think how to add this feature in future releases. So it will be very interesting to get updates from you about this feature if you will be able to get it work.

 

Coordinator
Jun 12, 2012 at 10:24 PM

Starting with version 3.2 it is possible to add conditions to existing query: http://sadomovalex.blogspot.fi/2012/06/camlexnet-32-add-expressions-to.html.

Feb 19, 2014 at 12:58 PM
It works, thanks, but this approach kills my <OrderBy> block in base query and result query doesn`t have any <OrderBy> element.
Please fix it.
Coordinator
Feb 24, 2014 at 6:13 PM
hi wideserg,
I've created new issue based on your comment: https://camlex.codeplex.com/workitem/14003, so it won't be lost. Will be back to it when will have some free time.