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

SharePoint Online: อัปเดตที่สร้างโดย/แก้ไขโดย, สร้าง/แก้ไขค่าฟิลด์โดยใช้ PowerShell


ข้อกำหนด: อัปเดตคอลัมน์ 'สร้างโดย' ใน SharePoint Online โดยใช้ PowerShell

บางครั้ง คุณอาจต้องสร้างรายการหรือตั้งค่าฟิลด์ข้อมูลเมตาของรายการที่มีอยู่ เช่น ค่าที่สร้างโดย แก้ไขโดย สร้าง และแก้ไข ให้กับผู้ใช้และการประทับเวลาที่ระบุ สมมติว่า คุณกำลังนำเข้าหรือย้ายข้อมูลจากไฟล์เครือข่ายที่ใช้ร่วมกันไปยัง SharePoint Online และคุณต้องการเก็บข้อมูลเมตานี้เหมือนกับเนื้อหาต้นฉบับ

เนื่องจาก SharePoint ไม่อนุญาตให้คุณตั้งค่าเหล่านี้จากอินเทอร์เฟซผู้ใช้บนเว็บ เราจึงต้องใช้ PowerShell นี่คือสคริปต์ PowerShell ของฉันสำหรับเปลี่ยนรายละเอียดของใครเป็นผู้สร้างรายการ แก้ไข และเวลาที่ผู้สร้างและแก้ไข สคริปต์นี้ตั้งค่าฟิลด์ระบบ (สร้าง สร้างโดย แก้ไข แก้ไขโดย) สำหรับรายการเฉพาะในรายการ/ไลบรารี ตั้งค่าในส่วนตัวแปรอย่างเหมาะสมและรันสคริปต์

ฉันไม่สนุกกับการจัดการกับช่องข้อมูลเมตาเหล่านี้ เว้นแต่ว่าจำเป็นจริงๆ!

SharePoint Online: การเปลี่ยนแปลงที่สร้างโดยใช้ PowerShell

สคริปต์ PowerShell ที่จะอัปเดต สร้างโดย, แก้ไขโดย, สร้างที่, แก้ไขที่ค่าฟิลด์:


#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 for Processing
$SiteUrl = "https://crescent.sharepoint.com/sites/sales/"
$ListName= "Projects"
$ID=6
$UserID="Salaudeen@crescent.com"
$TimeStamp = "2015/12/01 02:10:00 AM"

#Get Credentials to connect
$Cred = Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
$Ctx.Credentials = $Credentials

#Get the User
$User = $Ctx.Web.EnsureUser($UserID)
$ctx.Load($user)

#Get the list Item
$List=$Ctx.Web.Lists.GetByTitle($ListName)
$ListItem = $List.GetItemById($ID) 
$Ctx.Load($ListItem)

#update created by column sharepoint online powershell
$ListItem["Author"] = $User
#Update Modified By
$ListItem['Editor'] = $User

#Set Created on & Modified on Time values
$ListItem["Created"] =  $TimeStamp
$ListItem["Modified"] = $TimeStamp

#Update List item
$ListItem.Update()
$ctx.ExecuteQuery()

Write-host "Metadata values updated Successfully!" -f Green

สคริปต์ PowerShell นี้ตั้งค่าฟิลด์ข้อมูลเมตาของรายการใน SharePoint Online และผลลัพธ์จะอยู่ที่นี่:

สำหรับ SharePoint ภายในองค์กร ให้ใช้สคริปต์ PowerShell นี้: อัปเดตฟิลด์ข้อมูลเมตา “สร้างโดย”, “แก้ไขล่าสุด” ของรายการโดยใช้ PowerShell

ในทำนองเดียวกัน สำหรับเอกสาร คุณสามารถใช้โค้ดด้านล่างเพื่ออัปเดตข้อมูลเมตาได้:


#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.Runtime.dll"

#Variables
$SiteURL = "https://crescent.sharepoint.com/Sites/Marketing"

#Define Metadata
$CreatedBy = "salaudeen@crescent.com"
$ModifiedBy = "salaudeen@crescent.com"
$CreatedOn = "01/01/2017"
$ModifiedOn ="01/01/2018"

#Get Credentials to connect
$Cred = Get-Credential -Message "Enter the Admin Credentials:"

#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 File
$File =  $Ctx.Web.GetFileByServerRelativeUrl("/sites/marketing/Migrate/Active%20Projects/Requirements.docx")
$Ctx.Load($File)
$Ctx.ExecuteQuery()

