SharePoint CAML OrderBy Modified does not work - sharepoint

I have made a query against a list. I want to get the last modified item which meets a certain condition, and my query looks like this:
<Query><OrderBy><FieldRef Name='Modified' Ascending='FALSE' /></OrderBy><Where><Eq><FieldRef Name='kortnummer'/><Value Type='String'>kv11</Value></Eq></Where></Query>
I get the listitems i need, just not in the right order. Changing Ascending to true does nothing, so obviously there is something with the OrderBy clause that is not right..
According to MSDN it should be possible to order by Modified.
Any idea why my OrderBy does not work?

I always put the OrderBy after the Where.
Otherwise you could make sure that you are using the internal name. I usually use SharePoint Manager 2007 to get the internal field names. Codeplex SharePoint manager
I can see that on a list in my SharePoint, the modified column internal name is "Last_x0020_Modified".

If you're doing this in a C# string to get an SPListItemCollection, you don't need "<Query></Query>" tags.

Related

CAML Query with Contains and Or Clause Issue

What I want to achieve : Take a keyword array as input and query Sharepoint List to return all rows which contain the keywords in the list.
I have built a simple CAML query to query my list with one keyword (pdf) .
<Query><Where><Contains><FieldRef Name='Keyword'/><Value Type='Text'>pdf</Value></Contains></Where></Query>
This works fine.
But, when I try to use Or clause in the CAML query(see below), I get the following error
"One or more field types are not installed properly. Go to the list settings page to delete these fields."
<Query><Where><Or><Contains><FieldRef Name='Keyword'/><Value Type='Text'>pdf</Value></Contains></Or></Where></Query>
I googled for the syntax and everything looks good. Please let me know what is missing.
Thanks in advance.
In CAML Query if you want to use OR you must and should have 2 conditions.
The field reference name must be the internal name. You can find this by going to the colmn page in list/library settings and the name is the end of the URL. Spaces and underscores in the name must be handled differently.

SharePoint returns extra system columns

I do query to SherePoint. I have created query, viewquery and query options.
Web services returns me great results, but it include some other system columns such as:
ows_Modified , ows_DocIcon, ows_Editor. I don't want them. How do I return only those which is in ViewQuery string?
My queryoptions is:
#"<QueryOptions>
<IncludeMandatoryColumns>False</IncludeMandatoryColumns><ViewAttributes Scope='Recursive' />
</QueryOptions>";
Thanks!
In order to return only selected columns (and not all of them) use ViewFields property of SPQuery object. You can find some more information about it and a sample code here: http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spquery.viewfields.aspx.
In order to do it from javascript, you can try code as written here (that post is on another topic, but it still shows how to specify fields to select): https://sharepoint.stackexchange.com/questions/33683/spservices-today-not-returning-correct-results.

SharePoint 2010 ClientObjectModel : Find Item by GUID

I have a list of 6000 listitem GUIDs. I need to check if each item still exist on the Web.
Previously, I was using the SPWeb.GetFile(GUID) method to check the item.
But now I need to convert that process usinig the ClientObjectModel. I cannot find a way to retrieve the item using the GUID.
I even tried from the "List" object. And can't find a way to retrieve the item from its GUID neither.
Please help! :(
Try to use CAML Query.
Ex CAML Query:
<Query><Where><Eq><FieldRef Name='ID'/><Value type='Counter'>"+fileIDtoFind+"</Value></Eq></Where></Query>
For more details see: http://msdn.microsoft.com/en-us/library/ie/ee857094.aspx

Dynamically building CAML query in SharePoint 2010

I have an requirement of get the items from the list depends on the ItemID.I have list which contains 5000 items in which I retrieve only 1000 items for that I will dynamically build the CAML query using JohnHoliday CAML.NET and the query have 1000 conditions at that time I got Value does not fall within the Expected Range error. The query works fine upto 150 items but it throws error when the ItemID increases.Could you provide a suitable workaround for this issue ?
You're running into the size limit for CAML queries, which AFAIK isn't documented anywhere, but definitly exists.
If you only need to support 2010 then you can use the new <In> operator which probably with get you a bid further
In sharepoint 2010 there is a List view threshold configuration that give the administrator the ability to determine the maximum items you can retreive in a one patch
and to overcome this problem you can use
ContentIterator
check this link for more help
Why not iterate over SPList.Items and take which items you need? So there's no need to build a complex caml query. Or call SPList.GetItemByUniqueId.

Query and/or Search for SharePoint Document ID

We have the sharepoint 2010 environment with Document ID's enabled.
Given (part of) a Doc ID, we want to programmatically retrieve the document(s) matching that ID. The problem seems to be that this column is rather special, in that it might need special handling.
Using an SPSiteDataQuery, fetching the _dlc_DocId field as part of the viewfields works fine. However, including it as part of the where query never results in any documents being fetched.
Using the Search API has gotten us nowhere at all.
Has anyone pulled this off, or any suggestions on how to tackle this problem?
[Update] Turns out we were fooled by subtle errors in the XML and bad debugging misinterpretations. This stuff just works fine.
I don't normally contribute to these sorts of things because cleverer people than I always get there before me, but as this is an old one with no proper answer I think I'll add my thoughts for those who find this page.
I was struggling with this but after a little digging around and learning a bit of Caml I got this working.
I am using the SharePoint Client Object Model against SharePoint 2010 and Office365 beta.
Start off your query by looking at the all list items query:
Microsoft.SharePoint.Client.CamlQuery.CreateAllItemsQuery().ViewXml
"<View Scope=\"RecursiveAll\">\r\n <Query>\r\n </Query>\r\n</View>"
Stick a where child inside the query
Then add in
<Eq><FieldRef Name="_dlc_DocId" /><Value Type="Text">MDXC2KE55ASN-3-80</Value></Eq>
replacing MDXC2KE55ASN-3-80 with the doc ID you are looking for inside the where.
Also don't forget you might want to make use of these too:
<ViewFields><FieldRef Name="_dlc_DocId" /></ViewFields>
<RowLimit>1</RowLimit>
Then use List.GetItems() method to bring back the ListItemCollection.
Just in case nobody comes with a slick solutions from the depths of the Sharepoint infrastructure:
What would Google Do?
Slice is, Dice it and dump it in a reverse index.
Solr and Lucene offer supreme tools for this. The idea is to cut the DocId's in small pieces and add the location of the document to the bucket for that piece.
Say We have "A real nice document" with Id ABCD123. You would add it to the buckets
ABCD, BCD1, CD12, D123
When searching for a partial ID (+ other data like dates, types, ...) you (well the search engine) creates the union of the buckets + applies additonal constraints.
To make this happen you need to write a spider for the sharepoint server and a routine which makes a record of data elements to be indexed.
Put a nice REST interface in frnt of it (actually SOLR already has that), integrate it in the main sharepoint server, and nobody needs to know there is something else running behind it.
These products can also incrementally update the indexes, so they can be kept up to date.
you could use the following to get the Document ID.
SPFile file = MethodToUploadFileToServer(web, filepath);
SPListItem item = file.Item;
string DocID = item.Properties["_dlc_DocId"].ToString();

Resources