Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if ($ver.Version.Major -gt 1) {$host.Runspace.ThreadOptions = "ReuseThread"}
- if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null)
- {
- Add-PSSnapin "Microsoft.SharePoint.PowerShell"
- }
- Function UploadListTemplate($WebURL, $TemplateFilePath)
- {
- #Get the Web
- $web = Get-SPWeb $WebURL
- #Get the List template Gallery Folder
- $TemplateFolder = $web.GetFolder("List Template Gallery")
- #Get the Files collection
- $TemplateFileCollection = $TemplateFolder.Files
- #Get the Template file from Local File system
- $TemplateFile = Get-ChildItem $TemplateFilePath
- #Open the File in Read mode and Add to Templates collection
- $TemplateFileCollection.Add("_catalogs/lt/$($TemplateFile.Name)"
- $TemplateFile.OpenRead(), $true)
- Write-Host "Done!Template has been uploaded!!"
- }
- #Call the function
- UploadListTemplate "http://sharepoint.crescent.com" "D:TemplatesCustomTaskList.stp"
- function UploadAndCreate-CustomListTemplate()
- {
- param(
- [Parameter(Mandatory=$true)][string]$rooturl,
- [Parameter(Mandatory=$true)][string]$listCreationUrl,
- [Parameter(Mandatory=$false)][System.Net.NetworkCredential]$credentials,
- [Parameter(Mandatory=$true)][string]$listName,
- [Parameter(Mandatory=$true)][string]$listTemplateFileUrl,
- [Parameter(Mandatory=$true)][string]$listTemplateName
- )
- try
- {
- # Adding the PowerShell Snapin
- Add-PSSnapin “Microsoft.SharePoint.PowerShell”
- # Get the SiteURL
- $site = get-spsite($rooturl)
- # Get the root web
- $web = $site.RootWeb
- # Get the list template gallery
- $spLTG = $web.getfolder(“List Template Gallery”)
- # Get the list template gallery Collection
- $spcollection = $spLTG.files
- # Get the custom list template file
- $Templatefile = get-item $listTemplateFileUrl
- # Add the custom list template file to gallery
- $titleHashTbl = @{}
- $titleHashTbl.Add("Title", $listTemplateName)
- $folderListTemplate = "_catalogs/lt/" + $listTemplateName + ".stp"
- $spcollection.Add($folderListTemplate, $Templatefile.OpenRead(),$titleHashTbl,$true)
- Write-Host “Custom Template Uploaded to List Template Gallery Successfully”
- Write-Host “Creating the List based on the Template”
- # Get the custom list templates
- $CustomlistTemplates = $site.GetCustomListTemplates($web)
- #Create the custom list using template
- $site = Get-SPWeb $listCreationUrl
- $site.Lists.Add($listName, $listName, $CustomlistTemplates[$listTemplateName])
- Write-Host "Based on the template List Created"
- }
- catch
- {
- Write-Host ("Error while creating list. Error -->> " + $_.Exception.Message) -ForegroundColor Red
- }
- }
- $rootUrl = "http://somesite.com"
- $url = "http://somesite.com" #Or http://somesite.com/subsite
- $credentials = Get-Credential
- $listName = "Some List"
- $listTemplateFileUrl = "d:whatever"
- $listTemplateName = "List Template Name"
- UploadAndCreate-CustomListTemplate $rootUrl $url $credentials $listName $listTemplateFileUrl $listTemplateName
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement