Tìm kiếm trang web

SharePoint Online: Truy vấn CAML cho các trường ngày giờ trong PowerShell


Yêu cầu: Truy vấn CAML trực tuyến SharePoint để lọc theo giá trị trường ngày giờ.

SharePoint CAML: Sắp xếp theo ngày tạo

Ngôn ngữ đánh dấu ứng dụng cộng tác (CAML) là ngôn ngữ dựa trên XML được sử dụng để xác định và truy vấn dữ liệu SharePoint. Trong bài viết này, chúng ta sẽ khám phá cách sử dụng CAML để truy vấn các trường Ngày và Giờ trong SharePoint.

Đây là Truy vấn CAML của SharePoint Online để đặt hàng bằng cách sử dụng trường “Ngày tạo”.


#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"])"
}

Truy vấn CAML để lọc theo trường ngày:

CAML để lọc theo ngày, Nói, Nhận các mục Danh sách dự án trong đó “Ngày bắt đầu dự án” bằng: ngày 20 tháng 6 năm 2017


<View>
    <Query>
        <Where>
            <Eq>
                <FieldRef Name='ProjectStartDate' /><Value Type='DateTime'>2017-06-20T12:00:00Z</Value>
            </Eq>
        </Where>
    </Query>
</View>

SharePoint Online: Truy vấn CAML với ví dụ "Hôm nay":

Nhận tất cả các dự án có ngày bắt đầu dự án là hôm nay.


<View>
    <Query>
        <Where>
            <Gt>
                <FieldRef Name='ProjectStartDate' /><Value Type='DateTime'><Today /></Value>
            </Gt>
        </Where>
    </Query>
</View>

Đây là một ví dụ khác: Nhận tất cả dự án, trong đó ngày bắt đầu dự án là trong vòng 10 ngày tới kể từ ngày Hôm nay.


<View Scope='RecursiveAll'>
    <Query>        
        <Where>
                <Geq>
                    <FieldRef Name='ProjectStartDate' />
                         <Value Type='DateTime'><Today OffsetDays='10' /></Value>
                </Geq>
        </Where>
    </Query>
</View>"

Bạn cũng có thể sử dụng OffsetDays dưới dạng “-10” để nhận các dự án có ngày bắt đầu trong 10 ngày qua.

Truy vấn CAML cho ngày lớn hơn
Hoặc bạn có thể chỉ định một ngày cụ thể: Giả sử, tất cả các ngày sau ngày 1 tháng 1 năm 2017.


<Gt><FieldRef Name='ProjectStartDate' /><Value Type='DateTime'>2017-01-01T12:00:00Z</Value></Gt>

Truy vấn SharePoint CAML để lọc giữa các ngày
Giả sử chúng tôi muốn nhận tất cả các dự án có ngày bắt đầu dự án từ ngày 1 tháng 1 năm 2017 đến ngày 31 tháng 12 năm 2017. Đây là truy vấn SharePoint CAML cho phạm vi ngày .


<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>

Ngày giờ SharePoint CAML cần bao gồm thời gian
Đây là trường Ngày giờ PowerShell cho SharePoint CAML, bao gồm thời gian và bộ lọc - Phạm vi:


<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>

Để chuyển đổi các giá trị trường ngày giờ sang định dạng CAML, hãy sử dụng như sau:


[System.DateTime]::Now.ToString("yyyy-MM-ddTHH:mm:ssZ")

Cuối cùng nhưng không kém phần quan trọng: Sử dụng Công cụ tạo truy vấn CAML để tạo Truy vấn CAML cho SharePoint Online.

Bài viết liên quan