Thursday 2 April 2020

Creating SharePoint Document Library folders with a specific Content Type and metadata

A script that demonstrates how to create lots of folders in a document library with a specific content type and metadata. This approach uses pnp.

#Folder script to provision yearly folders with specific content type and metadata.
#Note this will also update existing folders

#Connect to tenant
Connect-PnPOnline -url https://mytenant.sharepoint.com/sites/SiteCol -UseWebLogin
$years = @("2020","2019","2018","2017","2016","2015","2014","2013","2012","2011","2010") 

function Provision-YearlyFolders {
        Param($folderName)
        #Get folder
        $query = "<View><Query><Where><Eq><FieldRef Name='Title'/><Value Type='Text'>"+ $folderName +"</Value></Eq></Where></Query></View>"

        $folder = Get-PnPListItem -List incident -Query $query

         #Create folder if it doesnt exist
         if(!$folder) {
                 Add-PnPFolder -Name
                 $folderName -Folder incident
                 $folder = Get-PnPListItem -List incident -Query $query
         }
         #Update content type and set year order column
         Set-PnPListItem -List incident -Identity
         $folder.Id -ContentType "Yearly Folder" -Values @{"YearSortOrder" = $folderName}
         Write-Host "Provisioned" $folderName
}
#loop through yearly folders
foreach($year in $years)
{
    Provision-YearlyFolders $year
}

No comments:

Post a Comment