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

[玩转系统] SharePoint Online:使用 PowerShell 将 Web 部件添加到新式页面

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

SharePoint Online:使用 PowerShell 将 Web 部件添加到新式页面


要求:使用 PowerShell 将 Web 部件添加到 SharePoint Online 中的新式页面。

如何将 Web 部件添加到现代 SharePoint Online 页面?

Web 部件是预构建或定制开发的功能块,可以添加到页面中以显示内容、与用户交互或执行特定任务。 SharePoint Online 允许您通过向页面添加 Web 部件来自定义网站的布局和功能。将 Web 部件添加到 SharePoint Online 中的页面是一个简单的过程。此博文将向您展示如何将 Web 部件添加到 SharePoint Online 中的新式页面。我们还将向您展示如何使用 PowerShell 将 Web 部件添加到 SharePoint Online 中的新式页面。

要将 Web 部件添加到 SharePoint Online 页面,请执行以下步骤:

  1. 在 Web 浏览器中登录到您的 SharePoint Online 网站 >> 创建新页面或编辑现有页面。
  2. 将鼠标悬停在正文区域,然后单击“+”图标将 Web 部件添加到页面。

    [玩转系统] SharePoint Online:使用 PowerShell 将 Web 部件添加到新式页面

  3. 从可用的 Web 部件中,选择所需的 Web 部件以将其添加到新式页面。

    [玩转系统] SharePoint Online:使用 PowerShell 将 Web 部件添加到新式页面

  4. 现在,您可以单击 Web 部件来设置其属性,例如文本 Web 部件的“文本”。
  5. 您还可以单击编辑 Web 部件来进一步自定义 Web 部件的属性,例如文本样式、颜色、链接等。

    [玩转系统] SharePoint Online:使用 PowerShell 将 Web 部件添加到新式页面

  6. 完成后,单击页面右上角的“发布”以使更改生效。

PowerShell 将 Web 部件添加到新式页面

考虑更新现代网站页面并向其中添加 Web 部件吗?要将 Web 部件添加到新式页面,您也可以使用 PowerShell!在这篇博文中,我们将向您展示如何使用 PowerShell 将 Web 部件添加到 SharePoint Online 中的页面。这是一种方便的技术,可以了解您是否想要一次对多个页面进行更改,或者是否希望以编程方式向页面添加内容并希望自动化该过程。


#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/Marketing"
$PageName ="Branding-Templates.aspx"

Try {
    #Connect to the site
    Connect-PnPOnline -Url $SiteURL -Interactive

    #Get the page
    $Page= Get-PnPPage -Identity $PageName

    #add text web part to the modern page
    Add-PnPPageTextPart -Page $Page -Text "Welcome to the Brand Center!"
    $Page.Publish()
    Write-Host "Web Part has been added to the page successfully!" -f Green
}
Catch [System.Exception]
{
    Write-Host -f Red "Error:"$_.Exception.Message
}

您可以通过提供部分、列和顺序值将 Web 部件定位到页面的特定部分。例如。


#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/Marketing"
$PageName ="Branding-Templates.aspx"

#Connect to the site
Connect-PnPOnline -Url $SiteURL -Interactive

#Get the page
$Page= Get-PnPPage -Identity $PageName

$WebPartText = "<h2>Welcome to Branding Portal</h2>What colour palette you use, what type of imagery to pick, `
                    what fonts go well together, how to capture that unique tone of voice. There’s so much to devise, plan out, `
                        and execute, and it can be frustrating and use up time that you just don’t have. So, we thought we’d make it a bit easier for you."

#Add Text to Section 1 column 1 and in 2nd order
Add-PnPPageTextPart -Page $Page -Text $WebPartText -Order 1 -Section 1 -Column 1
$Page.Publish()

这会在页面中已插入的文本 Web 部件(其顺序为 0)下方添加一个文本 Web 部件。

如何添加列表、图像、人物等 Web 部件?

添加其他 Web 部件(例如列表、文档库、图像、人员等)怎么样?出色地。下面是它的工作原理:


#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/Marketing"
$PageName ="Branding-Templates.aspx"
$ListName ="Projects"
 
Try {
    #Connect to the site
    Connect-PnPOnline -Url $SiteURL -Interactive
 
    #Get the list to display
    $List = Get-PnPList -Identity $ListName
 
    #Get an existing page
    $Page= Get-PnPPage -Identity $PageName
 
    #Add a section
    Add-PnPPageSection -Page $Page -SectionTemplate OneColumn -Order 1
     
    #Add a document library to page
    Add-PnPPageWebPart -Page $Page -DefaultWebPartType List -Section 1 -Column 1 -WebPartProperties @{selectedListId=$List.Id}
    $Page.Publish()
}
Catch [System.Exception]
{
    Write-Host -f Red "Error:"$_.Exception.Message
}

您必须指定“DefaultWebPartType”参数,然后相应地指定“WebPartProperties”。同样,要添加人员 Web 部件,请使用以下命令:


#Set Web Part Properties
$WP = '{"layout":1,"persons":[{"id":"[email protected]","role":""}],"title":"Contacts"}'

#Add web part to page
Add-PnPPageWebPart -Page $Page -DefaultWebPartType People -Section 1 -Column 1 -WebPartProperties $WP

您还可以使用相同的过程添加其他类型的 Web 部件。这只是您可以使用 PowerShell 和 SharePoint Online 执行的众多操作之一。这是关于将文档库添加到 SharePoint Online 中的新式页面的另一篇文章:如何使用 PowerShell 将文档库添加到 Sharepoint Online 中的页面?

结论

总之,将 Web 部件添加到现代 SharePoint Online 页面是一个简单的过程,允许您自定义网站的布局和功能。通过遵循本教程中概述的步骤,您可以轻松地将新的 Web 部件添加到页面,并为用户创建更加动态和交互式的体验。通过使用本教程中提供的脚本,您可以使用脚本轻松地将新的 Web 部件添加到页面中,从而节省您在网站上创建内容时的时间和精力。

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

取消回复欢迎 发表评论:

关灯