About Me

I am a Software Engineer presently working in Microsoft as vendor.The main aim of this blog is to Share my knowledge in Sharepoint,AZURE and Silverlight.

Thursday 3 July 2008

How Dates Played with me.

1)Last night i was writing some code for my project and my requirement is to pull some data from the list based on some condition and condition is linked with some date field.

And i thought of using U2U tool which makes my life happy. But U2U is working fine, when i copied the query into my code its not working :( .

So rather than depending on U2U i went with CAML Language and Dates. I came to know that CAML Language recognises dates which is in "yyyy-MM-ddTHH:mm:ssZ" this format.

So lets coming to my code i have one date field in my list where i have to filter the list items based on the condition and condition is
" If the list fields date is less than Today-24 hrs " then get the items.

here i m deleting one day from today.
so the code i written is

DateTime today;
today = DateTime.Now;
Int32 DurationArtworkWFHrs = 24;
TimeSpan s = new TimeSpan(DurationArtworkWFHrs, 0, 0);
DateTime result = today.Subtract(s);
string date = result.ToString("yyyy-MM-ddTHH:mm:ssZ");

And my CAML Query looks like below :

SPQuery query = new SPQuery();
query.Query = "<-Where><-And><-Eq><-FieldRef Name='Status' /><-Value Type='Choice'>In Progress<-/Value><-/Eq><-Lt><-FieldRef Name='ProjectedEndDate' /><-Value Type='DateTime'>" +date+ "<-/Lt><-/And><-/Where>";

(Remove "-" from the above tags to get it worked).

SPListItemCollection myItems = mylist.GetItems(query);
int Projectcount = myItems.Count;

In the above code myitems collection gives the items i required. The variable date contains the converted string which our CAML Language understands. :)

2)One more Date Played with me in infopath form.

my requirement is i have to capture the date and it should be appended to the infopath form when it is submitted.

So for this i created one field in infopath form called "Millisec" .

and assigned our custom date format to this field.

XPathNavigator xnDoc = this.MainDataSource.CreateNavigator();
XPathNavigator fldMilliseconds;
fldMilliseconds = xnDoc.SelectSingleNode("/my:myFields/my:Millisec", this.NamespaceManager);
DateTime dd;
dd = DateTime.Now;
//string[] s = dd.GetDateTimeFormats();
//string date = s[105].ToString();
//date = date.Replace(":", ""); This works but we are capturing 135 elements into arraywhich is not required.
string date = dd.ToString("yyyy-MM-ddTHHmmss");
int TimeMilli = dd.Millisecond;
string instancename = date + TimeMilli;
fldMilliseconds.SetValue(instancename);

and while submitting the form append the value in the field. :)

No comments: