Use PnP Powershell to add a document library webpart to a page (and only show a specific folder)

Published Jun 10 2021 12:10 AM 1,467 Views

As a non-developer (please read this as a disclaimer) I still try to make my life as easy as possible (yes, I am that lazy). PnP Powershell is a big component of that goal. A customer had the requirement to create a page for each of their 86 folders in a document library so they could add more information on those topics. That meant creating 86 pages, each with a document library webpart on it that showed a specific folder. No chance I was going to do that manually!

Creating the page wasn’t really difficult. Showing the document library and just the items in the folder was the hard part that I couldn’t find any examples of. The idea of this blog post is to help future people like me to just copy/paste the code.

 

 

The goal

We started with a document library containing 86 folders, each having a few documents. The goal was to create 86 pages, with each page showing a block of text on the left and the document library webpart showing only the files from that folder.

How to do this in the user interface

Using the user interface, following steps were required:

  • Create a new page (with the same name as the folder)
  • Add a section to the page with 2 columns
  • Add a text webpart to the left column
  • Add a document library webpart to the right column
  • As a subrequirement, only show the files from the necessary folder. This can be set up from the web part properties
 

MS-list.png

Document library UI properties

That would definitely be a lot of work to do manually, so I decided that PnP PowerShell needed to come to the rescue.

The code

Lets dig in to the code. I imagine that you have already dabbled with PnP Powershell and I will not explain how to install and configure it to run.

Connect-PnPOnline -Url https://yourtenant.sharepoint.com/sites/thesite/ -UseWebLogin

First we need to connect to the site. Replace the url with the correct url of your site. I am using -UseWebLogin in this example because I am using 2factor authentication.

Create the page

First thing to do is to create the page, using the Add-PnPClientSidePage command. I am using the $name variable here to give it a name.

Add-PnPClientSidePage -Name $name
  -LayoutType Article
  -HeaderLayoutType NoImage
  -CommentsEnabled:$false

 

Disabling the comments section on a modern SharePoint Page
I couldn’t figure out how to disable the comments section on the modern client page. I tried setting it to false, or 0, but that didn’t work.

The correct way to do is to use:

-CommentsEnabled:$false

Adding sections to the page

To add a new section to the page, I am using the Add-PnPClientSidePageSection command. I can just add a TwoColumn section on the page.

Add-PnPClientSidePageSection -Page $name -SectionTemplate TwoColumn -Order 1

Adding a text editor webpart

Adding a text editor is super easy, just use the Add-PnPClientSideText command. Don’t forget to add some text or it will fail.

Add-PnPClientSideText -Page $name -Section 1 -Column 1 -Text " "

The hard part: adding an existing document library as a webpart to the page

This was the easy bit, in my opinion. Adding a document library to a page is surprisingly hard in PnP Powershell (unless I am missing something big.. in that case please call me out on this!)

What you need to do, is to use the Add-PnPClientSideWebPart command. With this command you can add all kinds of webparts to the page. Document library isn’t one of them.

You need to add a List webparttype, and in the WebpartProperties you need to mention that it is a document library AND what the ID is.

Add-PnPClientSideWebPart -Page $name
  -DefaultWebPartType List -Section 1 -Column 2
  -WebPartProperties @{isDocumentLibrary="true";
                       selectedListId="1fa1fb45-e53b-4ea1-9325-ddca7afe986e";}

Where can I find the SharePoint document library Id ?

I didn’t have a clue how to get this Id via code, so I resorted to the UI: If you go to the library settings, the document library Id is shown in the url:

 

documentlibrary-id.png

 

SharePoint document library ID in the url of the library settings page

Just cut out the %7B in the front, and the %7D on the back.
In this example, the document library Id is 4683b239-caf6-40a3-96c4-a02dedfa3418.

Bonus: Only show a specific folder from the document library

I couldn’t figure out how to show only documents from a specific folder. Doing this in the UI is supereasy. But there wasn’t any example code out there. So here it is:

In the WebPartProperties, add selectedFolderPath=”/yourfoldername”;

Add-PnPClientSideWebPart -Page $name
  -DefaultWebPartType List -Section 1 -Column 2
  -WebPartProperties @{isDocumentLibrary="true";
                       selectedListId="1fa1fb45-e53b-4ea1-9325-ddca7afe986e";
                       selectedFolderPath="/$name";}

