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

[玩转系统] SharePoint Online:使用 PowerShell 创建发布页面

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

SharePoint Online:使用 PowerShell 创建发布页面


要求:使用 PowerShell 在 SharePoint Online 中创建发布页面。

SharePoint Online:使用 PowerShell CSOM 创建发布页面

让我向您展示如何使用 PowerShell 创建发布页面。在运行此脚本之前,请确保您已启用发布功能:


#Config Variables
$SiteURL = "https://Crescent.sharepoint.com/sites/Marketing"
$PageName = "About-us.aspx"
$PageTitle= "About Us"
$PageLayoutName = "BlankWebPartPage.aspx"
$PageContent="Out Fund brings together healthcare technology companies, foundations, development financing institutions and institutional investors,`
              to address and impact poor healthcare outcomes in Africa and Asia. Its healthcare investments include Care Hospitals in India"

#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 publishing Web  
$PublishingWeb = [Microsoft.SharePoint.Client.Publishing.PublishingWeb]::GetPublishingWeb($Ctx, $Ctx.Web)  
$ctx.Load($PublishingWeb) 
$Ctx.ExecuteQuery()

Write-host -f Yellow "Getting Page Layout..." -NoNewline
#Get the Page Layout
$RootWeb = $Ctx.Site.RootWeb
$MasterPageList = $RootWeb.Lists.GetByTitle('Master Page Gallery')
$CAMLQuery = New-Object Microsoft.SharePoint.Client.CamlQuery
$CAMLQuery.ViewXml = "<View><Query><Where><Eq><FieldRef Name='FileLeafRef' /><Value Type='Text'>$PageLayoutName</Value></Eq></Where></Query></View>"
$PageLayouts = $MasterPageList.GetItems($CAMLQuery)
$Ctx.Load($PageLayouts)
$Ctx.ExecuteQuery()
$PageLayoutItem = $PageLayouts[0]
$Ctx.Load($PageLayoutItem)
$Ctx.ExecuteQuery()
Write-host -f Green "Done!"

#Create Publishing page
Write-host -f Yellow "Creating New Page..." -NoNewline
$PageInfo = New-Object Microsoft.SharePoint.Client.Publishing.PublishingPageInformation  
$PageInfo.Name = $PageName
$PageInfo.PageLayoutListItem = $PageLayoutItem 
$Page = $PublishingWeb.AddPublishingPage($PageInfo)  
$Ctx.ExecuteQuery()
Write-host -f Green "Done!"

#Get the List item of the page
Write-host -f Yellow "Updating Page Content..." -NoNewline
$ListItem = $Page.ListItem
$Ctx.Load($ListItem)
$Ctx.ExecuteQuery()

#Update Page Contents
$ListItem["Title"] = $PageTitle
$ListItem["PublishingPageContent"] = $PageContent
$ListItem.Update()
$Ctx.ExecuteQuery()
Write-host -f Green "Done!"

#Publish the page
Write-host -f Yellow "Checking-In and Publishing the Page..." -NoNewline
$ListItem.File.CheckIn([string]::Empty, [Microsoft.SharePoint.Client.CheckinType]::MajorCheckIn)
$ListItem.File.Publish([string]::Empty)
$Ctx.ExecuteQuery()
Write-host -f Green "Done!"

PnP PowerShell 在 SharePoint Online 中创建发布页面:


#Config Variables
$SiteURL = "https://Crescent.sharepoint.com/sites/Marketing"
$PageName = "About-us"
$PageTitle= "About Us"
$PageTemplate = "BlankWebPartPage"
$PageContent="Out Fund brings together healthcare technology companies, foundations, development financing institutions and institutional investors,`
              to address and impact poor healthcare outcomes in Africa and Asia. Its healthcare investments include Care Hospitals in India"
 
Try {
    #Connect to PnP Online
    Connect-PnPOnline -Url $SiteURL -Interactive
 
    #Create a publishing page
    Add-PnPPublishingPage -PageName $PageName -Title $PageTitle -PageTemplateName $PageTemplate
    
    #Get the Page
    $Page = Get-PnPListItem -List "Pages" -Query "<View><Query><Where><Eq><FieldRef Name='Title'/><Value Type='Text'>$PageTitle</Value></Eq></Where></Query></View>"

    #check-out the page for editing
    Set-PnPFileCheckedOut -Url $Page["FileRef"]
 
    #Set Page Content
    Set-PnPListItem -List "Pages" -Identity $Page -Values @{"PublishingPageContent"=$PageContent}
 
    #check-in the page
    Set-PnPFileCheckedIn -Url $Page["FileRef"] -CheckinType MajorCheckIn
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

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

取消回复欢迎 发表评论:

关灯