#Get 'Created By' and 'Modified By' Users
$Author = $Ctx.Web.EnsureUser($CreatedBy)
$Ctx.Load($Author)
$Editor= $Ctx.Web.EnsureUser($ModifiedBy)
$Ctx.Load($Editor)
$Ctx.ExecuteQuery()

#update Metadata of the File
$ListItem = $File.ListItemAllFields
$Listitem["Author"] = $Author
$ListItem["Editor"] = $Editor
$ListItem["Created"] =  $CreatedOn
$ListItem["Modified"] = $ModifiedOn
$ListItem.Update()
$Ctx.ExecuteQuery()

PnP PowerShell เพื่ออัปเดตข้อมูลเมตาของรายการ

หากต้องการตั้งค่าข้อมูลเมตา เช่น สร้าง แก้ไข สร้างโดย และแก้ไขโดยค่าฟิลด์ ให้ใช้ PnP PowerShell นี้:


#Parameters
$SiteURL= "https://crescent.sharepoint.com/sites/Projects"
$ListName = "Projects"
$ItemID = "2"

$Created = "2018-01-01T12:00:00-00:00"
$Modified = "2018-02-02T10:30:00-00:00"
$CreatedBy= "Salaudeen@crescent.com"
$ModifiedBy= "Salaudeen@crescent.com"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive

Set-PnPListItem -List $ListName -Identity $ItemID -Values @{"Created"=$Created;"Modified"=$Modified; "Author"= $CreatedBy; "Editor"= $ModifiedBy}

คุณสามารถใช้เพื่ออัปเดตข้อมูลเมตาสำหรับทั้งรายการและเอกสารใน SharePoint Online ในทำนองเดียวกัน หากต้องการตั้งค่าข้อมูลเมตาของไฟล์ที่มีอยู่ ให้ใช้:


#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/branding"
$ListName = "Brand"
$SiteRelativeURL = "/Brand/Technical Design.docx"
$CreatedBy= "Salaudeen@crescent.com"
$Title = "New Design Document V2.docx"
 
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
  
#Get the File from SharePoint
$File = Get-PnPFile -Url $SiteRelativeURL -AsListItem
  
#Update document properties
Set-PnPListItem -List $ListName -Identity $File.Id -Values @{"Title" = $Title; "Author" = $CreatedBy; "Editor" = $CreatedBy}

หากต้องการอัปเดตค่าข้อมูลเมตาของโฟลเดอร์ ให้ใช้:


#Parameters
$SiteURL = "https://Crescent.sharepoint.com/sites/Sales"
$ListName = "Documents"
$FolderServerRelativeURL = "/sites/Sales/Shared Documents/Samples"

#Connect to SharePoint Online site
Connect-PnPOnline -Url $SiteURL -Interactive

#Get the Folder
$Folder = Get-PnPFolder -Url $FolderServerRelativeURL -Includes ListItemAllFields

#Update Folder's Created By and Modified By Values
Set-PnPListItem -List $ListName -Identity $Folder.ListItemAllFields.Id -Values @{"Author" = "Steve@Crescent.com"; "Editor"="Steve@Crescent.com" } | Out-Null

อัปเดตที่สร้างโดย แก้ไขโดย สำหรับไฟล์ทั้งหมดในไลบรารีเอกสาร

คราวนี้มาอัปเดตค่าเขตข้อมูลที่สร้างโดย ปรับเปลี่ยนโดย สร้าง และปรับเปลี่ยนสำหรับไฟล์ทั้งหมดในไลบรารีเอกสาร SharePoint Online


#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/Marketing"
$ListName = "Branding"
$CreatedBy = "Salaudeen@crescent.com"
$ModifiedBy = "Salaudeen@crescent.com"
$Created = "2018-01-01T12:00:00-00:00"
$Modified = "2018-02-02T10:30:00-00:00"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
   
#Get all Files from the document library
$ListItems = Get-PnPListItem -List $ListName -PageSize 2000 -Fields Author | Where {$_.FileSystemObjectType -eq "File"}
   
#Update document properties
$ListItems | ForEach-Object { 
    Set-PnPListItem -List $ListName -Identity $_.Id -Values @{"Created"=$Created; "Modified"=$Modified; "Author" = $CreatedBy; "Editor" = $ModifiedBy; } | Out-Null
    Write-host "Metadata Set to "$_.FieldValues.FileRef -f Green
    }

สคริปต์นี้จะสแกนและอัปเดตข้อมูลเมตาของเอกสารทั้งหมดที่จัดเก็บไว้ในไลบรารี รวมถึงไฟล์จากทุกโฟลเดอร์

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