none
CAML query - is it possible to write an expression without reference to the field?

    Question

  • I have CAML query in SelectCommand of SPDataSource data source. Let it be: <Where><Eq><FieldRef Name="Field1"/><Value Type="Integer">{Param1}</Value></Eq></Where>
    I have drop down list with different values which when CallBack occurs are placed into Param1 of the data source. This is a filter effect!
    But now I am adding an "View All" option to the drop down list - I want to see all records. I am adding HiddenField which can be TRUE or FALSE: if user chooses "View All" value, HF = TRUE if any other value, HF = FALSE.
    I suppose the expression "<Or><Something=TRUE/FALSE><Eq>...see above...</Eq>" must work as a switch (on/off) for my filter. But I don't know how to realize "Somthing" in CAML. It can not be a field - it is jaust logical expression! I can not find any appropriate CAML instruction for my purpose. All CAML instructions reference to list fields.
    Tuesday, January 17, 2017 9:00 AM

Answers

  • Hi Denis Prokofjev,

    In CAML query, you only can use the fields in the list and values. You cannot define variables not the fields in the list such as “something” in your post.

    There is a method. You could create a field for “something” and stores value in the field. Then you can use the field in the CAML query.

    More reference:

    Query Schema.

    https://msdn.microsoft.com/en-us/library/office/ms467521.aspx

    There is a workaround. You could use CAML query and JavaScript code. You could define variable in JavaScript, then use different CAML query string according to the variable.

    For more detailed information, you could refer to the article below.

    How to do a CAML Query on SharePoint 2013 List by JavaScript.

    http://www.sharepointpals.com/post/How-to-do-a-CAML-Query-on-SharePoint-2013-List-by-JavaScript

    Best regards,

    Sara Fan


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Wednesday, January 18, 2017 2:27 PM
    Moderator

All replies

  • Hi Denis Prokofjev,

    In CAML query, you only can use the fields in the list and values. You cannot define variables not the fields in the list such as “something” in your post.

    There is a method. You could create a field for “something” and stores value in the field. Then you can use the field in the CAML query.

    More reference:

    Query Schema.

    https://msdn.microsoft.com/en-us/library/office/ms467521.aspx

    There is a workaround. You could use CAML query and JavaScript code. You could define variable in JavaScript, then use different CAML query string according to the variable.

    For more detailed information, you could refer to the article below.

    How to do a CAML Query on SharePoint 2013 List by JavaScript.

    http://www.sharepointpals.com/post/How-to-do-a-CAML-Query-on-SharePoint-2013-List-by-JavaScript

    Best regards,

    Sara Fan


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Wednesday, January 18, 2017 2:27 PM
    Moderator
  • Thank you for your response, Sara! I've understood that CAML can't help me :). Some days earlier I did my own workaround with the help of additional fields in lists. I think this is the option 1 in your answer. I did the following query in SPDataSource objects:

    <Query><Where>
    <And>
    <Eq><FieldRef Name='pvPartnerRef' LookupId='TRUE'/><Value Type='Integer'>{prmPartnerId}</Value></Eq>
    <Or>
    <Eq><FieldRef Name='pvFilterAllColumn' /><Value Type='Integer'>{prmAllYears}</Value></Eq>
    <And>
    <Geq><FieldRef Name='pvDocumentDate' /><Value Type='DateTime'>{prmYearFrom}</Value></Geq>
    <Leq><FieldRef Name='pvDocumentDate' /><Value Type='DateTime'>{prmYearTo}</Value></Leq>
    </And>
    </Or>
    </And>
    </Where></Query>

    pvFilterAllColumn - is a "key" that help to fetch all items or nothing! But this field must have the same value for every list item.

    About your 2-nd workaround - it is not exactly what I want. I use DataFormWebPart, DFWP uses AggregateDataSourse. I think SP.CamlQuery object is not suitable here. But if SPDataSource (which is inside AggregateDataSource) can use parameter value as SelectCommand -  such attempt can help!


    Wednesday, January 18, 2017 3:43 PM
  • Hi Denis Prokofjev,

    It is very happy that you have found the workaround.

    Thank you for your sharing and it will help others have the same issue.

    Best regards,

    Sara Fan

    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com


    Thursday, January 19, 2017 12:46 AM
    Moderator
  • I have found another more convenient decision for my purpose. This is a dynamically assigning a query string to SelectCommand property of SPDataSource object. The sequence is similar:

    1. JS function which changes the Value attribute of HiddenField. The value is a CAML query string. You would like to set up the Value by default query string. This step gives you the default selection on the first page load.

    <asp:HiddenField ID="pvCamlQuery" Value="<View><Query><Where><Eq><FieldRef Name='pvPartnerRef' LookupId='TRUE' /><Value Type='Integer' >{prmPartnerId}</Value></Eq></Where></Query></View>" runat="server" />

    2. You must add a new parameter in the DFWP ParameterBindings: 

    <ParameterBindings>
    ...
    <ParameterBinding Name="wpprmCamlQuery" Location="Control(pvCamlQuery,Value)" DefaultValue="" />
    </ParameterBindings>

    3. You should to pass this parameter in the SPDataSource SelectParameters:

    <SelectParameters>...<WebPartPages:DataFormParameter ParameterKey="wpprmCamlQuery" PropertyName="ParameterValues" DefaultValue="" Name="prmCamlQuery"></WebPartPages:DataFormParameter>

    4. Now you can use it in SelectCommand of you data source:

    SelectCommand="{prmCamlQuery}"


    Thursday, January 26, 2017 7:55 AM
  • Hi Denis Prokofjev,

    Thank you for your sharing.

    Best regards,

    Sara Fan

    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Thursday, January 26, 2017 8:02 AM
    Moderator