SharePoint Online: Xuất-Nhập cột trang web bằng PowerShell
Yêu cầu: Xuất-Nhập cột trang web trong SharePoint Online.
SharePoint Online: Xuất cột trang web bằng PowerShell
Vì vậy, bạn có rất nhiều Cột Trang trong môi trường SharePoint Online của mình. Và cần xuất và nhập các Cột Trang web đó giữa các môi trường? Thực hiện việc này thông qua giao diện trình duyệt web có thể tẻ nhạt, vì vậy tôi đã viết một số tập lệnh PowerShell giúp quá trình này trở nên dễ dàng hơn nhiều. Trong bài đăng này, tôi sẽ chỉ cho bạn cách xuất và nhập Cột trang giữa các trang SharePoint Online (hoặc giữa các Người thuê SharePoint Online khác nhau) bằng PowerShell.
Tập lệnh PowerShell này xuất tất cả các cột trang của một nhóm cụ thể cho trang SharePoint Online sang tệp XML:
#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"
#Function to export site columns from a group
Function Export-SPOSiteColumns([String]$SiteURL, [String]$SiteColumnGroup,[String]$XMLFile)
{
Try{
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
#Get All Site Columns of the Web
$Ctx.Load($Ctx.Web)
$Ctx.Load($Ctx.Web.Fields)
$Ctx.ExecuteQuery()
#Get the site column Group
$SiteColumns = $Ctx.Web.Fields | where {$_.Group -eq $SiteColumnGroup}
if($SiteColumns -ne $NULL)
{
#Wrap Field Schema XML inside <Fields> Element
Add-Content $XMLFile "`n<Fields>"
#Loop through each site column of the group
ForEach($Field in $SiteColumns)
{
Add-Content $XMLFile $Field.SchemaXml
}
#Closing Wrapper
Add-Content $XMLFile "</Fields>"
Write-host -f Green "Site Columns Exported Successfully!"
}
else
{
Write-host -f Yellow "Could not find the Site Column Group '$($SiteColumnGroup)' at '$($SiteURL)'"
}
}
Catch {
write-host -f Red "Error Exporting Site Columns!" $_.Exception.Message
}
}
#Set Config Parameters
$SiteURL="https://crescent.sharepoint.com"
$SiteColumnGroup="Crescent Projects"
$XMLFile="C:\Temp\CrescentSiteColumns.xml"
#Get Credentials to connect
$Cred= Get-Credential
#Call the function to export the Site Column group to XML
Export-SPOSiteColumns $SiteURL $SiteColumnGroup $XMLFile
Sau khi được thực thi, tập lệnh PowerShell này sẽ xuất tất cả các cột của trang từ nhóm cột trang đã cho sang tệp XML như sau:
Nhập cột trang web từ tệp XML bằng PowerShell:
Bước tiếp theo là nhập chúng vào một môi trường hoặc trang web SharePoint Online khác.
#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"
#Function to Import site columns from a XML file
Function Import-SPOSiteColumns([String]$SiteURL, [String]$XMLFile)
{
Try{
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
#Get XML file contents
[xml]$FieldsXML = Get-Content($XMLFile)
#Remove the "Version" Attribute from the XML
$FieldsXML.Fields.Field.RemoveAttribute("Version")
#Loop Through Each Field
ForEach($Field in $FieldsXML.Fields.Field)
{
#Check if the web has the field already!
$Ctx.Load($Ctx.Web.Fields)
$Ctx.ExecuteQuery()
$SiteColumn = $Ctx.Web.Fields | Where {$_.InternalName -eq $Field.Name}
If($SiteColumn -eq $Null)
{
#Add Site column to Web
$SiteColumn = $Ctx.Web.Fields.AddFieldAsXml($Field.OuterXml, $True,[Microsoft.SharePoint.Client.AddFieldOptions]::AddFieldToDefaultView)
$Ctx.ExecuteQuery()
Write-host "Site Column Added:" $($Field.Name)
}
Else
{
Write-host -f Yellow "Site Column '$($Field.Name)' Already Exists!"
}
}
Write-host -f Green "Site Columns Imported Successfully!"
}
Catch {
write-host -f Red "Error Importing Site Columns!" $_.Exception.Message
}
}
#Set Config Parameters
$SiteURL="https://crescent.sharepoint.com/sites/marketing"
$XMLFile="C:\Temp\CrescentSiteColumns.xml"
#Get Credentials to connect
$Cred= Get-Credential
#Call the function to Import the Site Columns from XML
Import-SPOSiteColumns $SiteURL $XMLFile
Đây là một bài đăng khác về xuất và nhập các cột trang trong SharePoint On-Premise bằng PowerShell: Làm cách nào để Xuất-Nhập Cột Trang trong SharePoint bằng PowerShell?
