executing a caml query to get a lookup field : "Cannot complete this action.Please try again."?

4.1k Views Asked by At

It is my caml query code to get the value of a lookup field in SharePoint 2016 on-premises (CSOM).

<View>
                                <Query>
                                    <Where>
                                        <Eq>
                                            <FieldRef Name='myLookupField' LookupId='TRUE'/>
                                            <Value Type='Lookup'>18456</Value>
                                        </Eq>
                                    </Where>
                                </Query>
                            </View>

tried removing this part "LookupId='TRUE'" but still get this exception: "Cannot complete this action. Please try again." I run this caml query in U2U caml query builder but it works.

1

There are 1 best solutions below

1
Jerry On

Tested CAML in CSOM like below, it's working:

        using (ClientContext ctx=new ClientContext("http://sp/sites/dev/"))
        {
            List list = ctx.Web.Lists.GetByTitle("MyList22");
            CamlQuery caml = new CamlQuery();
            caml.ViewXml = "<View><Query><Where><Eq><FieldRef Name='myLookupField' LookupId='TRUE'/><Value Type='Lookup'>1</Value></Eq></Where></Query></View>";
            ListItemCollection items = list.GetItems(caml);
            ctx.Load(items);
            ctx.ExecuteQuery();
            foreach (ListItem item in items)
            {
                FieldLookupValue value = item["myLookupField"] as FieldLookupValue;
                Console.WriteLine("LookupId: "+ value.LookupId);
                Console.WriteLine("LookupValue: "+ value.LookupValue);

            }
        }

This is the test list data: enter image description here

enter image description here