Dynamically building CAML query in SharePoint 2010 - sharepoint

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.

Related

What is the limit of nested OR's in a CAML Query?

From the question 'SQL IN equivalent in CAML' I learned that SharePoint 2010 has a SQL "IN" equivalent for CAML. Also that the 2007 version does not support this. The OP solved this by nesting a bunch of OR statements to achieve the same result. I tested this and the nesting indeed does the magic, but...
In my case I'm getting items from a list with around a 1000 items. I dynamically create a statement with all the IDs in nested OR-blocks for my CAML-query. I didn't worry about the big number of nested blocks as this is what MSDN states about the OR-element:
Occurrences: Minimum: 0, Maximum: Unbounded.
and:
This element can be nested inside other Or and And elements. The server supports unlimited complicated queries.
When I call the GetListItems-method from the SharePoint 2007 built-in webservice, I get following error:
Exception of type 'Microsoft.SharePoint.SoapServer.SoapServerException' was thrown. Some part of your SQL statement is nested too deeply. Rewrite the query or break it up into smaller queries.
My code is written correctly because I tested the same code with only 5 nested Or-elements and the result is as expected. My question is: what IS the limit of nested Or-elements? I cannot find this anywhere as Microsoft claims this is unlimited.
Thanks in advance!
For those facing the same problem. I went for a search on SharePoint.StackExchange.com and found the following question:
Nested CAML Query results in exception “ Some part of your SQL statement is nested too deeply. Rewrite the query or break it up into smaller queries”
One of the answers points to this website:
CAML: Nested Too Deep
The person discovered that 500 items was too much but that a batch of 300 worked fine. So I tried this and this also works for me. So for anyone that faces the same problem, this might be the solution. :)
Same problem with SP2010 and nested ORs, breaks when reaching 160+ nested items.
Error raised is not very helpful (System.ArgumentException) when trying to get the SPListItemCollection = List.GetItems(Query);
Looks like SQL server refuse to execute the query.
Solution is to break in multiple queries or change the way of querying.

How can I configure Sitecore search to retrieve custom values from the search index

I am using the AdvancedDatabaseCrawler as a base for my search page. I have configured it so that I can search for what I want and it is very fast. The problem is that as soon as you want to do anything with the search results that requires accessing field values the performance goes through the roof.
The main search results part is fine as even if there are 1000 results returned from the search I am only showing 10 or 20 results per page which means I only have to retrieve 10 or 20 items. However in the sidebar I am listing out various filtering options with the number or results associated with each filtering option (eBay style). In order to retrieve these filter options I perform a relationship search based on the search results. Since the search results only contain SkinnyItems it has to call GetItem() on every single result to get the actual item in order to get the value that I'm filtering by. In other words it will call Database.GetItem(id) 1000 times! Obviously that is not terribly efficient.
Am I missing something here? Is there any way to configure Sitecore search to retrieve custom values from the search index? If I can search for the values in the index why can't I also retrieve them? If I can't, how else can I process the results without getting each individual item from the database?
Here is an idea of the functionality that I’m after: http://cameras.shop.ebay.com.au/Digital-Cameras-/31388/i.html
Klaus answered on SDN: use facetting with Apache Solr or similar.
http://sdn.sitecore.net/SDN5/Forum/ShowPost.aspx?PostID=35618
I've currently resolved this by defining dynamic fields for every field that I will need to filter by or return in the search result collection. That way I can achieve the facetted searching that is required without needing to grab field values from the database. I'm assuming that by adding the dynamic fields we are taking a performance hit when rebuilding the index. But I can live with that.
In the future we'll probably look at utilizing a product like Apache Solr.

Sharepoint search all columns of a list

I have a list in sharepoint. I want to search across all the columns for a term. How would you co about doing this?
My idea was to get the SPFieldCollection, get all the fields and generate the CAML on the fly then query the list to get the items. Just wondering if there was a better way to do this as generating the CAML may prove to be difficult.
If you don't like the idea of building the CAML using string concatenation, then you could consider using CAML.Net
I'm accessing SP with webservices, not Object Model. Anyway, I would create the CAML dynamically. Generating the CAML will not be difficult, as you can always tryout your logic with
U2U CAML Query Builder http://www.u2u.net/res/Tools/CamlQueryBuilder.aspx

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();

SharePoint CAML OrderBy Modified does not work

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.

Resources