I am looking for a way to sort files by date (id seems to do the same).
SPFolder imageFolder = web.GetFolder(...);
...
I know I have to do this with caml but how?
Thanks
Yes, this should be done with a CAML query. Use the SPQuery class to execute such a query. Use the OrderBy element in order to sort the result set:
<OrderBy>
<FieldRef Name="yourdatefield" />
</OrderBy>
Example:
SPList list = ... // the list where you images are stored.
SPQuery query = new SPQuery();
query.Folder = imageFolder;
query.Query = "<OrderBy><FieldRef Name=\"Created\" /></OrderBy>";
SPListItemCollection items = list.GetItems(query);
The variable items now contains the contents of the imageFolder sorted by the field "Created".
In order to access the image files use the member File on SPListItem:
foreach (SPListItem item in items)
{
Console.WriteLine("Filename: " + item.File.Name);
}
Related
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.
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
I have a webpart which is 2-3 subsites down the top level site. I need to query the list which is in top site collection and one at the same level,I guess its possible through SPSiteDataquery...I have some confusion related to it can i write single query which can query both these list....
The scope of this query is sitecollection so that means it wud going to look into all list in sitecollection..and if my CAML query is same for both these lists ...it should work?
let me explain through my code:
SPSite mySite = SPControl.GetContextSite(Context);
SPWeb myWeb = SPControl.GetContextWeb(Context);
SPSiteDataQuery qry = new SPSiteDataQuery();
qry.Lists = "<Lists BaseType='0' />";
qry.Query = "<Where><Contains><FieldRef Name='Country'/><Value Type='Text'>" + strcount + "</Value></Contains></Where>";
qry.ViewFields = "<FieldRef Name='Capital' Nullable='TRUE'/><FieldRef Name='Currency' Nullable='TRUE'/>";
qry.Webs = "<Webs Scope='SiteCollection' />";
DataTable dt = myWeb.GetSiteData(qry);
Now i need currency from list which is in top level site and Capital from the list which is at same level. Is this possible? or I misunderstood SPSiteDataQuery...?
You are on the right track and it is possible to retrieve results for lists in different webs. The following example shows how to retrieve items from the default 'Tasks' or 'Workflow Tasks' lists and works with task lists created at the root level and within a sub site.
SPSiteDataQuery q = new SPSiteDataQuery();
q.ViewFields = "<FieldRef Name='Title'/><FieldRef Name='Priority'/><FieldRef Name='Status'/>";
q.Webs = "<Webs Scope='SiteCollection' />";
q.Lists = "<Lists BaseType='0' />";
q.Query = "<Where><Gt><FieldRef Name='ID' /><Value Type='Number'>0</Value></Gt></Where>";
DataTable results = new DataTable();
using (SPSite site = new SPSite("http://sharepoint"))
{
using (SPWeb web = site.OpenWeb("subsite"))
{
results = web.GetSiteData(q);
}
}
I've written it using a hardcoded URL so you can run it inside a console application for testing but you can replace the using statements with something like SPWeb web = SPContext.Current.Web; when you put this inside a web part.
A few other things worth considering:
The lists you are querying must contain all the fields in the ViewFields element
Multi-lookup fields do not work well with the SPSiteDataQuery (single value lookup fields are ok)
The u2u CAML builder tool is also useful for testing CAML queries. See http://www.u2u.be/Res/Tools/CamlQueryBuilder.aspx
I'm trying to query a SharePoint list using the following CAML query in a webpart. I have tested the query in U2U CAML Query Builder and Stramit CAML Viewer and it works fine, only returning the matching records, but when I use it in my webpart it return all list items. It is driving me crazyyyyy. Here is the code:
string camlQuery = string.Format(#"<Query><Where><Contains><FieldRef Name='Title' /><Value Type='Text'>2</Value></Contains></Where></Query>");
SPQuery query = new SPQuery();
query.Query = camlQuery;
SPListItemCollection items = Articles.GetItems(query);
grid.DataSource = items.GetDataTable();
grid.DataBind();
Leave out the surrounding Query tag, just use:
<Where><Contains><FieldRef Name='Title' /><Value Type='Text'>2</Value></Contains></Where>
SPQuery adds the Query tag itself.
I need help converting the following code snippet to use SPSiteDataQuery instead of SPQuery b/c I need to query accross all Document Libraries in the site.
Here's the original code:
using (SPWeb oWeb = new SPSite(properties.SiteId).OpenWeb(properties.RelativeWebUrl))
{
SPList oList = oWeb.Lists["Quality Documents"];
//create query
SPQuery oQuery = new SPQuery();
//configure the query //
oQuery.Query = "<Where><Eq><FieldRef Name='Document_x0020_Number' /><Value Type='Text'>" + docNum + "</Value></Eq></Where>";
//get the collection of items in the list
SPListItemCollection oItems = oList.GetItems(oQuery);
if (oItems.Count > 0)
{
newRnd = 0;
}
else
{
newRnd = 1;
}
}
And here's what I have for the SPSiteDataQuery but I don't know if it's correct and/or how to make sure the code does the same thing as the above code.
SPSiteDataQuery q = new SPSiteDataQuery();
q.Lists = "<Lists BaseType='1'/>";
q.Query = "<Where><Eq><FieldRef Name='Document_x0020_Number' /><Value Type='Text'>" + docNum + "</Value></Eq></Where>";
q.Webs = "<Webs Scope='SiteCollection' />";
q.ViewFields = "<FieldRef Name='Document_x0020_Number' />"';
q.RowLimit = 1;
The main difference between SPQuery and SPSiteDataQuery is in specifying the list to query. Unlike with SPQuery, in SPSiteDataQuery you cannot specify a list title to narrow down your query in each site, you only have the Lists element which is a lot less flexible. Vincent Rothwell describes all of the possibilities here.
This inflexibility means that the use of BaseType=1 in your Lists element code will return documents from all document libraries. The only way you can avoid this is if you deployed a custom list template for Quality Documents with its own ID number. You would then be able to use <Lists ServerTemplate='XYZ' /> (where XYZ is the ID number to narrow results down to that type of list). Otherwise you will need to filter the results you get back so they only contain entries from the Quality Documents library.
Apart from this your code looks equivalent. You may also like to look at CrossListQueryInfo and CrossListQueryCache if you are running MOSS. These allow you to cache SPSiteDataQuery-style queries. See this post by Jeff Dalton for good information on this.
Update from comments about object disposal:
Your code will leak the SPSite object - this needs a using clause as well. Replace the using line you currently have with these lines:
using (SPSite oSite = new SPSite(properties.SiteId))
using (SPWeb oWeb = oSite.OpenWeb(properties.RelativeWebUrl))
{
// Your code
}
Also, it is easy to use SPDisposeCheck. To integrate with Visual Studio add the following line to your project's post build event:
"%ProgramFiles%\Microsoft\SharePoint
Dispose Check\SPDisposeCheck.exe"
$(TargetPath)
The build will now fail if you have memory leaks. Check the Output window for details on where they are. Note that SPDisposeCheck sometimes reports false positives and it is possible to ignore these (read the documentation to tell you how).