我有一个采用 UTC+2 时区的 SharePoint 在线网站。我需要了解当我使用 PnP Core SDK 版本 1.11.0 从 SharePoint 获取数据时,任何日期时间 SharePoint 列的时区将如何显示?
这是我的案例:-
1)我的 SharePoint Online 网站时区是 UTC+2
2)我有一个使用 PnP Core SDK 版本 1.11.0 的 .net core 控制台应用程序
3)具有 Visual Studio 2022 的托管服务器也具有 UTC+2 时区。
4)我正在使用此代码从 SharePoint 列表中获取所有项目:-
var BCDList = context.Web.Lists.GetByTitle("BookingCalendarDetails", p => p.Title,
p => p.Fields.QueryProperties(p => p.InternalName,
p => p.FieldTypeKind,
p => p.TypeAsString,
p => p.Title));
// Build a query that only returns these fields for the Work Orders
string viewXml10_20 = @"<View Scope='RecursiveAll'>
<ViewFields>
<FieldRef Name='Title' />
<FieldRef Name='ContractType' />
<FieldRef Name='Created' />
<FieldRef Name='FileLeafRef' />
<FieldRef Name='Amount' />
<FieldRef Name='BookingCalendarID' />
<FieldRef Name='NetworkID' />
<FieldRef Name='StartDate1' />
<FieldRef Name='EndDate1' />
<FieldRef Name='MediumID' />
<FieldRef Name='MediumTitleSync' />
</ViewFields>
<Query>
<Where>
<And>
<And>
<Eq>
<FieldRef Name='FSObjType' />
<Value Type='Integer'>0</Value>
</Eq>
<Eq>
<FieldRef Name='IsCanceled' />
<Value Type='Boolean'>0</Value>
</Eq>
</And>
<Eq>
<FieldRef Name='ContractType' />
<Value Type='Choice'>Out of Home</Value>
</Eq>
</And>
</Where>
</Query>
<RowLimit Paged='TRUE'>5000</RowLimit>
</View>";
// Load all the needed data using paged requests
bool paging10_20 = true;
string nextPage10_20 = null;
while (paging10_20)
{
var output = await BCDList.LoadListDataAsStreamAsync(new RenderListDataOptions()
{
ViewXml = viewXml10_20,
RenderOptions = RenderListDataOptionsFlags.ListData,
Paging = nextPage10_20 ?? null,
}).ConfigureAwait(false);
if (output.ContainsKey("NextHref"))
{
nextPage10_20 = output["NextHref"].ToString().Substring(1);
}
else
{
paging10_20 = false;
}
}
现在对于 ID = 1838 的列表项,我已将 StartDate1 添加为 1/10/2024 + EndDate1 添加为 1/11/2024,它们将在 SharePoint UI 中正确显示,如下所示:-
当我使用 SharePoint REST API 使用此 url 获取此项目时
https://******.sharepoint.com/sites/PPM/_api/web/lists/getbytitle('bookingcalendardetails')/items(1838)
,,,,StartDate1 和 EndDate1 将存储在 UTC 中,如下所示:-:-
<d:StartDate1 m:type="Edm.DateTime">2024-09-30T21:00:00Z</d:StartDate1>
<d:EndDate1 m:type="Edm.DateTime">2024-10-31T22:00:00Z</d:EndDate1>
现在,当我使用 Visual Studio 运行控制台应用程序时,将根据站点时区显示 StartDate1 和 EndDate1,如下所示:-
那么PnP Core SDK是如何正确进行转换的呢?我认为 Visual Studio 会显示 UTC 时区的日期,因为这是 SharePoint Online 存储日期的方式。所以我不确定我的代码如何获取正确时区(即网站时区)的日期?难道Visual Studio是怎么转换的?或者此转换将由 PnP Core SDK 根据 sharepoint 站点时区完成?或者此转换将由基于托管服务器(.net 控制台应用程序运行的位置)的 PnP Core SDK 完成?
使用 PnP Core SDK 时,会发生
DateTime
值从 UTC 到 SharePoint 网站时区的转换,因为默认情况下 SDK 遵循 SharePoint 网站的时区设置。
以下是此转换如何发生的详细说明:
SharePoint 在线存储:
DateTime
值,正如您在 REST API 响应中注意到的那样(2024-09-30T21:00:00Z
表示 StartDate1
)。PnP 核心 SDK 转换:
PnP Core SDK
获取列表数据时,它会检索 DateTime
值并自动将它们转换为为 SharePoint 网站配置的时区。在您的情况下,站点时区是 UTC+2,因此日期会从 UTC 转换为 UTC+2。Visual Studio/控制台应用程序:
DateTime
值,因为 PnP Core SDK 根据站点时区设置处理此问题。因此,即使 Visual Studio 在同一时区 (UTC+2) 中运行,SharePoint 网站的配置也决定了显示时间。RenderListDataAsStreamAsync:
DateTime
字段时,SDK 会根据站点配置的时区调整 DateTime
值。PnP Core SDK 会根据 SharePoint 网站的时区自动转换
DateTime
值。它不依赖于 Visual Studio 或服务器的时区,而是直接使用 SharePoint 站点设置中设置的时区,在您的情况下为 UTC+2。
此行为可确保当您从 SharePoint 检索
DateTime
值时,即使 SharePoint 将它们存储为 UTC,它们也会在控制台应用程序中正确显示为网站的时区。