Automate site setup and library creation with PnP PowerShell


Setting up sites and libraries in SharePoint can be a tedious and repetitive process. Wouldn’t it be nice to be able to automate that process, so you don’t have to click the settings cog wheel or library settings over and over? 

As you may know, PowerShell is a way to communicate and interact with SharePoint. PnP PowerShell, which stands for Patterns and Practices, was released a few years ago to make using PowerShell with SharePoint even easier.  

PnP PowerShell is a library of pre-built commands that allows you to remotely interact with SharePoint to create, read, update, and delete data. Even better, it works for both SharePoint Online and on-premise. 

SharePoint PnP PowerShell revolves around a few simple prefix commands of: 

  1. Add: To create something new, such a library, list, or view 

  2. Get: To retrieve a library, list, or view in order to do something to it, such as:

    • Remove: To delete what you retrieved 

    • Set: To update the properties of what you retrieved 

For example, to add a new library to a SharePoint site, the code looks like:

$LibraryTitle = “Contract Management"
$LibraryURL = “ContractManagement”
$newLibrary =  New-PnPList -Title $LibraryTitle -Template   DocumentLibrary -Url $LibraryURL -EnableContentTypes -OnQuickLaunch 

During department site roll-outs for our clients, we build tens of sites with potentially hundreds of libraries within a short period of time.  

Each library needs to have custom content types and views with associated custom columns in the correct sequence added. It also needs to have the default “Document” content type from the “+New” menu removed. This is a very manual and repetitive process, and since the process is the same for each library, we thought, why don’t we create a script to do that for us automatically? 

Using SharePoint PnP PowerShell, our automation script allows you to:  

  1. Set the logo and theme of the site 

  2. Rename the generic Teams connected Documents library to “Teams Library” 

  3. Remove nodes from Quick Launch, such as “Site Contents” and “Pages” 

  4. Based on each line item in a user inputted CSV file: 

    • Create a library and display it in the Quick Launch 

    • Add specified content type(s) to the library and set the default content type 

    • Create a new default view and add columns in the order specified 

    • Remove the default “Document” content type from the Visible on New Button list

For each new department, the client’s IT administrator or the project team needs to:   

  1. Manually provision the desired site collection 

  2. Publish out required content types for the site (there’s a script for that too!) 

  3. Fill in the CSV files with the required library information 

  4. Have the logo file handy 

  5. Run the script 

  6. Sit back and relax! 

With the help of this automation script, we were able to streamline our provisioning process and save hours of setup time per department. With the library settings coded in the CSV file, it also acts as documentation and gives the user a clear idea of which content type(s) are associated to which library. As a final benefit, there is less chance for error or missing something when sequencing the columns for the views.


Do you want to use a similar script to automate your own SharePoint site rollouts? Contact us and we’d be happy to share the set of scripts with you and give you a demo on how to use it.  

Previous
Previous

The problems of "lift and shift" into SharePoint

Next
Next

Do we still need a SharePoint Intranet?