2

I want to get the number of comments from the list "Comments" of a blog site. To do that, I have a query with this code:

 using (SPWeb oWebsite = oSiteCollection.OpenWeb(new Guid(WebId)))
                { 
                    SPList oList = oWebsite.Lists["Comentarios"];

                    if (oList.ItemCount > 0)
                    {
                        var query = new SPQuery()
                        {
                            Query = "<Query><Where>" +
                                        "<Eq>" +
                                            "<FieldRef Name='ows_PostID' LookupId='TRUE'/>" +
                                            "<Value Type='Lookup'>" + IDEntryBlog + "</Value>" +
                                        "</Eq>" +
                                    "</Where></Query>",
                           // ViewFields = "ID",
                            //ViewFieldsOnly = true,
                            IncludePermissions = false,
                            RowLimit = BlogInfo.MaxNumberOfComments
                        };
                        SPListItemCollection items = oList.GetItems(query);
                        ncomments = items.Count;
                    }
                }

I always receive the total number of elements. Have a look to the xml content of items: http://pastebin.com/g2nGpLKj

What am I doing wrong?

  • 6
    First off, you should spell Query with a capital Q in your query tag. But if this is server side code, as it looks like, you should omit the <Query>part from the query – Robert Lindgren Oct 9 '15 at 12:23
  • Yep, your query is malformed which is why you aren't getting the results you'd expect – Eric Alexander Oct 9 '15 at 12:26
  • @Robert It doesn't mind, but changed. Thanks EricAlexander, Do you mind tell me where? – Daniel 976034 Oct 9 '15 at 12:27
  • 2
    Use this tool to build your CAML query. u2u.be/Software – Atish Dipongkor - MVP Oct 9 '15 at 12:29
  • 1
    Which comments you wish to get by this CAML query? I mean what's the logic? – Atish Dipongkor - MVP Oct 9 '15 at 12:33
3

Your code looks like Server Object Model. Based on my experience on using CAML queries, I can suggest you to get rid of <Query> tags. So update your query as below

Query = "<Where>" +
                "<Eq>" +
                    "<FieldRef Name='ows_PostID' LookupId='TRUE'/>" +
                    "<Value Type='Lookup'>" + IDEntryBlog + "</Value>" +
                "</Eq>" +
        "</Where>"  
  • 1
    Exception. If you're interested how I resolved this little issue, check my answer. Thank your interest in my problem. – Daniel 976034 Oct 13 '15 at 8:16
  • 1
    @Daniel976034 : I am glad that the problem is solved. :) – Asad Refai Oct 13 '15 at 8:47
1

This is the code generated by the tool that @Atish commented. Note the "at"

SPList list = web.Lists[new Guid("d4dd7678-6dd9-47de-9d0c-951368081bdf")];
var q = new SPQuery()
{
    Query = @"<Where><Eq><FieldRef Name='PostID' /><Value Type='Lookup'>3</Value></Eq></Where>"
};


var r = list.GetItems(q);

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.