SharePoint Online: PowerShell để tải lên trang chính
Yêu cầu: Tải trang chính lên SharePoint Online.
Làm cách nào để thêm trang chính vào SharePoint Online?
Để thêm trang chính vào SharePoint Online, hãy làm như sau:
- Đăng nhập vào trang SharePoint Online của bạn >> Nhấp vào Cài đặt >> Cài đặt trang
Trên trang Cài đặt trang, nhấp vào “Trang chính” trong Phòng trưng bày thiết kế web. Thư viện này tương tự như bất kỳ thư viện tài liệu nào khác.
Nhấp vào liên kết “Tải tài liệu lên”, duyệt và định vị trang chính tùy chỉnh của bạn và hoàn tất quá trình tải lên.
- Sau khi tải lên, bạn có thể thay đổi trang chính từ giao diện người dùng web SharePoint hoặc PowerShell.
PowerShell để tải trang chính lên SharePoint Online
Tập lệnh PowerShell này lấy trang Chính từ vị trí được chỉ định và tải nó lên 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"
Function Upload-CustomMasterPage()
{
param
(
[Parameter(Mandatory=$true)] [string] $SiteURL,
[Parameter(Mandatory=$true)] [string] $MasterPageLocation
)
Try {
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Credentials
#Get the Web
$Web = $Ctx.web
$Ctx.Load($Web)
#Get the Master Page Gallery
$MasterPageGallery = $Web.Lists.GetByTitle("Master Page Gallery")
$Ctx.Load($MasterPageGallery)
$Ctx.ExecuteQuery()
#Get the Master page from given local location
$MasterPageFile = Get-Item $MasterPageLocation
$MasterPageContent = [System.IO.File]::ReadAllBytes($MasterPageFile.FullName)
#Prepare the File to upload Master page Gallary
$FileCreationInfo = New-Object Microsoft.SharePoint.Client.FileCreationInformation
$FileCreationInfo.Overwrite = $True
$FileCreationInfo.Content = $MasterPageContent
$FileCreationInfo.URL = $MasterPageFile.name
Write-host "Uploading Master Page to Site:"$SiteURL
#Upload the File to Master page Gallary
$MasterPage = $MasterPageGallery.RootFolder.Files.Add($FileCreationInfo)
$Ctx.ExecuteQuery()
#Check-Out and Check-In to create major version
$MasterPage.CheckOut()
$Ctx.ExecuteQuery()
$CheckIn = $MasterPage.CheckIn("Master Page Uploaded",[Microsoft.SharePoint.Client.CheckinType]::MajorCheckIn)
$Ctx.ExecuteQuery()
Write-Host "Master Page '$MasterPageFile' Uploaded to site '$SiteURL'" -f Green
<#Process subsites - Call the function recursively
$Ctx.Load($Web.Webs)
$Ctx.ExecuteQuery()
If($Web.Webs.Count -gt 0)
{
Foreach ($web in $Web.Webs)
{
Upload-CustomMasterPage -SiteURL $web.Url -MasterPageLocation $MasterPageLocation
}
}#>
}
Catch {
write-host -f Red "Error Uploading Custom Master Page!" $_.Exception.Message
}
}
#Set parameter values
$SiteURL="https://crescent.sharepoint.com/sites/sales/"
$MasterPageLocation="C:\Deployment\Crescent.master"
#Setup Credentials to connect
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
#Call the function to change master page
Upload-CustomMasterPage -SiteURL $SiteURL -MasterPageLocation $MasterPageLocation
Xin lưu ý, tập lệnh này chỉ tải trang chính lên URL trang web đã cho. Nếu bạn muốn tải trang chính lên tất cả các trang con bên dưới, hãy bỏ ghi chú các dòng. Sau khi trang Chính được tải lên, bạn có thể sử dụng bài viết khác này để áp dụng trang chính trong SharePoint Online: Cách đặt Trang chính tùy chỉnh trong SharePoint Online bằng PowerShell?
SharePoint Online: Triển khai trang Chính tùy chỉnh bằng PnP PowerShell
Đây là PnP PowerShell để tải lên trang chính trong SharePoint Online:
#Config Variables
$SiteURL = "https://Crescent.sharepoint.com/sites/marketing"
$MasterPagePath = "C:\Users\salaudeen\Downloads\CrescentV2.master"
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
#Upload Master page to SharePoint Online site
Add-PnPMasterPage -SourceFilePath $MasterPagePath -Title "CrescentV2" -Description "Crescent V2 Master Page" -DestinationFolderHierarchy "CrescentV2"

