ค้นหาเว็บไซต์

SharePoint Online: กรองรายการตามวันที่สร้างหรือแก้ไขโดยใช้ PowerShell


ความต้องการ: กรองและรับรายการตามค่าวันที่ที่สร้างหรือแก้ไขใน SharePoint Online โดยใช้ PowerShell

จะกรองและรับรายการที่สร้างขึ้นใน 30 วันที่ผ่านมาโดยใช้ PowerShell ได้อย่างไร

นี่คือตัวอย่าง PowerShell เพื่อรับรายการที่สร้างขึ้นในช่วง 30 วันที่ผ่านมาในรายการ SharePoint Online


#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"
 
#Variables
$SiteURL = "https://crescent.sharepoint.com/sites/marketing"
$ListName = "Documents"
 
Try {
    #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
    $List = $Ctx.Web.lists.GetByTitle($ListName)

    #Define CAML Query to Filter
    $Query = New-Object Microsoft.SharePoint.Client.CamlQuery
    $Query.ViewXML = "<View>
                        <Query>
                            <Where>
                                <Gt>
                                    <FieldRef Name='Created' Type='DateTime'/>
                                    <Value Type='DateTime'>
                                        <Today OffsetDays='-30'/>
                                    </Value>
                                </Gt>
                            </Where>
                        </Query>
                    </View>"

    #Get List Items
    $ListItems = $List.GetItems($Query)
    $Ctx.Load($ListItems)
    $Ctx.ExecuteQuery()

    Write-host -f Green "Number of List Items Found:"$ListItems.Count
    #Get Each Item's Created Date
    $ListItems | ForEach-Object { Write-host ("List Item:{0} was created on {1}" -f $_["FileLeafRef"],$_["Created"]) }
}
Catch {
    write-host -f Red "Error:" $_.Exception.Message
}

PnP PowerShell เพื่อกรองรายการที่แก้ไขใน 24 ชั่วโมงที่ผ่านมา:

ในทำนองเดียวกัน เราสามารถกรองรายการตามวันที่แก้ไขล่าสุดด้วย PnP PowerShell ได้ดังนี้:


#Parameter
$SiteURL= "https://crescent.sharepoint.com/sites/marketing/"
$ListName = "Documents"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

#Define Query to Filter
$Query= "<View>
            <Query>
                <Where>
                    <Gt>
                        <FieldRef Name='Modified' Type='DateTime'/>
                        <Value Type='DateTime' IncludeTimeValue='TRUE'>
                            <Today OffsetDays='-1'/>
                        </Value>
                    </Gt>
                </Where>
            </Query>
        </View>"

$ListItems = Get-PnPListItem -List $ListName -Query $Query
Write-host "Total Number of Items Found:"$ListItems.count

#Get Each Item's Modified Date
$ListItems | ForEach-Object { Write-host ("List Item:{0} was Modified on {1}" -f $_["FileLeafRef"],$_["Modified"]) }

โดยสรุป การเรียกข้อมูลรายการใน SharePoint Online ตามวันที่สร้างหรือแก้ไขสามารถทำได้อย่างง่ายดายด้วยความช่วยเหลือของ PowerShell ด้วยการทำตามขั้นตอนที่อธิบายไว้ในบทความนี้ คุณสามารถใช้ PowerShell เพื่อดึงข้อมูลรายการสินค้าตามวันที่สร้างหรือแก้ไขได้อย่างรวดเร็วและมีประสิทธิภาพ

บทความที่เกี่ยวข้อง