how to use content iterator in sharepoit 2010 - sharepoint

there are 7000 items in my list. i need to filter the list and retrieve result
i am using the following code in my webpart.
string query = "<Where><BeginsWith><FieldRef Name='Project' /><Value Type='Text'>ab</Value></BeginsWith></Where>"
SPQuery spquery = new SPQuery();
spquery.Query = query;
ContentIterator iterator = new ContentIterator();
iterator.ProcessListItems(list, spquery, ProcessListItem, ProcessListItemError)
as i am using ContentIterator still it is giving me the error "The attempted operation is prohibited because it exceeds the list view threshold enforced by the administrator"
Update:
string query2 = #"<Where><Eq><FieldRef Name='Project' /><Value Type='Text'>11759</Value></Eq></Where>";
SPQuery spquery = new SPQuery();
spquery.RowLimit = 10;
spquery.Query = query2;
spquery.QueryThrottleMode = SPQueryThrottleOption.Override;
ContentIterator iterator = new ContentIterator();
iterator.ProcessListItems(list, spquery, ProcessListItem, ProcessListItemError);
In every case weather I used SPCollectionItem or Iterator. when ever I am passing the where condition in spquery. same error comes.

To efficiently use ContentIterator and avoid throttle exception, you should explicitly include OrderBy clauses.
Try to use ContentIterator.ItemEnumerationOrderByNVPField which actually enables the index to be used.
For more details,check this out
http://extreme-sharepoint.com/2012/07/17/data-access-via-caml-queries/

Likely because the the Field you are comparing on is not indexed: http://msdn.microsoft.com/en-us/library/ff798465
If you were to build a query that returns the first 100 items sorted
by the ID field, the query would execute without issue because the ID
column is always indexed. However, if you were to build a query that
returns the first 100 items sorted by a non-indexed Title field, the
query would have to scan all 10,000 rows in the content database in
order to determine the sort order by title before returning the first
100 items. Because of this, the query would be throttled, and rightly
so—this is a resource-intensive operation.

You either have to ...
... set a RowLimit below the configured threshold on your query.
... or define a query that returns less items.
... or change the threshold of the list.

have you seen this:
msdn link
hmmz, just tried this and it still gave an error. If I use this however I can iterate over way more than 7k listitems:
private int GetItems(SPList list){
var query = new SPQuery();
query.Query = "";
query.QueryThrottleMode = SPQueryThrottleOption.Override;
var items = list.GetItems(query);
return items.Count;
}
so my advice is to just use the list.getitems

You just need to change your query.
//Keep your query inside <View></View> tag.
string query = "<View><Where><BeginsWith><FieldRef Name='Project' /><Value Type='Text'>ab</Value></BeginsWith></Where><View>"
SPQuery spquery = new SPQuery();
spquery.Query = query;
ContentIterator iterator = new ContentIterator();
iterator.ProcessListItems(list, spquery, ProcessListItem, ProcessListItemError)
Now run the program and it will work.

Related

Sharepoint: Trying to get the highest ows_ID inclusive of deleted items

I am using SharePoint web service and am trying to return the highest ID number from a list. My scenario is like say I have 10 items in a list. Highest ows_ID at this point is 10. Now, if I delete an item and check the max of ows_ID, I got 9, however the highest ows_ID is 10 inclusive of deletion.
If a new item gets inserted, it gets the ows_ID as 11 instead of 10. In the same way, is it possible to get the highest ows_ID even though the item is deleted?
You can call GetList to see the highest ID used. That method returns information about the list itself rather than the items in it.
Try "rowLimit", configure your CAML to do your stuff and only get the first item in the search criteria:
string rowLimit = "1";
System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
System.Xml.XmlElement query = xmlDoc.CreateElement("Query");
System.Xml.XmlElement viewFields = xmlDoc.CreateElement("ViewFields");
System.Xml.XmlElement queryOptions = xmlDoc.CreateElement("QueryOptions");
query.InnerXml = "<Where><Eq><FieldRef Name=\"FileLeafRef\"/><Value Type=\"" + fieldType + "\">" + fieldValue + "</Value></Eq></Where> ";
viewFields.InnerXml = "<FieldRef Name=\""+fieldToGet+"\" />";
queryOptions.InnerXml = "";
System.Xml.XmlNode nodes = myservice.GetListItems(listGuid, viewGuid, query, viewFields, rowLimit, null, null);
In my example below, I just get the last created item, using some criteria, but as You don't need this, just fix the caml in my code.

SPQuery calculated fields filter does not work in SharePoint 2007/2010

I have a calculated field in my list and I am trying to use filter on this field. For some reason, the following query always returns all items instead of a filtered item collection:
var spQuery = new SPQuery
{
Query = #"<Where><Geq><FieldRef Name='Score' /><Value Type='Calculated'>10000</Value></Geq></Where><OrderBy><FieldRef Name='Modified' Ascending='True' /></OrderBy>",
RowLimit = 200,
ViewFields = #"<FieldRef Name='Username' />"
};
var spList = web.Lists["Users"];
var spListItemCollection = spList.GetItems(spQuery);
try using
spQuery.ViewFieldsOnly = true;
Remove the query element from your SPQuery. See syntax here
Try removing ViewFields section (to get all columns, also those that are needed for calculation) or set SPQUery.IncludeMandatoryColumns

