SOLVED

CAML Query with two conditions not working

%3CLINGO-SUB%20id%3D%22lingo-sub-1008486%22%20slang%3D%22en-US%22%3ECAML%20Query%20with%20two%20conditions%20not%20working%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1008486%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20all.%20I%20am%20very%20new%20to%20CAML%20Quey%20in%20SharePoint%2C%20but%20I%20want%20to%20make%20a%20query%20in%20a%20highlighted%20content%20web%20part%2C%20with%20two%20conditions%2C%20and%20it%20is%20not%20working%20%3A(.%20If%20I%20only%20set%20one%20condition%2C%20then%20the%20results%20shows%20up%20fine%2C%20but%20two%20conditions%2C%20and%20nothing%20happens.%20This%20is%20my%20query%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CVIEW%3E%3CBR%20%2F%3E%3CQUERY%3E%3CBR%20%2F%3E%3CORDERBY%3E%3CBR%20%2F%3E%3CFIELDREF%20name%3D%22%26quot%3BTitle%26quot%3B%26lt%3BBR%22%3E%3C%2FFIELDREF%3EAscending%3D%22TRUE%22%26gt%3B%3C%2FORDERBY%3E%3CBR%20%2F%3E%3C%2FQUERY%3E%3CBR%20%2F%3E%3CWHERE%3E%3CBR%20%2F%3E%3CAND%3E%3CBR%20%2F%3E%3CBEGINSWITH%3E%3CFIELDREF%20name%3D%22%26quot%3BTitle%26quot%3B%22%3E%3C%2FFIELDREF%3E%3CVALUE%20type%3D%22%26quot%3BText%26quot%3B%22%3EI-0%3C%2FVALUE%3E%3C%2FBEGINSWITH%3E%3CBR%20%2F%3E%3CBEGINSWITH%3E%3CFIELDREF%20name%3D%22%26quot%3BTitle%26quot%3B%22%3E%3C%2FFIELDREF%3E%3CVALUE%20type%3D%22%26quot%3BText%26quot%3B%22%3EI-1%3C%2FVALUE%3E%3C%2FBEGINSWITH%3E%3CBR%20%2F%3E%3C%2FAND%3E%3CBR%20%2F%3E%3C%2FWHERE%3E%3CBR%20%2F%3E%3C%2FVIEW%3E%3CBR%20%2F%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CP%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ECan%20anyone%20guide%20mee%20in%20where%20I%20am%20doing%20something%20wrong%3F%20I%20have%20tried%20with%20the%20Or%20condition%20as%20well%2C%20but%20that%20did%20not%20help%20%3A(.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHope%20some%20of%20you%20hard%20coders%20can%20figure%20this%20out.%3C%2FP%3E%3CP%3ETHX%20in%20advance.%3C%2FP%3E%3CP%3E%2FFrank%20Nielsen%2C%20Denmark%3C%2FP%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1008486%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EDeveloper%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1010308%22%20slang%3D%22en-US%22%3ERe%3A%20CAML%20Query%20with%20two%20conditions%20not%20working%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1010308%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F187265%22%20target%3D%22_blank%22%3E%40Frank%20Nielsen%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHi%20Frank%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ENot%20sure%20which%20version%20of%20SharePoint%20you%20are%20usingQuery%20looks%20OK%20for%20me%20%2CAND%20condition%20wont%20work.%3C%2FP%3E%3CP%3EMake%20sure%20you%20use%20OR%20condition%2CHowever%20we%20can%20design%20the%20CAML%20Query%20using%20the%20UI%20Tools%20or%20google%26nbsp%3B%20with%20CAML%20Query%20Builder%20%2FCAML%20Designer%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Ehere%20is%20the%20link%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fwww.biwug.be%2Fresources%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fwww.biwug.be%2Fresources%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20hope%20this%20works%20for%20Sharepoint%20online%20too%2Cjust%20check%20it.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1010446%22%20slang%3D%22en-US%22%3ERe%3A%20CAML%20Query%20with%20two%20conditions%20not%20working%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1010446%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F453843%22%20target%3D%22_blank%22%3E%40O365Developer%3C%2FA%3E%26nbsp%3BTHX%20for%20quick%20reply.%20I%20am%20sorry%20I%20did%20not%20mention%20wich%20version%20of%20SharePoint%20I%20am%20using.%20It%20is%20SharePoint%20Online%2C%20and%20I%20was%20thinking%20that%20the%20highlitetd%20webpart%20in%20Sharepoint%20Online%20is%20not%20supporting%20two%20CAML%20conditions.%20I%20have%20seen%20a%20lot%20with%20SharePoint%20through%20the%20years%2C%20so%20I%20would%20not%20be%20surprised%26nbsp%3B%3CIMG%20class%3D%22lia-deferred-image%20lia-image-emoji%22%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Fhtml%2Fimages%2Femoticons%2Fcryingwhilelaughing_anim.gif%22%20alt%3D%22%3Acryingwithlaughter%3A%22%20title%3D%22%3Acryingwithlaughter%3A%22%20%2F%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20will%20try%20your%20suggestion%20and%20use%20the%20Or%20function%20instead.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%2FFrank%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1010455%22%20slang%3D%22en-US%22%3ERe%3A%20CAML%20Query%20with%20two%20conditions%20not%20working%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1010455%22%20slang%3D%22en-US%22%3E%3CP%3EHi%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F453843%22%20target%3D%22_blank%22%3E%40O365Developer%3C%2FA%3E%26nbsp%3BThe%20%22Or%22%20did%20the%20trick.%20Thanks%20a%20lot.%20I%20am%20happy%20happy%26nbsp%3B%3CIMG%20class%3D%22lia-deferred-image%20lia-image-emoji%22%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Fhtml%2Fimages%2Femoticons%2Flaugh_40x40.gif%22%20alt%3D%22%3Alol%3A%22%20title%3D%22%3Alol%3A%22%20%2F%3E%3CIMG%20class%3D%22lia-deferred-image%20lia-image-emoji%22%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Fhtml%2Fimages%2Femoticons%2Flaugh_40x40.gif%22%20alt%3D%22%3Alol%3A%22%20title%3D%22%3Alol%3A%22%20%2F%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1713205%22%20slang%3D%22en-US%22%3ERe%3A%20CAML%20Query%20with%20two%20conditions%20not%20working%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1713205%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F187265%22%20target%3D%22_blank%22%3E%40Frank%20Nielsen%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20have%20also%20been%20trying%20to%20get%20a%20CAML%20query%20functioning%20in%20SharePoint%20online.%3C%2FP%3E%3CP%3E1.%20In%20addition%20to%20the%20CAML%20code%2C%20do%20you%20need%20any%20other%20code%20inserted%20into%20the%20web%20part%20to%20get%20the%20query%20to%20work%3F%3C%2FP%3E%3CP%3E2.%20What%20web%20part%20are%20you%20using%3F%26nbsp%3B%20I%20see%20a%20%22content%20query%22%20web%20part%20as%20an%20option.%20Is%20this%20where%20you%20insert%20the%20code%2C%20and%20then%20it%20would%20apply%20to%20the%20list%20on%20that%20page%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1715684%22%20slang%3D%22en-US%22%3ERe%3A%20CAML%20Query%20with%20two%20conditions%20not%20working%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1715684%22%20slang%3D%22en-US%22%3E%3CP%3EHi%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F773919%22%20target%3D%22_blank%22%3E%40miler4199%3C%2FA%3E%26nbsp%3BI%20am%20using%20a%26nbsp%3B%3CSPAN%3Ehighlited%20webpart%20in%20Sharepoint%20Online%26nbsp%3BModern%2C%20and%20the%20trick%20with%20changing%20the%20And%20to%20Or%20did%20the%20magic.%20The%20CQWP%20is%20located%20in%20the%20old%20versions%20of%20SharePoint%2C%20and%20I%20am%20trying%20to%20keep%20up%20with%20the%20modern%20design.%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%3E%2FFrank%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1721913%22%20slang%3D%22en-US%22%3ERe%3A%20CAML%20Query%20with%20two%20conditions%20not%20working%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1721913%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F187265%22%20target%3D%22_blank%22%3E%40Frank%20Nielsen%3C%2FA%3E%26nbsp%3BThanks.%26nbsp%3B%20I%20just%20realized%20that%20CAML%20queries%20appear%20to%20be%20valid%20for%20SharePoint%20libraries%2C%20but%20not%20for%20lists.%26nbsp%3B%20There%20was%20an%20option%20to%20select%20a%20document%20library%2C%20but%20not%20a%20list.%3C%2FP%3E%3C%2FLINGO-BODY%3E
Occasional Contributor

Hi all. I am very new to CAML Quey in SharePoint, but I want to make a query in a highlighted content web part, with two conditions, and it is not working :(. If I only set one condition, then the results shows up fine, but two conditions, and nothing happens. This is my query:

 

<View>
<Query>
<OrderBy>
<FieldRef Name="Title"
Ascending="TRUE"></FieldRef>
</OrderBy>
<Where>
<And>
<BeginsWith><FieldRef Name="Title"></FieldRef><Value Type="Text">I-0</Value></BeginsWith>
<BeginsWith><FieldRef Name="Title"></FieldRef><Value Type="Text">I-1</Value></BeginsWith>
</And>
</Where>
</Query>
</View>

 

Can anyone guide mee in where I am doing something wrong? I have tried with the Or condition as well, but that did not help :(.

 

Hope some of you hard coders can figure this out.

THX in advance.

/Frank Nielsen, Denmark

6 Replies
best response confirmed by Frank Nielsen (Occasional Contributor)
Solution

@Frank Nielsen 

 

Hi Frank,

 

Not sure which version of SharePoint you are usingQuery looks OK for me ,AND condition wont work.

Make sure you use OR condition,However we can design the CAML Query using the UI Tools or google  with CAML Query Builder /CAML Designer

 

here is the link

https://www.biwug.be/resources

 

I hope this works for Sharepoint online too,just check it.

@O365Developer THX for quick reply. I am sorry I did not mention wich version of SharePoint I am using. It is SharePoint Online, and I was thinking that the highlitetd webpart in Sharepoint Online is not supporting two CAML conditions. I have seen a lot with SharePoint through the years, so I would not be surprised :cryingwithlaughter:

 

I will try your suggestion and use the Or function instead.

 

/Frank

Hi @O365Developer The "Or" did the trick. Thanks a lot. I am happy happy :lol::lol:

@Frank Nielsen 

I have also been trying to get a CAML query functioning in SharePoint online.

1. In addition to the CAML code, do you need any other code inserted into the web part to get the query to work?

2. What web part are you using?  I see a "content query" web part as an option. Is this where you insert the code, and then it would apply to the list on that page?

 

Thanks.

Hi @miler4199 I am using a highlited webpart in Sharepoint Online Modern, and the trick with changing the And to Or did the magic. The CQWP is located in the old versions of SharePoint, and I am trying to keep up with the modern design.

 

/Frank

@Frank Nielsen Thanks.  I just realized that CAML queries appear to be valid for SharePoint libraries, but not for lists.  There was an option to select a document library, but not a list.