SharePoint Online: How to Delete a List Template using PowerShell?

Requirement: Delete list template in SharePoint Online

How to delete a list template SharePoint Online?
To remove a list template in SharePoint Online, do the following:
  • Login to your SharePoint Online site collection (List templates are scoped at site collection level) >> Click on Settings >> Site Settings
  • On Site Settings Page, Click on "List Templates" under Web Designer Galleries (https://Tenant.sharepoint.com/sites/SiteURL/_catalogs/lt/Forms/AllItems.aspx)
  • Select the list template to delete and click on "Delete Document" from the ribbon
  • Confirm the prompt to delete custom list template in SharePoint Online.
    sharepoint online delete list template

SharePoint Online: PowerShell to Delete List Template
#Load SharePoint Online Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
    
#Variables for Processing
$SiteUrl = "https://crescent.sharepoint.com/sites/Projects"
$ListTemplateInternalName= "Project Tasks.stp"

#Get Credentials to connect
$Cred= Get-Credential
  
Try {    
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
     
    #Get the Web
    $Web = $Ctx.Web
    $Ctx.Load($Web)
    $Ctx.ExecuteQuery()
    
    #Get the List Template
    $ListTemplateFile = $Web.GetFileByURL("_catalogs/lt/" + $ListTemplateInternalName)
    $Ctx.Load($ListTemplateFile)
    $Ctx.ExecuteQuery()

    If($ListTemplateFile -ne $Null)
    {
        $ListTemplateFile.Recycle()
        $Ctx.ExecuteQuery()
        Write-host -f Green "List Template Deleted Successfully!"
    }
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

PnP PowerShell to Delete a List Template in SharePoint Online:
#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/IC"
$ListTemplateInternalName= "IC Documents.stp"

#Connect to the Site
Connect-PnPOnline -URL $SiteURL -UseWebLogin

#Get the List Template
$TemplateFile = Get-PnPFolderItem -FolderSiteRelativeUrl "_catalogs/lt" -ItemName $ListTemplateInternalName

If ($TemplateFile -ne $null)
{   
    #Delete the List Template
    Remove-PnPFile -SiteRelativeUrl ("/_catalogs/lt/"+$ListTemplateInternalName) -Recycle -Force
    Write-host -f Green "List Template Deleted Successfully!"
}
Else
{
    Write-host -f Yellow "Template Not Found!"
}
SharePoint Online: How to Delete a List Template using PowerShell? SharePoint Online: How to Delete a List Template using PowerShell? Reviewed by Salaudeen Rajack on March 26, 2019 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.