I'm using the following CSOM code to retrieve a document title by id:

    ClientContext context = new ClientContext("http://sharepoint");

    List oList = context.Web.Lists.GetByTitle("Documents");
    CamlQuery camlQuery = new CamlQuery();
    camlQuery.ViewXml = "<Query><Where><Eq><FieldRef Name='ID'/><Value type='Text'>001</Value></Eq></Where></Query>";
    Microsoft.SharePoint.Client.ListItemCollection items = oList.GetItems(camlQuery);

I query specific on id: 001, but the camlQuery returns all documents. How to return only the document that matches?

  • 001 is an invalid ID, so it returns everything, it should just be 1. – Eric Alexander Feb 4 '16 at 13:42
  • @EricAlexander 001 is just an example in my question. I am using the document id service. Should I use the id's (1) or the document id's (sales001)? – Citizen SP Feb 4 '16 at 13:43
  • 1
    ID and document ID are 2 different things, if you want to query by list item ID, then you would use 1, SharePoint doesn't store list item ids with leading zeroes so it is invalid and would return all results. I don't have a DocumentID service CAML query example handy. – Eric Alexander Feb 4 '16 at 13:52

You forgot <View> tag in the CAML query, please see an example below:

        using (var ctx = new ClientContext("http://sharepoint"))
            var itemId = int.Parse("001");
            var list   = ctx.Web.Lists.GetByTitle("Documents");
            var query  = new CamlQuery();
            query.ViewXml = string.Format(
                                <Eq><FieldRef Name='ID' />
                                <Value Type='Counter'>{0}</Value></Eq>
                    </View>", itemId);

            var items = list.GetItems(query);
| improve this answer | |

Your query returns all the items because it contains error. Try to remove <Query> from your string

Update: Ok. If I have libarary and [Document ID] column with data like ' UNIQUEID-10-31', then query will be like this:

         <FieldRef Name='_dlc_DocId' />
         <Value Type='Text'>UNIQUEID-10-31</Value>

Update2: + You have type from lower case

| improve this answer | |
  • Removed <Query> but with the same result. – Citizen SP Feb 4 '16 at 13:52

I used the following query to solve this:

                <FieldRef Name='_dlc_DocId'/>
                <Value Type='Text'>DOC-ID</Value>
| improve this answer | |

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.