Query to a list doesn't work
-
05-10-2020 - |
سؤال
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?
المحلول 2
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 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>"
لا تنتمي إلى sharepoint.stackexchange