Forum Discussion

badar_000's avatar
badar_000
Copper Contributor
Aug 07, 2022
Solved

CAML Query to filter library not working as expected

below the is the CAML query i am using in my code for fetching data and binding to DATALIST control...

 

 _query.Query = "<View><Query><Where><And><Eq><FieldRef Name='Description0' /><Value Type='Text'>" + myParam.ToString() + "</Value></Eq></And><And><Geq><FieldRef Name='Modified' /><Value IncludeTimeValue='FALSE' Type='DateTime'><Now /></Value></Geq></And></Where><OrderBy><FieldRef Name='Modified' Ascending='True' /></OrderBy></Query></View>";

 

from above query i am getting result but not as expected , means my condition to fetch based on datetime not working and also on description.

 

please help.

 

  • kalpeshvaghela ganeshsanap 

     

    Dear All, I used below format and it worked, thanks for your support... I am sharing below CAML code...

    string Iso8601Today = XmlConvert.ToString(DateTime.Now, XmlDateTimeSerializationMode.Local);

     

    _query.ViewXml = "<View><Query>" +
    "<Where>" +

    "<Leq>" +
    "<FieldRef Name='testDate'/>" +
    "<Value IncludeTimeValue='FALSE' Type='DateTime'>"+Iso8601Today+"</Value>" +
    "</Leq>" +
    "</Where>" +
    "<OrderBy><FieldRef Name ='Created' Ascending = 'True' /></OrderBy></Query></View>";

     

    I got the required result, please add your comment for more optimization ...

     

    regards

    Badar

17 Replies

  • badar_000 

     

    You should use two condition in AND tag and here you need only one AND tag according to your requirement. Please use below query and it should work

     

    <View>
    	<Query>
    		<Where>
    			<And>
    				<Eq>
    					<FieldRef Name='Description0' />
    					<Value Type='Text'>You Value</Value>
    				</Eq>
    				<Geq>
    					<FieldRef Name='Modified' />
    					<Value IncludeTimeValue='FALSE' Type='DateTime'>
    						<Now />
    					</Value>
    				</Geq>
    			</And>
    		</Where>
    		<OrderBy>
    			<FieldRef Name='Modified' Ascending='True' />
    		</OrderBy>
    	</Query>
    </View>

     

    CAML Query Documentation: https://docs.microsoft.com/en-us/sharepoint/dev/schema/query-schema

     

    Hope it will help to you. 

     

     

     

    • badar_000's avatar
      badar_000
      Copper Contributor

      kalpeshvaghela 

       

      I tried below after your comments but result is same , thanks for your reply..

       

      _query.Query = "<View><Query>" +
      "<Where><And>" +
      "<Eq><FieldRef Name='Description0' /><Value Type='Text'>" + myParam.ToString() + "</Value></Eq>" +
      "<Geq><FieldRef Name='Modified'/><Value IncludeTimeValue='FALSE' Type='DateTime'><Now /></Value></Geq>" +
      "</And>" +
      "</Where>" +
      "<OrderBy><FieldRef Name ='Created' Ascending = 'True' /></OrderBy></Query></View>";

       

       

      • kalpeshvaghela's avatar
        kalpeshvaghela
        Iron Contributor

        badar_000 

         

        What is the type of Description0 field? if it's multiline of text then use below query.

         

        <View>
        	<Query>
        		<Where>
        			<And>
        				<Eq>
        					<FieldRef Name='Description0' />
        					<Value Type='Note'>You Value</Value>
        				</Eq>
        				<Geq>
        					<FieldRef Name='Modified' />
        					<Value IncludeTimeValue='FALSE' Type='DateTime'>
        						<Now />
        					</Value>
        				</Geq>
        			</And>
        		</Where>
        		<OrderBy>
        			<FieldRef Name='Modified' Ascending='True' />
        		</OrderBy>
        	</Query>
        </View>

Resources