当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] SharePoint Online:用于上传母版页的 PowerShell

作者:精品下载站 日期:2024-12-14 14:18:22 浏览:14 分类:玩电脑

SharePoint Online:用于上传母版页的 PowerShell


要求:将母版页上传到 SharePoint Online。

如何将母版页添加到 SharePoint Online?

要将母版页添加到 SharePoint Online,请执行以下操作:

  1. 登录到您的 SharePoint Online 站点 >> 单击“设置”>>“站点设置”
  2. 在“站点设置”页面上,单击“Web 设计师库”下的“母版页”。该库与任何其他文档库类似。

    [玩转系统] SharePoint Online:用于上传母版页的 PowerShell

  3. 单击“上传文档”链接,浏览并找到您的自定义母版页并完成上传。

    [玩转系统] SharePoint Online:用于上传母版页的 PowerShell

  4. 上传后,您可以从 SharePoint Web UI 或 PowerShell 更改母版页。

PowerShell 将母版页上传到 SharePoint Online

此 PowerShell 脚本从指定位置获取母版页并将其上传到 SharePoint Online。


#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\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

请注意,此脚本仅将母版页上传到给定的站点 URL。如果要将母版页上传到下面的所有子网站,请取消注释这些行。上传母版页后,您可以使用另一篇文章在 SharePoint Online 中应用母版页:如何使用 PowerShell 在 SharePoint Online 中设置自定义母版页?

SharePoint Online:使用 PnP PowerShell 部署自定义母版页

以下是用于在 SharePoint Online 中上传母版页的 PnP PowerShell:


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

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