SharePoint Online:PowerShell 中日期时间字段的 CAML 查询
要求:SharePoint Online CAML 查询按日期时间字段值过滤。
SharePoint CAML:按创建日期排序
协作应用程序标记语言 (CAML) 是一种基于 XML 的语言,用于定义和查询 SharePoint 数据。在本文中,我们将探讨如何使用 CAML 查询 SharePoint 中的日期和时间字段。
以下是使用“创建日期”字段进行的 SharePoint Online CAML 查询。
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
#Set parameter values
$SiteURL="https://crescent.sharepoint.com/"
$ListName="Projects"
#Get Credentials to connect
$Cred= Get-Credential
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
#Get the List Object
$List = $Ctx.Web.lists.GetByTitle($ListName)
#Define the CAML Query
$Query = New-Object Microsoft.SharePoint.Client.CamlQuery
$Query.ViewXml = "@
<View Scope='RecursiveAll'>
<Query>
<OrderBy><FieldRef Name='Created' Ascending='FALSE' /></OrderBy>
</Query>
</View>"
#Get All List Items matching the query
$ListItems = $List.GetItems($Query)
$Ctx.Load($ListItems)
$Ctx.ExecuteQuery()
Write-host "Total Number of Items:"$ListItems.count
#Loop through each List Item
ForEach($Item in $ListItems)
{
#Do something
Write-host "Item: $($Item["Title"]) Created On: $($Item["Created"])"
}
按日期字段过滤的 CAML 查询:
CAML 按日期过滤,例如获取项目列表项目,其中“项目开始日期”等于:2017 年 6 月 20 日
<View>
<Query>
<Where>
<Eq>
<FieldRef Name='ProjectStartDate' /><Value Type='DateTime'>2017-06-20T12:00:00Z</Value>
</Eq>
</Where>
</Query>
</View>
SharePoint Online:带有“今天”示例的 CAML 查询:
获取项目开始日期为今天的所有项目。
<View>
<Query>
<Where>
<Gt>
<FieldRef Name='ProjectStartDate' /><Value Type='DateTime'><Today /></Value>
</Gt>
</Where>
</Query>
</View>
这是另一个示例:获取所有项目,其中项目开始日期是从今天开始的 10 天内。
<View Scope='RecursiveAll'>
<Query>
<Where>
<Geq>
<FieldRef Name='ProjectStartDate' />
<Value Type='DateTime'><Today OffsetDays='10' /></Value>
</Geq>
</Where>
</Query>
</View>"
您还可以使用 OffsetDays 作为“-10”来获取开始日期在过去 10 天内的项目。
CAML 查询大于日期
或者您可以指定特定日期:例如,2017 年 1 月 1 日之后的所有日期。
<Gt><FieldRef Name='ProjectStartDate' /><Value Type='DateTime'>2017-01-01T12:00:00Z</Value></Gt>
用于在日期之间进行筛选的 SharePoint CAML 查询
假设,我们希望获取项目开始日期在 2017 年 1 月 1 日至 2017 年 12 月 31 日之间的所有项目。以下是针对日期范围的 SharePoint CAML 查询。
<View Scope='RecursiveAll'>
<Query>
<Where>
<And>
<Geq>
<FieldRef Name='ProjectStartDate' />
<Value Type='DateTime'>2017-01-01T12:00:00Z</Value>
</Geq>
<Leq><FieldRef Name='ProjectStartDate' />
<Value Type='DateTime'>2017-12-31T12:00:00Z</Value>
</Leq>
</And>
</Where>
</Query>
</View>
包含时间的 SharePoint CAML 日期时间
这是 PowerShell for SharePoint CAML 日期时间字段,其中包含时间和筛选器 - 范围:
<View Scope='RecursiveAll'>
<Query>
<Where>
<And>
<Geq>
<FieldRef Name='ProjectStartDate' />
<Value Type='DateTime' IncludeTimeValue='TRUE'>2017-01-05T09:00:00Z</Value>
</Geq>
<Leq>
<FieldRef Name='ProjectStartDate' />
<Value Type='DateTime' IncludeTimeValue='TRUE'>2017-01-05T17:00:00Z</Value>
</Leq>
</And>
</Where>
</Query>
</View>
要将日期时间字段值转换为 CAML 格式,请使用以下命令:
[System.DateTime]::Now.ToString("yyyy-MM-ddTHH:mm:ssZ")
最后但并非最不重要的一点是:使用 CAML 查询生成器工具生成 SharePoint Online 的 CAML 查询。