Aishwarya
Aishwarya

Reputation: 23

Outlook AdvancedSearch DASL using c#

I am doing advancesearch in outlook using c# office addin (VSTO)

Filter DASL query - urn:schemas:httpmail:datereceived > 'dd.M.yy HH:mm' - Returning Items in my Machine. But it is not working in some other machines . My machine date time format is dd-mm-yy

Filter urn:schemas:httpmail:datereceived > 'dd/MM/yyyy HH:mm' - Not returing items in my machine and working in some other machine .

Is there any universal format is there to filter to work in all machines(environments).

Upvotes: 1

Views: 678

Answers (1)

Dzyann
Dzyann

Reputation: 5208

I think your problem is that you are passing by hand the format. From this post. (emphasis mine)

The format used by Outlook corresponds to the General (short date and short time) pattern in the DateTimeFormatInfo class. If you use the Parse method of the DateTime structure, you should be certain that the argument to the Parse method follows the short date and short time format for the current locale. If you use the constructor for the DateTime structure, you need to specify year, month, day, hour, minute, and second arguments and then use the ToString method with the "g" format specifier to convert the date-time value to the short date and short time string expected by Outlook. The date specifier argument to the ToString method is case-sensitive, so be sure to use "g" as the format specifier. When you use the DateTime constructor and the "g" format specifier in the ToString method, you create a date-time literal that will be interpreted correctly by Outlook.

I would create the filters like in the link, creating a DateTime object and then converting it to string using the "g" parameter. From the documentation:

string searchCriteria = String.Format("\"urn:schemas:httpmail:datereceived\" >= '{0}'", new DateTime(2005, 6, 12, 15, 30, 0).ToString("g"))

That way you can be sure the date will be properly formatted according to the culture of the system the query is running on.

Note: Remember it is Case sensitive so you must use "g" and not "G".

Upvotes: 1

Related Questions