0

I'm trying to query a SharePoint list on my root site collection but keep getting the error "Request failed, cannot complete this action". I messed around with the CAML query below and have figured out that the CAML is the issue, but I'm not sure what the problem is. I know it's the issue because when I remove the clause it queries successfully.

function retrieveListItems() {
                    var clientContext = new SP.ClientContext(siteUrl);
                    var oList = clientContext.get_web().get_lists().getByTitle('ICE Divisions');
                    console.log(oList);

                    var camlQuery = new SP.CamlQuery();
                    camlQuery.set_viewXml("<View><Query><Where><Eq><FieldRef Name='internalFieldName'><Value Type='Lookup'>value</Value></FieldRef></Eq></Where><OrderBy><FieldRef Name='internalFieldName'/></OrderBy></Query></View>");


                    var collListItem = oList.getItems(camlQuery);


                    clientContext.load(collListItem);
                    console.log(collListItem);


                    clientContext.executeQueryAsync(Function.createDelegate(this, onQuerySucceeded), Function.createDelegate(this, onQueryFailed));


                    function onQuerySucceeded(sender, args) {

                        var listItemInfo = '';

                        var listItemEnumerator = collListItem.getEnumerator();

                        while (listItemEnumerator.moveNext()) {
                            var oListItem = listItemEnumerator.get_current();
                            console.log(oListItem);
                        }
                    }

                    function onQueryFailed(sender, args) {

                        alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
                    }
                }
            }
        },

Any ideas?

  • 1
    It probably won't resolve your problem but <FieldRef> cannot contain any child element like <Value>. Instead it should be <Eq><FieldRef Name='internalFieldName' /><Value Type='Lookup'>value</Value></Eq> – Lukas Nespor Jan 11 at 22:58
1

Try your query like this:

<Query>
    <Where>
        <Eq>
            <FieldRef Name="internalFieldName"/>
            <Value Type="Lookup">value</Value>
        </Eq>
    </Where>
    <OrderBy>
        <FieldRef Name="internalFieldName" />
    </OrderBy>  
</Query>  

Default lookup query uses the value instead of lookup Id

The typical query looks like

<Query>
    <Where>
        <Eq>
            <FieldRef Name='internalFieldName'  />
            <Value Type='Lookup'>value</Value>
        </Eq>
    </Where>
</Query>

To query by ID you have to setup LookupId='TRUE' attribute of FieldRef element

<FieldRef Name='internalFieldName' LookupId='TRUE' />

Reference:

FieldRef Element

  • Have you tried using this? – Ganesh Sanap Jan 20 at 13:04
0

Here is the demo to filter lookup field in CAML:

<View>  
    <Query> 
       <Where><Eq><FieldRef Name='testlookup' /><Value Type='Lookup'>AdminTitle</Value></Eq></Where><OrderBy><FieldRef Name='testlookup' /></OrderBy> 
    </Query> 
 </View>

Make sure your internal field name is valid and you'd better build CAML with CAML Designer:

Caml Designer

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.