Query Options的一些用法(4):UTC时间处理
接着上一篇Query Options的一些用法(3): 分页,这一篇也是介绍一下Lists.GetListItems方法文档上已经存在的一个:DateInUtc。
DateInUtc |
TRUE to return dates in Coordinated Universal Time (UTC) format. FALSE to return dates in ISO format. This element is optional, and its default value is False.(注意,这里文档在SharePoint2010环境下错误了,默认值应该是FALSE。见下面示例。) |
首先先运行下没有传入Query Options的方法。
static void Main(string[] args)
{
Lists client = new Lists();
client.Url = webUrl + "/_vti_bin/lists.asmx";
client.Credentials = System.Net.CredentialCache.DefaultCredentials;
XmlNode resultNode = client.GetListItems(listName, String.Empty, QueryAll, ViewFields, "1", null, String.Empty);
Console.ReadLine();
}
得到如下结果,注意看创建和修改时间,发现这里并没有使用UTC时间,所以默认值应该是FALSE。
<rs:data ItemCount="1" ListItemCollectionPositionNext="Paged=TRUE&p_ID=1" xmlns:rs="urn:schemas-microsoft-com:rowset">
<z:row ows_Title="Test01" ows_Attachments="1" ows_Author="1;#Qin, Lei (Lambert, ES-Apps-GD-China-SH)" ows_Editor="1;#Qin, Lei (Lambert, ES-Apps-GD-China-SH)" ows_MetaInfo="1;#" ows__ModerationStatus="0" ows__Level="1" ows_ID="1" ows_UniqueId="1;#{E1F2046C-16AC-4650-8EE3-73E12ED5E833}" ows_owshiddenversion="4" ows_FSObjType="1;#0" ows_Created="2012-11-30 15:37:17" ows_PermMask="0x7fffffffffffffff" ows_Modified="2012-12-13 10:42:20" ows_FileRef="1;#teams/smetechcom/Lists/QueryOptionsDemo/1_.000" xmlns:z="#RowsetSchema" />
</rs:data>
这里,我使用<DateInUtc>FALSE</DateInUtc>进行验证,发现的确如此。
static XmlNode QueryOptions
{
get
{
XmlElement queryOptions = _camlDoc.CreateElement("QueryOptions");
queryOptions.InnerXml = "<DateInUtc>FALSE</DateInUtc>";
return queryOptions;
}
}
static void Main(string[] args)
{
Lists client = new Lists();
client.Url = webUrl + "/_vti_bin/lists.asmx";
client.Credentials = System.Net.CredentialCache.DefaultCredentials;
XmlNode resultNode = client.GetListItems(listName, String.Empty, QueryAll, ViewFields, "1", QueryOptions, String.Empty);
Console.ReadLine();
}
<rs:data ItemCount="1" ListItemCollectionPositionNext="Paged=TRUE&p_ID=1" xmlns:rs="urn:schemas-microsoft-com:rowset">
<z:row ows_Title="Test01" ows_Attachments="1" ows_Author="1;#Qin, Lei (Lambert, ES-Apps-GD-China-SH)" ows_Editor="1;#Qin, Lei (Lambert, ES-Apps-GD-China-SH)" ows_MetaInfo="1;#" ows__ModerationStatus="0" ows__Level="1" ows_ID="1" ows_UniqueId="1;#{E1F2046C-16AC-4650-8EE3-73E12ED5E833}" ows_owshiddenversion="4" ows_FSObjType="1;#0" ows_Created="2012-11-30 15:37:17" ows_PermMask="0x7fffffffffffffff" ows_Modified="2012-12-13 10:42:20" ows_FileRef="1;#teams/smetechcom/Lists/QueryOptionsDemo/1_.000" xmlns:z="#RowsetSchema" />
</rs:data>
最后,设置显示UTC时间,看看结果有什么不同。
static XmlNode QueryOptions
{
get
{
XmlElement queryOptions = _camlDoc.CreateElement("QueryOptions");
queryOptions.InnerXml = "<DateInUtc>TRUE</DateInUtc>";
return queryOptions;
}
}
<rs:data ItemCount="1" ListItemCollectionPositionNext="Paged=TRUE&p_ID=1" xmlns:rs="urn:schemas-microsoft-com:rowset">
<z:row ows_Title="Test01" ows_Attachments="1" ows_Author="1;#Qin, Lei (Lambert, ES-Apps-GD-China-SH)" ows_Editor="1;#Qin, Lei (Lambert, ES-Apps-GD-China-SH)" ows_MetaInfo="1;#" ows__ModerationStatus="0" ows__Level="1" ows_ID="1" ows_UniqueId="1;#{E1F2046C-16AC-4650-8EE3-73E12ED5E833}" ows_owshiddenversion="4" ows_FSObjType="1;#0" ows_Created="2012-11-30T07:37:17Z" ows_PermMask="0x7fffffffffffffff" ows_Modified="2012-12-13T02:42:20Z" ows_FileRef="1;#teams/smetechcom/Lists/QueryOptionsDemo/1_.000" xmlns:z="#RowsetSchema" />
</rs:data>
为什么我们会需要UTC时间?
在我看来主要需要解决的就是一个不同时区的问题。SharePoint中,界面显示的都是本地时间。每个用户可以修改自己的显示时区。如下图
对于开发人员来说,一旦你忽略了时区,用自己的本地时区去查询一个和你不同时区的站点数据的时候,查询结果的数量就会产生问题。
所以推荐使用这个选项,来进行你的每一次查询。