December 4, 2016

// // Leave a Comment

Upload List Template and Create List based on List Template using PowerShell

Hi All,

1) In this post we will be seeing how to upload a List Template to sharepoint list template folder and use the template to create a list based on the template uploaded . We will be using powershell to achieve this . 


2) Refer the below posts in the powershell series.


 Bulk Creation of Site Columns:

Bulk Creation of Site Content Types:   

Create multiple columns inside SharePoint List using Powershell



3) Use the below code to achieve the functionality




#Date: Aprl 10 2015

param(
[string]$webUrl = $(Read-Host -prompt "Root Web Application Url...?"),
[string]$TemplateFilePath = $(Read-Host -prompt "Template Location...?")
)
  

  
  try
{
    # Get the SiteURL
   
    $oweb = Get-SPWeb($webUrl)
    $site = $oweb.Site
    # Get the list template gallery
    $spLTG = $oweb.getfolder("List Template Gallery")
    # Get the list template gallery Collection
    $spcollection = $spLTG.files
    # Get the custom list template file
    $Templatefile = get-item $TemplateFilePath
    # Add the custom list template file to gallery
    $spcollection.Add("_catalogs/lt/YourTemplateName.stp", $Templatefile.OpenRead(), $true)
    log "Custom Template Uploaded to List Template Gallery Successfully"
    log Creating the List based on the Template
    $spListCollection = $oweb.Lists
    $spLibrary = $spListCollection.TryGetList("YourList")
    if($spLibrary -ne $null){
        log "YourList already exists in the site"
      } 
      else {       
        $listTemplates = $site.GetCustomListTemplates($oweb)
        $oweb.Lists.Add("YourList", "", $listTemplates["YourTemplateName"])
        log "Added YourList...."
      }         
    # Get the custom list templates 
    log "Based on the template List Created"
}

catch 
{
    log "Other exception  $_.Exception.Message"
}

finally
{
   $site.Dispose()
   $oweb.Dispose()
}

  
  

0 comments:

Post a Comment