网站搜索

使用 PowerShell 将列表或文档库添加到 SharePoint Online 中的页面


要求:将列表或文档库添加到 SharePoint Online 中的新式页面。

SharePoint Online:如何将文档库添加到页面?

在现代 SharePoint Online 页面中,我们可以添加列表或文档库以在页面上显示其内容。在这篇博文中,我们将介绍如何将列表添加到 SharePoint Online 页面的步骤。我们还将了解如何使用 PowerShell 将文档库添加到 SharePoint Online 中的页面。

要将文档库添加到 SharePoint Online 中的页面,请执行以下步骤:

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

  3. 从可用的 Web 部件中选择文档库或列表。例如,任务列表、文档等。

  4. 现在,您可以选择要在页面上显示的文档库。
  5. 您还可以单击“编辑 Web 部件”来进一步自定义文档库的属性,例如要显示的文件夹、列表视图、大小等。完成后,单击“应用”。
  6. 单击页面右上角的“发布”以使更改生效。

要将列表添加到 SharePoint Online 中的页面,您也可以使用 PowerShell。

PnP PowerShell 将文档库或列表添加到页面

我们可以使用 PnP PowerShell 自动执行上述步骤,以将列表添加到 SharePoint Online 中的页面。如果您想快速将库添加到页面并且不想通过 Web 浏览器界面导航,这会很有帮助。


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

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

    #Get the document library
    $Library = Get-PnPList -Identity $LibraryName

    #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 @{isDocumentLibrary="true";selectedListId=$Library.Id}
    $Page.Publish()
    Write-Host "Document Library has been added to the page successfully!"
}
Catch [System.Exception]
{
    Write-Host -f Red "Error:"$_.Exception.Message
}

文档库现在将显示在页面上,允许用户查看其中包含的文档并与之交互。您可以在“WebPartProperties”中使用“selectedFolderPath=”/your-folder-path”来显示文档库中的特定文件夹。当您想要向页面添加列表时,将“isDocumentLibrary”设置为“false”。

使用 PowerShell 在 SharePoint Online 中创建新的新式页面位于我的另一篇文章中:如何使用 PowerShell 在 SharePoint Online 中创建页面?

相关文章