Tìm kiếm trang web

SharePoint Online: PowerShell để đặt giá trị trường ngày


PowerShell để lấy giá trị cột ngày trong SharePoint Online:

Các trường ngày giờ thường được sử dụng trong danh sách và thư viện SharePoint Online để ghi lại và hiển thị ngày giờ. Các trường này có thể được đặt để chỉ hiển thị ngày hoặc ngày và giờ. Bằng cách sử dụng SharePoint Online PowerShell, bạn có thể dễ dàng lấy hoặc đặt giá trị của trường ngày giờ trong danh sách hoặc thư viện, giúp bạn tự động hóa các tác vụ và tiết kiệm thời gian.

Hãy tìm hiểu cách sử dụng PowerShell để lấy giá trị của trường ngày và giờ trong SharePoint Online. Điều này có thể hữu ích cho việc báo cáo hoặc các tác vụ tự động hóa khác. Chúng tôi cũng sẽ chia sẻ các tập lệnh PowerShell để cập nhật các cột ngày giờ trong 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"
 
#Set parameter values
$SiteURL="https://Crescent.sharepoint.com/"
$ListName="Projects"
$FieldName="StartDate"

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

#Get All List Items
$ListItems = $List.GetItems([Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery()) 
$Ctx.Load($ListItems)
$Ctx.ExecuteQuery()     

#Loop through each List Item and get Date Field Value
ForEach($Item in $ListItems)
{ 
    #Get Date & Time of the Field - E.g. Wednesday, February 01, 2017 12:00:00 AM
    $Item[$FieldName]
    
    #Get Date Alone - 2/1/2017
    Get-Date($Item[$FieldName]) -format "d"

    #Get Date in specific format E.g. dd/MM/yyyy HH:mm:ss
    Get-Date ($Item[$FieldName]) -Format "dd-MMM-yyyy"
    
    #Long Date Format - E.g. Wednesday, February 01, 2017
    (Get-Date($Item[$FieldName])).ToLongDateString()

    #Get Time Alone - E.g. 8:00 PM
    (Get-Date($Item[$FieldName])).ToShortTimeString()

    #Get Long Time Format - E.g. 12:30:25 AM
    (Get-Date($Item[$FieldName])).ToLongTimeString()

    #To Universal Date time format: E.g. Tuesday, January 19, 2016 8:00:00 PM
    (Get-Date($Item[$FieldName])).ToUniversalTime()

    #Get Day-Month-Year/Hour-Minute-Second-millisecond from the Date
    (Get-Date($Item[$FieldName])).Year
}

SharePoint Online: PowerShell để đặt trường ngày

Đây là PowerShell để cập nhật cột ngày giờ trong SharePoint Online:


#Set parameter values
$SiteURL="https://Crescent.sharepoint.com/"
$ListName="Projects"
$FieldName="StartDate"

#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 the CAML Query to filter list items
$Query = New-Object Microsoft.SharePoint.Client.CamlQuery
$Query.ViewXml = "@
<View>
    <Query>
        <Where>
            <Eq>
                <FieldRef Name='Title' /><Value Type='Text'>SharePoint 2016 Upgrade</Value>
            </Eq>
        </Where>
    </Query>
</View>"
 
#Get the List Items matching the query
$ListItems = $List.GetItems($Query)
$Ctx.Load($ListItems)
$Ctx.ExecuteQuery()
  
$ListItems | ForEach-Object { 
    #Set Start Date Field to 7 Days from Today's Date
    $StartDate = (Get-Date).AddDays(7)

    $_[$FieldName] = $StartDate 
    $_.Update()  
    $Ctx.ExecuteQuery()

    Write-host "Date Field Updated!"
}

Làm cách nào để chuyển đổi chuỗi thành ngày?
Bạn có thể sử dụng: Get-Date “10/02/1984″ hoặc [DateTime]”10/02/1984”

Bài viết liên quan