GitHub Icon Image
GitHub

Create a library from Out-of-the-Box (OOB) library template

Summary

This post demonstrates how to create a SharePoint library using an out-of-the-box (OOB) list template via the Site Script ApplyListDesign REST API and how to call it from PnP PowerShell.

While Invoke-PnPListDesign is useful for deploying custom list templates (see Custom document library template using PnP PowerShell), creating a library from an OOB list design requires calling the ApplyListDesign() REST endpoint.


The Solution: REST API Hack

By using SharePoint's REST API hack through PnP PowerShell as a hack, we can programmatically apply these site designs to existing sites. Here's how to do it:

  • PnP PowerShell
$SiteUrl     = "https://yourtenant.sharepoint.com/sites/YourSite"
$ClientId    = "<your-client-id>"   # Entra app client id or omit for interactive login
$Method      = "POST"
$Endpoint    = "_api/Microsoft.Sharepoint.Utilities.WebTemplateExtensions.SiteScriptUtility.ApplyListDesign()"

# The list design ID for the Media Library template (example)
$listDesignId = "7fdc8cba-3e07-4851-a7ac-b747040ff1ce"

# The runtime parameter name used by the template to set the list name
$listName_FieldValue = "MediaLibrary_listName"
$listName = "Media Lib"

# Build the body object. runtimeParameters must be a JSON string.
$BodyObject = @{
    listDesignId      = $listDesignId
    store             = 1
    runtimeParameters = "{`"$listName_FieldValue`":`"$listName`"}"
}

# Connect using PnP
Connect-PnPOnline -Url $SiteUrl -ClientId $ClientId -Interactive

$Uri = "$SiteUrl/$Endpoint"

# Call the REST endpoint using PnP helper
$Response = Invoke-PnPSPRestMethod -Method $Method -Url $Uri -Content $BodyObject

# Output the response for debugging
$Response | ConvertTo-Json -Depth 10

Check out the PnP PowerShell to learn more at: https://aka.ms/pnp/powershell

The way you login into PnP PowerShell has changed please read PnP Management Shell EntraID app is deleted : what should I do ?

Source Credit

Sample first appeared on Create a library from Out-of-the-Box (OOB) library template with PnP PowerShell

Contributors

Author(s)
Reshmee Auckloo

Disclaimer

THESE SAMPLES ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.

Back to top Script Samples
Generated by DocFX with Material UI