Bonus 2: hide the command bar on the SharePoint Document Library Webpart

In the UI, there is a way to simply hide the command bar. Because we are showing this information in a nice looking page, there is no need for all that extra fluff of “new”, “upload” and so on.

In the same way as showing just files from a specific folder, you can use the hideCommandBar=”false”; in the WebPartProperties:

Add-PnPClientSideWebPart -Page $name
  -DefaultWebPartType List -Section 1 -Column 2
  -WebPartProperties @{isDocumentLibrary="true";
                       selectedListId="1fa1fb45-e53b-4ea1-9325-ddca7afe986e";
                       selectedFolderPath="/$name";
                       hideCommandBar="false"}

Publishing the page

All the parts we need are now on the page. The only thing now is to publish the page so it is visible to all visitors. For that, we need to grab the page again and publish it.

$page = Get-PnPClientSidePage -Identity $name
$page.Publish()

Looping the code for all folders

The last part of the code was to make this repeatable, for all 86 folders. There is probably a really nice way to , in code, get all folders from the doclib and loop through them, but as stated a gazillion times.. I am not a developer.:smile: 

 

So I exported the document library to Excel and copied the foldernames. I added some quotes and a comma (in an Excel formula using =CHAR(34) &  A2 & CHAR(34) &”,”) and added an array to store these.

The full code is:

Connect-PnPOnline -Url https://yourtenant.sharepoint.com/sites/Yoursite/ -UseWebLogin
$ray = "folder1",
"folder2",
"folder3"
foreach ($name in $ray) {

#create page
Add-PnPClientSidePage -Name $name -LayoutType Article -HeaderLayoutType NoImage -CommentsEnabled:$false

#add sections
Add-PnPClientSidePageSection -Page $name -SectionTemplate TwoColumn -Order 1

#add text webpart
Add-PnPClientSideText -Page $name -Section 1 -Column 1 -Text " "

#add doclib
Add-PnPClientSideWebPart -Page $name -DefaultWebPartType List -Section 1 -Column 2 -WebPartProperties @{isDocumentLibrary="true";selectedListId="1fa1fb45-e53b-4ea1-9325-ddca7afe986e";selectedFolderPath="/$name";hideCommandBar="false"}
$page = Get-PnPClientSidePage -Identity $name
$page.Publish()
}
1 Comment
Frequent Contributor

Great job Marijn

I hope a lot of people find this useful and to take it further. Thanks for sharing your learning journey. 

