Showing posts with label SPQuery. Show all posts
Showing posts with label SPQuery. Show all posts

Monday 1 November 2010

Using CAML and SPQuery to query using dates

Problem: Using SPQuery I can't filter using a datatime parameter.
Hypothesis: I can see my custom web part code is formatting the DateTime selection (SharePoint:DateTimeControl) differently in my custom code CAML than the CAML generated by U2U.
Resolution:
Use the Microsoft.SharePoint.Utilities.SPUtility.CreateISO8601DateTimeFromSystemDateTime() method to get your DateTime control value into the correct format.
SPUtility.CreateISO8601DateTimeFromSystemDateTime(DateTime.UtcNow)

More Info:
http://blogs.msdn.com/b/saurabhkv/archive/2008/05/05/spquery-with-boolean-and-datetime.aspx
http://snahta.blogspot.com/2009/07/spquery-few-important-things.html
http://www.aidangarnish.net/post/Using-SPQuery-and-CAML-to-filter-and-order.aspx

Thursday 5 August 2010

Retrieving Publishing Columns using a CAML Query

Overview: LINQ is easy and strongly type however, occasionally we need to get data in an optimised fashion or LINQ won't do the job i.e. inefficient queries, Publishing fields and we need to revert to a CAML query.

Code Example:

Explanation:
  1. Perform a CAML query, U2U still works on SP2010 and I use it to work out my CAML query.
  2. Add code that performs the query, optimise it (you don't need every field returned by CAML, you can look at the query results without the "ViewFieldOnly" & "IncludeMadatoryFields" setting and you will see how many fields are returned which for big queries isn't good).
  3. Using the SPListItem retrieve the field data from the publishing fields.
Andrew Connell explains how to extend SPMetal
Tobias Zimmergrin's blog has good Linq to SharePoint 2010 info, his blog on showing the CAML generated by LINQ to SharePoint queries is invaluable to work out what SPMetal is generating.
Update: 09/10/2010 Extend SPMetal to retrieve list attachments

Update 27/10/2010 - A re-hash of Tobias Zimmergrin's blog describing retrieving the CAML generated by a LINQ to sharePoint Query.