1

I'm working on an application that uses a CAML query to return certain documents based on a number of search and filter parameters. I need some assistance with a query that uses both AND and OR conditions.

The simplified version of the query I need to run is:

WHERE Created >= '2018-10-01'
AND Created <= '2018-10-24'
AND (File_Type == 'docx' || File_Type == 'xlsx' || File_Type == 'pptx')
AND (Title CONTAINS 'weekly' || Description CONTAINS 'weekly')

And the CAML Query I have created is:

<Where>
    <And>
        <Geq>
            <FieldRef Name="Created" /><Value Type="DateTime">2018-10-01 00:00:00</Value>
        </Geq>
        <And>
            <Leq>
                <FieldRef Name="Created" /><Value Type="DateTime">2018-10-24 00:00:00</Value>
            </Leq>
            <Or>
                <Eq>
                    <FieldRef Name="File_x0020_Type" /><Value Type="Text">docx</Value>
                </Eq>
                <Or>
                    <Eq>
                        <FieldRef Name="File_x0020_Type" /><Value Type="Text">xlsx</Value>
                    </Eq>
                    <Or>
                        <Eq>
                            <FieldRef Name="File_x0020_Type" /><Value Type="Text">pptx</Value>
                        </Eq>
                        <Or>
                            <Contains>
                                <FieldRef Name="FileLeafRef" /><Value Type="Text">weekly</Value>
                            </Contains>
                            <Contains>
                                <FieldRef Name="Description0" /><Value Type="Text">weekly</Value>
                            </Contains>
                        </Or>
                    </Or>
                </Or>
            </Or>
        </And>
    </And>
</Where>

When I run the query I get all results where either of the Title/Description or File_Type parameters are met.

My question is how do I rearrange the CAML query to incorporate an AND condition for both Title/Description and File_Type?

  • 1
    Try creating a query using CAML Designer (a free tool to create SharePoint caml queries) – harshal gite Oct 24 '18 at 10:55
  • @harshalgite Thanks for the suggestion, but are there any online tools similar to this? company policy prevents unauthorised downloads – SPBloggs101 Oct 24 '18 at 11:20
  • You can try this one c-sharpcorner.com/UploadFile/anavijai/… – harshal gite Oct 24 '18 at 11:42
  • You need to tell them you need this tool and get authorization for download. CAML is not most intuitive language to work with and CAML tools are essential for productivity as SP DEV. – Marek Sarad Oct 24 '18 at 12:03
0

Thanks for the suggestions, but I eventually worked it out through trial and error.

I had to seperate the two conditions into two OR threads nested inside an AND as such:

<Where>
    <And>
        <Geq>
            <FieldRef Name="Created" />
            <Value Type="DateTime">2018-01-01 00:00:00</Value>
        </Geq>
        <And>
            <Leq>
                <FieldRef Name="Created" />
                <Value Type="DateTime">2018-10-24 00:00:00</Value>
            </Leq>
            <And>
                <Or>
                    <Eq>
                        <FieldRef Name="File_x0020_Type" />
                        <Value Type="Text">xlsx</Value>
                    </Eq>
                    <Or>
                        <Eq>
                            <FieldRef Name="File_x0020_Type" />
                            <Value Type="Text">docx</Value>
                        </Eq>
                        <Eq>
                            <FieldRef Name="File_x0020_Type" />
                            <Value Type="Text">pptx</Value>
                        </Eq>
                    </Or>
                </Or>
                <Or>
                    <Contains>
                        <FieldRef Name="FileLeafRef" />
                        <Value Type="Text">weekly</Value>
                    </Contains>
                    <Contains>
                        <FieldRef Name="Description0" />
                        <Value Type="Text">weekly</Value>
                    </Contains>
                </Or>
            </And>
        </And>
    </And>
</Where>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.