%3CLINGO-SUB%20id%3D%22lingo-sub-2428310%22%20slang%3D%22en-US%22%3EUse%20PnP%20Powershell%20to%20add%20a%20document%20library%20webpart%20to%20a%20page%20(and%20only%20show%20a%20specific%20folder)%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2428310%22%20slang%3D%22en-US%22%3E%3CP%3EAs%20a%20non-developer%20(please%20read%20this%20as%20a%20disclaimer)%20I%20still%20try%20to%20make%20my%20life%20as%20easy%20as%20possible%20(yes%2C%20I%20am%20that%20lazy).%20PnP%20Powershell%20is%20a%20big%20component%20of%20that%20goal.%20A%20customer%20had%20the%20requirement%20to%20create%20a%20page%20for%20each%20of%20their%2086%20folders%20in%20a%20document%20library%20so%20they%20could%20add%20more%20information%20on%20those%20topics.%20That%20meant%20creating%2086%20pages%2C%20each%20with%20a%20document%20library%20webpart%20on%20it%20that%20showed%20a%20specific%20folder.%20No%20chance%20I%20was%20going%20to%20do%20that%20manually!%3C%2FP%3E%0A%3CP%3ECreating%20the%20page%20wasn%E2%80%99t%20really%20difficult.%20Showing%20the%20document%20library%20and%20just%20the%20items%20in%20the%20folder%20was%20the%20hard%20part%20that%20I%20couldn%E2%80%99t%20find%20any%20examples%20of.%20The%20idea%20of%20this%20blog%20post%20is%20to%20help%20future%20people%20like%20me%20to%20just%20copy%2Fpaste%20the%20code.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH1%20id%3D%22toc-hId-1355964393%22%20id%3D%22toc-hId-1356743028%22%20id%3D%22toc-hId-1356743028%22%20id%3D%22toc-hId-1355964393%22%3EThe%20goal%3C%2FH1%3E%0A%3CP%3EWe%20started%20with%20a%20document%20library%20containing%2086%20folders%2C%20each%20having%20a%20few%20documents.%20The%20goal%20was%20to%20create%2086%20pages%2C%20with%20each%20page%20showing%20a%20block%20of%20text%20on%20the%20left%20and%20the%20document%20library%20webpart%20showing%20only%20the%20files%20from%20that%20folder.%3C%2FP%3E%0A%3CH1%20id%3D%22toc-hId--451490070%22%20id%3D%22toc-hId--450711435%22%20id%3D%22toc-hId--450711435%22%20id%3D%22toc-hId--451490070%22%3EHow%20to%20do%20this%20in%20the%20user%20interface%3C%2FH1%3E%0A%3CP%3EUsing%20the%20user%20interface%2C%20following%20steps%20were%20required%3A%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3ECreate%20a%20new%20page%20(with%20the%20same%20name%20as%20the%20folder)%3C%2FLI%3E%0A%3CLI%3EAdd%20a%20section%20to%20the%20page%20with%202%20columns%3C%2FLI%3E%0A%3CLI%3EAdd%20a%20text%20webpart%20to%20the%20left%20column%3C%2FLI%3E%0A%3CLI%3EAdd%20a%20document%20library%20webpart%20to%20the%20right%20column%3C%2FLI%3E%0A%3CLI%3EAs%20a%20subrequirement%2C%20only%20show%20the%20files%20from%20the%20necessary%20folder.%20This%20can%20be%20set%20up%20from%20the%20web%20part%20properties%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CDIV%20class%3D%22wp-block-image%22%3E%0A%3CFIGURE%20class%3D%22aligncenter%20size-large%22%3E%0A%3CDIV%20id%3D%22tinyMceEditorMarijn%20Somers_0%22%20class%3D%22mceNonEditable%20lia-copypaste-placeholder%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22MS-list.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F287231iEA8C8F80460DE259%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22MS-list.png%22%20alt%3D%22MS-list.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CFIGCAPTION%3EDocument%20library%20UI%20properties%3C%2FFIGCAPTION%3E%0A%3C%2FFIGURE%3E%0A%3C%2FDIV%3E%0A%3CP%3EThat%20would%20definitely%20be%20a%20lot%20of%20work%20to%20do%20manually%2C%20so%20I%20decided%20that%20PnP%20PowerShell%20needed%20to%20come%20to%20the%20rescue.%3C%2FP%3E%0A%3CH1%20id%3D%22toc-hId-2036022763%22%20id%3D%22toc-hId-2036801398%22%20id%3D%22toc-hId-2036801398%22%20id%3D%22toc-hId-2036022763%22%3EThe%20code%3C%2FH1%3E%0A%3CP%3ELets%20dig%20in%20to%20the%20code.%20I%20imagine%20that%20you%20have%20already%20dabbled%20with%20PnP%20Powershell%20and%20I%20will%20not%20explain%20how%20to%20install%20and%20configure%20it%20to%20run.%3C%2FP%3E%0A%3CDIV%20class%3D%22wp-block-syntaxhighlighter-code%20%22%3E%0A%3CDIV%20id%3D%22highlighter_645158%22%20class%3D%22syntaxhighlighter%20%20powershell%22%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3EConnect-PnPOnline%20-Url%20https%3A%2F%2Fyourtenant.sharepoint.com%2Fsites%2Fthesite%2F%20-UseWebLogin%0A%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3CP%3EFirst%20we%20need%20to%20connect%20to%20the%20site.%20Replace%20the%20url%20with%20the%20correct%20url%20of%20your%20site.%20I%20am%20using%20-UseWebLogin%20in%20this%20example%20because%20I%20am%20using%202factor%20authentication.%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--1568383059%22%20id%3D%22toc-hId--1567604424%22%20id%3D%22toc-hId--1567604424%22%20id%3D%22toc-hId--1568383059%22%3ECreate%20the%20page%3C%2FH2%3E%0A%3CP%3EFirst%20thing%20to%20do%20is%20to%20create%20the%20page%2C%20using%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fpnp.github.io%2Fpowershell%2Fcmdlets%2FAdd-PnPClientSidePage.html%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3EAdd-PnPClientSidePage%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FA%3Ecommand.%20I%20am%20using%20the%20%24name%20variable%20here%20to%20give%20it%20a%20name.%3C%2FP%3E%0A%3CDIV%20class%3D%22wp-block-syntaxhighlighter-code%20%22%3E%0A%3CDIV%20id%3D%22highlighter_199923%22%20class%3D%22syntaxhighlighter%20%20powershell%22%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3EAdd-PnPClientSidePage%20-Name%20%24name%0A%26nbsp%3B%26nbsp%3B-LayoutType%20Article%0A%26nbsp%3B%26nbsp%3B-HeaderLayoutType%20NoImage%0A%26nbsp%3B%26nbsp%3B-CommentsEnabled%3A%24false%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EDisabling%20the%20comments%20section%20on%20a%20modern%20SharePoint%20Page%3CBR%20%2F%3EI%20couldn%E2%80%99t%20figure%20out%20how%20to%20disable%20the%20comments%20section%20on%20the%20modern%20client%20page.%20I%20tried%20setting%20it%20to%20false%2C%20or%200%2C%20but%20that%20didn%E2%80%99t%20work.%3C%2FP%3E%0A%3CP%3EThe%20correct%20way%20to%20do%20is%20to%20use%3A%3C%2FP%3E%0A%3CP%3E-CommentsEnabled%3A%24false%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-919129774%22%20id%3D%22toc-hId-919908409%22%20id%3D%22toc-hId-919908409%22%20id%3D%22toc-hId-919129774%22%3EAdding%20sections%20to%20the%20page%3C%2FH2%3E%0A%3CP%3ETo%20add%20a%20new%20section%20to%20the%20page%2C%20I%20am%20using%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fpnp.github.io%2Fpowershell%2Fcmdlets%2FAdd-PnPClientSidePageSection.html%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3EAdd-PnPClientSidePageSection%3C%2FA%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Ecommand.%20I%20can%20just%20add%20a%20TwoColumn%20section%20on%20the%20page.%3C%2FP%3E%0A%3CDIV%20class%3D%22wp-block-syntaxhighlighter-code%20%22%3E%0A%3CDIV%20id%3D%22highlighter_139207%22%20class%3D%22syntaxhighlighter%20%20powershell%22%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3EAdd-PnPClientSidePageSection%20-Page%20%24name%20-SectionTemplate%20TwoColumn%20-Order%201%0A%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3CH2%20id%3D%22toc-hId--888324689%22%20id%3D%22toc-hId--887546054%22%20id%3D%22toc-hId--887546054%22%20id%3D%22toc-hId--888324689%22%3EAdding%20a%20text%20editor%20webpart%3C%2FH2%3E%0A%3CP%3EAdding%20a%20text%20editor%20is%20super%20easy%2C%20just%20use%20the%20Add-PnPClientSideText%20command.%20Don%E2%80%99t%20forget%20to%20add%20some%20text%20or%20it%20will%20fail.%3C%2FP%3E%0A%3CDIV%20class%3D%22wp-block-syntaxhighlighter-code%20%22%3E%0A%3CDIV%20id%3D%22highlighter_664808%22%20class%3D%22syntaxhighlighter%20%20powershell%22%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3EAdd-PnPClientSideText%20-Page%20%24name%20-Section%201%20-Column%201%20-Text%20%22%20%22%0A%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3CH2%20id%3D%22toc-hId-1599188144%22%20id%3D%22toc-hId-1599966779%22%20id%3D%22toc-hId-1599966779%22%20id%3D%22toc-hId-1599188144%22%3EThe%20hard%20part%3A%20adding%20an%20existing%20document%20library%20as%20a%20webpart%20to%20the%20page%3C%2FH2%3E%0A%3CP%3EThis%20was%20the%20easy%20bit%2C%20in%20my%20opinion.%20Adding%20a%20document%20library%20to%20a%20page%20is%20surprisingly%20hard%20in%20PnP%20Powershell%20(unless%20I%20am%20missing%20something%20big..%20in%20that%20case%20please%20call%20me%20out%20on%20this!)%3C%2FP%3E%0A%3CP%3EWhat%20you%20need%20to%20do%2C%20is%20to%20use%20the%3CA%20href%3D%22https%3A%2F%2Fpnp.github.io%2Fpowershell%2Fcmdlets%2FAdd-PnPClientSideWebPart.html%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3EAdd-PnPClientSideWebPart%20command%3C%2FA%3E.%20With%20this%20command%20you%20can%20add%20all%20kinds%20of%20webparts%20to%20the%20page.%20Document%20library%20isn%E2%80%99t%20one%20of%20them.%3C%2FP%3E%0A%3CP%3EYou%20need%20to%20add%20a%20List%20webparttype%2C%20and%20in%20the%20WebpartProperties%20you%20need%20to%20mention%20that%20it%20is%20a%20document%20library%20AND%20what%20the%20ID%20is.%3C%2FP%3E%0A%3CDIV%20class%3D%22wp-block-syntaxhighlighter-code%20%22%3E%0A%3CDIV%20id%3D%22highlighter_600997%22%20class%3D%22syntaxhighlighter%20%20powershell%22%3E%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3EAdd-PnPClientSideWebPart%20-Page%20%24name%0A%26nbsp%3B%26nbsp%3B-DefaultWebPartType%20List%20-Section%201%20-Column%202%0A%26nbsp%3B%26nbsp%3B-WebPartProperties%20%40%7BisDocumentLibrary%3D%22true%22%3B%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3BselectedListId%3D%221fa1fb45-e53b-4ea1-9325-ddca7afe986e%22%3B%7D%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3CH3%20id%3D%22toc-hId--2005217678%22%20id%3D%22toc-hId--2004439043%22%20id%3D%22toc-hId--2004439043%22%20id%3D%22toc-hId--2005217678%22%3EWhere%20can%20I%20find%20the%20SharePoint%20document%20library%20Id%20%3F%3C%2FH3%3E%0A%3CP%3EI%20didn%E2%80%99t%20have%20a%20clue%20how%20to%20get%20this%20Id%20via%20code%2C%20so%20I%20resorted%20to%20the%20UI%3A%20If%20you%20go%20to%20the%20library%20settings%2C%20the%20document%20library%20Id%20is%20shown%20in%20the%20url%3A%3C%2FP%3E%0A%3CDIV%20class%3D%22wp-block-image%22%3E%0A%3CFIGURE%20class%3D%22aligncenter%20size-large%22%3E%0A%3CDIV%20id%3D%22tinyMceEditorMarijn%20Somers_1%22%20class%3D%22mceNonEditable%20lia-copypaste-placeholder%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20image-alt%3D%22documentlibrary-id.png%22%20style%3D%22width%3A%20581px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F287222iE3871EB251E50F68%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22documentlibrary-id.png%22%20alt%3D%22documentlibrary-id.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CFIGCAPTION%3ESharePoint%20document%20library%20ID%20in%20the%20url%20of%20the%20library%20settings%20page%3C%2FFIGCAPTION%3E%0A%3C%2FFIGURE%3E%0A%3C%2FDIV%3E%0A%3CP%3EJust%20cut%20out%20the%20%257B%20in%20the%20front%2C%20and%20the%20%257D%20on%20the%20back.%3CBR%20%2F%3EIn%20this%20example%2C%20the%20document%20library%20Id%20is%204683b239-caf6-40a3-96c4-a02dedfa3418.%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--2015720782%22%20id%3D%22toc-hId--2014942147%22%20id%3D%22toc-hId--2014942147%22%20id%3D%22toc-hId--2015720782%22%3EBonus%3A%20Only%20show%20a%20specific%20folder%20from%20the%20document%20library%3C%2FH2%3E%0A%3CP%3EI%20couldn%E2%80%99t%20figure%20out%20how%20to%20show%20only%20documents%20from%20a%20specific%20folder.%20Doing%20this%20in%20the%20UI%20is%20supereasy.%20But%20there%20wasn%E2%80%99t%20any%20example%20code%20out%20there.%20So%20here%20it%20is%3A%3C%2FP%3E%0A%3CP%3EIn%20the%20WebPartProperties%2C%20add%20selectedFolderPath%3D%E2%80%9D%2Fyourfoldername%E2%80%9D%3B%3C%2FP%3E%0A%3CDIV%20class%3D%22wp-block-syntaxhighlighter-code%20%22%3E%0A%3CDIV%20id%3D%22highlighter_720175%22%20class%3D%22syntaxhighlighter%20%20powershell%22%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3EAdd-PnPClientSideWebPart%20-Page%20%24name%0A%26nbsp%3B%26nbsp%3B-DefaultWebPartType%20List%20-Section%201%20-Column%202%0A%26nbsp%3B%26nbsp%3B-WebPartProperties%20%40%7BisDocumentLibrary%3D%22true%22%3B%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3BselectedListId%3D%221fa1fb45-e53b-4ea1-9325-ddca7afe986e%22%3B%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3BselectedFolderPath%3D%22%2F%24name%22%3B%7D%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3CH2%20id%3D%22toc-hId-471792051%22%20id%3D%22toc-hId-472570686%22%20id%3D%22toc-hId-472570686%22%20id%3D%22toc-hId-471792051%22%3EBonus%202%3A%20hide%20the%20command%20bar%20on%20the%20SharePoint%20Document%20Library%20Webpart%3C%2FH2%3E%0A%3CP%3EIn%20the%20UI%2C%20there%20is%20a%20way%20to%20simply%20hide%20the%20command%20bar.%20Because%20we%20are%20showing%20this%20information%20in%20a%20nice%20looking%20page%2C%20there%20is%20no%20need%20for%20all%20that%20extra%20fluff%20of%20%E2%80%9Cnew%E2%80%9D%2C%20%E2%80%9Cupload%E2%80%9D%20and%20so%20on.%3C%2FP%3E%0A%3CP%3EIn%20the%20same%20way%20as%20showing%20just%20files%20from%20a%20specific%20folder%2C%20you%20can%20use%20the%20hideCommandBar%3D%E2%80%9Dfalse%E2%80%9D%3B%20in%20the%20WebPartProperties%3A%3C%2FP%3E%0A%3CDIV%20class%3D%22wp-block-syntaxhighlighter-code%20%22%3E%0A%3CDIV%20id%3D%22highlighter_988677%22%20class%3D%22syntaxhighlighter%20%20powershell%22%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3EAdd-PnPClientSideWebPart%20-Page%20%24name%0A%26nbsp%3B%26nbsp%3B-DefaultWebPartType%20List%20-Section%201%20-Column%202%0A%26nbsp%3B%26nbsp%3B-WebPartProperties%20%40%7BisDocumentLibrary%3D%22true%22%3B%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3BselectedListId%3D%221fa1fb45-e53b-4ea1-9325-ddca7afe986e%22%3B%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3BselectedFolderPath%3D%22%2F%24name%22%3B%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3BhideCommandBar%3D%22false%22%7D%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3CH2%20id%3D%22toc-hId-338107327%22%20id%3D%22toc-hId-338885962%22%20id%3D%22toc-hId-338885962%22%20id%3D%22toc-hId-338107327%22%3EPublishing%20the%20page%3C%2FH2%3E%0A%3CP%3EAll%20the%20parts%20we%20need%20are%20now%20on%20the%20page.%20The%20only%20thing%20now%20is%20to%20publish%20the%20page%20so%20it%20is%20visible%20to%20all%20visitors.%20For%20that%2C%20we%20need%20to%20grab%20the%20page%20again%20and%20publish%20it.%3C%2FP%3E%0A%3CDIV%20class%3D%22wp-block-syntaxhighlighter-code%20%22%3E%0A%3CDIV%20id%3D%22highlighter_460127%22%20class%3D%22syntaxhighlighter%20%20powershell%22%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%24page%20%3D%20Get-PnPClientSidePage%20-Identity%20%24name%0A%24page.Publish()%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3CH1%20id%3D%22toc-hId--1598429855%22%20id%3D%22toc-hId--1597651220%22%20id%3D%22toc-hId--1597651220%22%20id%3D%22toc-hId--1598429855%22%3ELooping%20the%20code%20for%20all%20folders%3C%2FH1%3E%0A%3CP%3EThe%20last%20part%20of%20the%20code%20was%20to%20make%20this%20repeatable%2C%20for%20all%2086%20folders.%20There%20is%20probably%20a%20really%20nice%20way%20to%20%2C%20in%20code%2C%20get%20all%20folders%20from%20the%20doclib%20and%20loop%20through%20them%2C%20but%20as%20stated%20a%20gazillion%20times..%20I%20am%20not%20a%20developer.%3CIMG%20class%3D%22lia-deferred-image%20lia-image-emoji%22%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Fhtml%2F%408341BD79091AF36AA2A09063B554B5CD%2Fimages%2Femoticons%2Fsmile_40x40.gif%22%20alt%3D%22%3Asmile%3A%22%20title%3D%22%3Asmile%3A%22%20%2F%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ESo%20I%20exported%20the%20document%20library%20to%20Excel%20and%20copied%20the%20foldernames.%20I%20added%20some%20quotes%20and%20a%20comma%20(in%20an%20Excel%20formula%20using%20%3DCHAR(34)%20%26amp%3B%26nbsp%3B%20A2%20%26amp%3B%20CHAR(34)%20%26amp%3B%E2%80%9D%2C%E2%80%9D)%20and%20added%20an%20array%20to%20store%20these.%3C%2FP%3E%0A%3CP%3EThe%20full%20code%20is%3A%3C%2FP%3E%0A%3CDIV%20class%3D%22wp-block-syntaxhighlighter-code%20%22%3E%0A%3CDIV%20id%3D%22highlighter_239645%22%20class%3D%22syntaxhighlighter%20%20powershell%22%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3EConnect-PnPOnline%20-Url%20https%3A%2F%2Fyourtenant.sharepoint.com%2Fsites%2FYoursite%2F%20-UseWebLogin%0A%24ray%20%3D%20%22folder1%22%2C%0A%22folder2%22%2C%0A%22folder3%22%0Aforeach%20(%24name%20in%20%24ray)%20%7B%0A%0A%23create%20page%0AAdd-PnPClientSidePage%20-Name%20%24name%20-LayoutType%20Article%20-HeaderLayoutType%20NoImage%20-CommentsEnabled%3A%24false%0A%0A%23add%20sections%0AAdd-PnPClientSidePageSection%20-Page%20%24name%20-SectionTemplate%20TwoColumn%20-Order%201%0A%0A%23add%20text%20webpart%0AAdd-PnPClientSideText%20-Page%20%24name%20-Section%201%20-Column%201%20-Text%20%22%20%22%0A%0A%23add%20doclib%0AAdd-PnPClientSideWebPart%20-Page%20%24name%20-DefaultWebPartType%20List%20-Section%201%20-Column%202%20-WebPartProperties%20%40%7BisDocumentLibrary%3D%22true%22%3BselectedListId%3D%221fa1fb45-e53b-4ea1-9325-ddca7afe986e%22%3BselectedFolderPath%3D%22%2F%24name%22%3BhideCommandBar%3D%22false%22%7D%0A%24page%20%3D%20Get-PnPClientSidePage%20-Identity%20%24name%0A%24page.Publish()%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FDIV%3E%0A%3C%2FDIV%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-2428310%22%20slang%3D%22en-US%22%3E%3CP%3EAs%20a%20non-developer%20(please%20read%20this%20as%20a%20disclaimer)%20I%20still%20try%20to%20make%20my%20life%20as%20easy%20as%20possible%20(yes%2C%20I%20am%20that%20lazy).%20PnP%20Powershell%20is%20a%20big%20component%20of%20that%20goal.%20A%20customer%20had%20the%20requirement%20to%20create%20an%20accompanying%20page%20for%20each%20of%20their%2086%20folders%20in%20a%20document%20library%20so%20they%20could%20add%20more%20information%20on%20those%20topics.%20That%20meant%20creating%2086%20pages%2C%20each%20with%20a%20document%20library%20webpart%20on%20it%20that%20showed%20a%20specific%20folder.%20No%20chance%20I%20was%20going%20to%20do%20that%20manually!%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2436814%22%20slang%3D%22en-US%22%3ERe%3A%20Use%20PnP%20Powershell%20to%20add%20a%20document%20library%20webpart%20to%20a%20page%20(and%20only%20show%20a%20specific%20folder)%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2436814%22%20slang%3D%22en-US%22%3E%3CP%3EGreat%20job%20Marijn%3C%2FP%3E%3CP%3EI%20hope%20a%20lot%20of%20people%20find%20this%20useful%20and%20to%20take%20it%20further.%20Thanks%20for%20sharing%20your%20learning%20journey.%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
Co-Authors
Version history
Last update:
‎Jun 08 2021 01:21 PM
Updated by:
www.000webhost.com