• Upgrade your Internet Experience
  • Sign in
  • Microsoft.com
  • United States (English)
    Brasil (Português)Česká republika (Čeština)Deutschland (Deutsch)España (Español)France (Français)Italia (Italiano)Россия (Русский)대한민국 (한국어)中华人民共和国 (中文)台灣 (中文)日本 (日本語)香港特别行政區 (中文)
 
 
Microsoft Developer Network
 
 
Home
 
 
Library
 
 
Learn
 
 
Downloads
 
 
Support
 
 
Community
 
 
Forums
 
 
 
Microsoft Developer Network > SharePoint Products and Technologies Forums > SharePoint - Development and Programming > SPQuery with datetime field
Ask a questionAsk a question
Search Forums:
  • Search SharePoint - Development and Programming Forum Search SharePoint - Development and Programming Forum
  • Search All SharePoint Products and Technologies Forums Search All SharePoint Products and Technologies Forums
  • Search All MSDN Forums Search All MSDN Forums
 

AnswerSPQuery with datetime field

  • Wednesday, July 30, 2008 6:35 AMprabhukumard Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Vote As Helpful
    0
    hi,

          i need to show created, created by and document name into gridview based on created date.

    for ex: I have two textbox one for "from date" and other for "to date" and a button. when user click on the button what are the documents available in document library between the two date. 
    i have tried this way,

    SPQuery oQuery = new SPQuery();
    oQuery.ViewFields = "<FieldRef Name='Created'/>" + "<FieldRef Name='FileLeafRef'/>" + "<FieldRef Name='Author'/>";
    DateTime strFromDate = Convert.ToDateTime(txtFromDate.Text);
    DateTime strToDate = Convert.ToDateTime(txtToDate.Text);
    oQuery.Query = "<Where><FieldRef Name='Created'><Value Type=\"DateTime\" StorageTZ='TRUE'>" + strFromDate + "</Value><FieldRef Name='Created'><Value Type=\"DateTime\" StorageTZ='TRUE'>" + strToDate + "</Value></Where>";
    SPListItemCollection items = splist.GetItems(oQuery);
    DataTable dt = items.GetDataTable();
    return dt;
    i am new to this technology.


    i don't know how to use the spquery for this scenario.

    please anyone can help me.......
    thanks in advace........


     

    • ReplyReply
    • QuoteQuote
     

Answers

  • Wednesday, July 30, 2008 7:03 AMLaxmikant Rathi Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    Vote As Helpful
    0
     you need to use AND condition and the "less than" and "greater than" operators to get the result:

    your query should look something like this :

    string createdBeforeQuery = "<Leq><FieldRef Name=\'Created\'/><Value Type=\'DateTime\'>" + strToDate.ToString("s") + "</Value></Leq>";  
     
    string createdAfterQuery = "<Geq><FieldRef Name=\'Created\'/><Value Type=\'DateTime\'>" + strFromDate.ToString("s") + "</Value></Geq>";  
     
     
    oQuery.Query = "<Where><And>" + createdBeforeQuery + createdAfterQuery + "</And></Where>"  
     

    at the start comment the oQuery.ViewFields line to be sure that there are no syntax mistakes in it.

    check if this helps..
    • Proposed As Answer byTobias Zimmergren [MVP]MVP, ModeratorWednesday, July 30, 2008 7:11 AM
    • Marked As Answer byPaul AndrewMSFT, ModeratorWednesday, July 30, 2008 4:52 PM
    •  
    • ReplyReply
    • QuoteQuote
     

All Replies

  • Friday, August 01, 2008 7:15 AMLZandman Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    Vote As Helpful
    1

    I don't see you define any ViewFields in your query. The returned SPListItems only contains fields if you explicitly ask for them.

    Like this:

    public static string BuildViewFieldsXml(params string[] fieldNames)  
    {  
       const string TEMPLATE = @"<FieldRef Name='{0:S}'/>";  
       StringBuilder sb = new StringBuilder();  
       foreach (string fieldName in fieldNames)  
       {  
           sb.AppendFormat(TEMPLATE, fieldName));  
       }  
       return sb.ToString();  
    }  
     
    // Example usage:  
    SPQuery query = new SPQuery();  
    query.ViewFields = BuildViewFieldsXml("Title", "Created", "SomeCustomField");  
    query.Query = ...;  // You know the drill  
     
    • ReplyReply
    • QuoteQuote
     
  • Wednesday, July 30, 2008 7:10 AMTobias Zimmergren [MVP]MVP, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Proposed Answer
    Vote As Helpful
    0
    Hi,

    In order to make sure that your CAML Query works as it should, you can (and in my opinion, you should) test it with the U2U CAML Query Builder: http://www.u2u.info/SharePoint/U2U%20Community%20Tools/Forms/AllItems.aspx

    What this tool does, is generate queries for your automatically and of course lets you try your existing queries.

    If the query is successful in the Query Builder, it should be successfull in your code aswell.
    And as previous posted mentioned, try commenting out the viewfields line to make sure there's no errors in there.

    Regards,
    Tobias Zimmergren
    http://www.zimmergren.net
    • Proposed As Answer byTobias Zimmergren [MVP]MVP, ModeratorWednesday, July 30, 2008 7:11 AM
    •  
    • ReplyReply
    • QuoteQuote
     
  • Wednesday, July 30, 2008 10:16 AMLZandman Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Vote As Helpful
    0
    DateTime values have to be converted before you can use them in a CAML query. You can use the SPUtility.CreateISO8601DateTimeFromSystemDateTime() method for this.

    Also check this blog entry if you notice the time part of your DateTime values are ignored.
    • ReplyReply
    • QuoteQuote
     
  • Thursday, July 31, 2008 4:13 AMprabhukumard Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Vote As Helpful
    0
    hi All,

       Thanks for your response.

    after executing the following code, i am getting "A field or property with the name 'Created' was not found on the selected data source" error.

    SPSite spsite = new SPSite(http://localhost);

    SPWeb spweb = spsite.OpenWeb();

    SPList splist = spweb.Lists["Shared Documents"];

    SPQuery oQuery = new SPQuery();

    DateTime strFromDate = Convert.ToDateTime(txtFromDate.Text);

    DateTime strToDate = Convert.ToDateTime(txtToDate.Text);

    string convertedTime = Microsoft.SharePoint.Utilities.SPUtility.CreateISO8601DateTimeFromSystemDateTime(strFromDate);

    string convertedTime1 = Microsoft.SharePoint.Utilities.SPUtility.CreateISO8601DateTimeFromSystemDateTime(strToDate);

    string Query = @" <Where><And>

    <Leq>

    <FieldRef Name='Created' />

    <Value Type='DateTime'>" + convertedTime1 + @"</Value>

    </Leq>

    <Geq>

    <FieldRef Name='Created' />

    <Value Type='DateTime'>" + convertedTime + @"</Value>

    </Geq>

    </And>

    </Where>";

    oQuery.Query = Query;

    SPListItemCollection items = splist.GetItems(oQuery);


    DataTable dt = items.GetDataTable();

    return dt;

    First Time it was executing fine.
    after that the Created column will be vanished.
    please help me...




     

    • ReplyReply
    • QuoteQuote
     
Need Help with Forums? (FAQ)
 
© 2009 Microsoft Corporation. All rights reserved.
Terms of Use
|
Trademarks
|
Privacy Statement