How can I write a SPQuery to filter items based on a LinkFieldValue?

I need to select a single value from a SharePoint list based on a field value. The type of the field is LinkFieldValue. How should I write the CAML query?
When I select the items with an empty query, I receive all the items in the list as expected.
When I add constraints to the query, it returns an empty result. I have tried constructing the query as follows:
string.Format("<Where><Eq><FieldRef Name=\"PollInstancePoll\" /><Value "
+"Type=\"Text\">{0}</Value></Eq></Where>",
new LinkFieldValue { NavigateUrl = "/az/Lists/Polls/DispForm.aspx?ID=1",
Text = "example poll" });
which results in the following query text:
<Where><Eq><FieldRef Name="PollInstancePoll" />
<Value Type="Text">example poll</Value>
</Eq></Where>
I have solved my problem with the following query:
new SPQuery
{
Query =
CAML.Where(
CAML.And(
CAML.Contains(
CAML.FieldRef("PollInstancePoll"),
CAML.Value(pollPath)),
CAML.Contains(
CAML.FieldRef("PollInstancePage"),
CAML.Value(pagePath))))
};
Essentially I am checking only the URL part of the Link field, and providing the value for comparison as Type="Text". It is important to remember that SharePoint stores the values in database always as server-relative URLs.

Checking exception before using GETITEMBYID()

I am getting item by getiembyid...but I want to check before using it that whether item exist or not...I don't want to use query as main purpose of using Getitembyid is performance.....any idea how to achieve this...
itemid = Response.QueryString["loc"];
SPList mylist = myweb.GetList(SPUrlUtility.CombineUrl(myweb.ServerRelativeUrl, "/Lists/Location"));
//now id itemid does not exist it throws exception...so i want to check before using following statement that itemid exist...I know i can check throw SPQuery but as i said above because of performance issue only i m using itemid....
SPListItem myitem = mylist.GetItemById(Convert.ToInt32(itemid));
Any idea how to achieve this?
SPQuery by no way will make your code slow, infact every other SharePoint article on net advice you to use SPQuery to gain Performance. To make things still more interesting GetItemByID internally use SPQuery to fetch the Item back to you,following is part of code taken from the GetItemByID function
SPQuery query = new SPQuery();
query.Query = "<Where><Eq><FieldRef Name=\"ID\"></FieldRef><Value Type=\"Integer\">" + id.ToString(CultureInfo.InvariantCulture) + "</Value></Eq></Where>";
Important to note here is SPQuery has a Internal property called SingleItemId which takes Id of the Item Id you want to fetch, further tracing of how it being used cannot be found as the call finally lands up in COM Object.
Being said that you have two options
Option 1:
Wrap your GetItemByID code inside the catch block and check for exception,if one occurs you can mark a flag to denote that Item Id is invalid and take action for it.
Option 2:Use SPQuery, you can test the time difference GetItemByID & using SPQuery, you will see that there is no and just a very very little.

SharePoint's List.GetItems(view) returns ALL items instead of filtered view items

I'm trying to get a count of items in a list, based on a view. Every method I've tried so far only returns the grand total of the list. I've tried just about every method I've run across while searching, and everything ends up with the same results.
Here's one of the methods I've tried:
SPWeb web = SPContext.Current.Web;
SPView view = web.GetViewFromUrl("url to my view");
int count = view.ParentList.GetItems(view).Count;
My list has 28 items, but the view I'm referencing filters it and shows four items. I expect my count to be 4, not 28 - but 28 is what I always get.
Here's another method I've tried:
SPSite site = SPContext.Current.Site;
SPWeb web = site.OpenWeb();
SPQuery MyQuery = new SPQuery();
MyQuery.Query = "<Query><Where><Eq><FieldRef Name='strStatus' /><Value Type='Text'>submitted</Value></Eq></Where></Query>";
IEnumerable<SPListItem> results = web.Lists["Requests"].GetItems(MyQuery).Cast<SPListItem>();
// results.Count() is 28... should be 4
So in this method I'm skipping the view and just trying to pass in a CAML query. When testing my query in U2U, four results are returned as expected...
The larger picture is that I'm doing this inside of my custom menu control's OnMenuItemDataBound event handler. I don't know if that makes a difference at all, but the idea I'm heading towards is that each item that links to a view in a specific list, will show the count of items in that view next to the link.
Any ideas why I'm getting a list total instead of the filtered totals? Thanks!
If I remind correctly, you need to remove the <Query> from your SPQuery. The CAML Builders use it but its unnecessary in the actual SPQuery. Of course you need to make sure the fields exist.
MyQuery.Query = "<Where><Eq><FieldRef Name='strStatus' /><Value Type='Text'>submitted</Value></Eq></Where>";

Resources