Thursday, 25 November 2021

PowerShell Save List as template and Download List Template to local folder

$AppId =""
$AppSecret = "" 


Function SaveListasTemplate {
    param
    (
        [string]$SiteURL  = $(throw "Enter the Site URL!"),
        [string]$ListName = $(throw "Enter the List Name!"),
        [string]$FileName = $(throw "Enter the List Template Name!"),
        [string]$TemplateName = $(throw "Enter the List Template Name!"),
        [string]$Description = $(throw "Enter the List Description Info!"),
        [string]$IncludeData= $(throw "List content include or not!")
    )
    Try{
        #Get Credentials to connect
        #$Cred= Get-Credential
       
        #Setup the context
        #$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        #$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
        
        #Setup Authentication Manager
        $AuthenticationManager = new-object OfficeDevPnP.Core.AuthenticationManager
        $Ctx = $AuthenticationManager.GetAppOnlyAuthenticatedContext($SiteUrl,$AppId,$AppSecret)
        $Ctx.Load($Ctx.Web)
        $Ctx.ExecuteQuery()
 
        Write-Host $Ctx.Web.Title

        #Get the List
        $List = $Ctx.Web.lists.GetByTitle($ListName)
        $List.SaveAsTemplate($FileName, $TemplateName, $Description, $IncludeData)
        $Ctx.ExecuteQuery()
     
        Write-Host -f Green "List Saved as Template!"
    }
    Catch {
            write-host -f Red "Error Saving List as template!" $_.Exception.Message
    }
}

Function pnp_SaveListasTemplate {
    param
    (
        [string]$SiteURL  = $(throw "Enter the Site URL!"),
        [string]$ListName = $(throw "Enter the List Name!"),
        [string]$FileName = $(throw "Enter the List Template Name!"),
        [string]$TemplateName = $(throw "Enter the List Template Name!"),
        [string]$Description = $(throw "Enter the List Description Info!"),
        [string]$IncludeData= $(throw "List content include or not!")
    )
    Try{
        #Connect to PnP Online
        Connect-PnPOnline -Url $SiteURL -ClientId $AppId -ClientSecret $AppSecret 
        $Context  = Get-PnPContext
 
        #Get the List
        $List = Get-PnpList -Identity $ListName
 
        #Save List as template
        $List.SaveAsTemplate($TemplateFileName, $TemplateName, $TemplateDescription, $IncludeData)
        $Context.ExecuteQuery()

        Write-Host -f Green "List Saved as Template!"
    }
    Catch {
            write-host -f Red "Error Saving List as template!" $_.Exception.Message
    }
}

#Function to download a list template from SharePoint Online using powershell

Function Download_SPOListTemplate
{
    param
    (
        [string]$SiteURL  = $(throw "Enter the Site URL!"),
        [string]$ListTemplateName = $(throw "Enter the List Template Name!"),
        [string]$ExportFile = $(throw "Enter the File Name to Export List Template!")
    )
    Try {
        #Get Credentials to connect
        #Setup Authentication Manager
        $AuthenticationManager = new-object OfficeDevPnP.Core.AuthenticationManager
        $Ctx = $AuthenticationManager.GetAppOnlyAuthenticatedContext($SiteUrl,$AppId,$AppSecret)
         
        #Get the "List Templates" Library
        $List= $Ctx.web.Lists.GetByTitle("List Template Gallery")
        $ListTemplates = $List.GetItems([Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery())
        $Ctx.Load($ListTemplates)
        $Ctx.ExecuteQuery()
 
        #Filter and get given List Template
        $ListTemplate = $ListTemplates | Where-Object { $_["TemplateTitle"] -eq $ListTemplateName }
 
        If($Null -ne $ListTemplate)
        {
            #Get the File from the List item
            $Ctx.Load($ListTemplate.File)
            $Ctx.ExecuteQuery()
 
            #Download the list template
            $FileInfo = [Microsoft.SharePoint.Client.File]::OpenBinaryDirect($Ctx,$ListTemplate.File.ServerRelativeUrl)
            $WriteStream = [System.IO.File]::Open($ExportFile,[System.IO.FileMode]::Create)
            $FileInfo.Stream.CopyTo($WriteStream)
            $WriteStream.Close()
 
            write-host -f Green "List Template Downloaded to $ExportFile!" $_.Exception.Message
        }
        else
        {
            Write-host -f Yellow "List Template Not Found:"$ListTemplateName
        }
    }
    Catch {
        write-host -f Red "Error Downloading List Template!" $_.Exception.Message
    }
}


$SiteURL = ""
$ListName="PSTestLibrary" 
#Configure Save list as template parameters
$FileName="PSTestLibrary Template"
$TemplateName="PSTestLibrary Template"
$Description ="List Template for PSTestLibrary"
$IncludeData = $False
$ExportFile = "C:\Temp\" + $ListName + ".stp"
SaveListasTemplate -SiteURL $SiteURL -ListName $listName -FileName $fileName -TemplateName $templateName -Description $description -IncludeData $include

#Call the function to Download the list template
Download_SPOListTemplate -SiteURL $SiteURL -ListTemplateName $TemplateName -ExportFile $ExportFile

No comments:

Post a Comment