Advertisement
Guest User

Untitled

a guest
Apr 18th, 2015
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.12 KB | None | 0 0
  1. if ($ver.Version.Major -gt 1) {$host.Runspace.ThreadOptions = "ReuseThread"}
  2. if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null)
  3. {
  4. Add-PSSnapin "Microsoft.SharePoint.PowerShell"
  5. }
  6.  
  7.  
  8. Function UploadListTemplate($WebURL, $TemplateFilePath)
  9. {
  10. #Get the Web
  11. $web = Get-SPWeb $WebURL
  12.  
  13. #Get the List template Gallery Folder
  14. $TemplateFolder = $web.GetFolder("List Template Gallery")
  15.  
  16. #Get the Files collection
  17. $TemplateFileCollection = $TemplateFolder.Files
  18.  
  19. #Get the Template file from Local File system
  20. $TemplateFile = Get-ChildItem $TemplateFilePath
  21.  
  22. #Open the File in Read mode and Add to Templates collection
  23. $TemplateFileCollection.Add("_catalogs/lt/$($TemplateFile.Name)"
  24. $TemplateFile.OpenRead(), $true)
  25. Write-Host "Done!Template has been uploaded!!"
  26. }
  27.  
  28. #Call the function
  29. UploadListTemplate "http://sharepoint.crescent.com" "D:TemplatesCustomTaskList.stp"
  30.  
  31. function UploadAndCreate-CustomListTemplate()
  32. {
  33. param(
  34. [Parameter(Mandatory=$true)][string]$rooturl,
  35. [Parameter(Mandatory=$true)][string]$listCreationUrl,
  36. [Parameter(Mandatory=$false)][System.Net.NetworkCredential]$credentials,
  37. [Parameter(Mandatory=$true)][string]$listName,
  38. [Parameter(Mandatory=$true)][string]$listTemplateFileUrl,
  39. [Parameter(Mandatory=$true)][string]$listTemplateName
  40.  
  41. )
  42.  
  43. try
  44. {
  45. # Adding the PowerShell Snapin
  46. Add-PSSnapin “Microsoft.SharePoint.PowerShell”
  47.  
  48. # Get the SiteURL
  49. $site = get-spsite($rooturl)
  50.  
  51. # Get the root web
  52. $web = $site.RootWeb
  53.  
  54. # Get the list template gallery
  55. $spLTG = $web.getfolder(“List Template Gallery”)
  56.  
  57. # Get the list template gallery Collection
  58. $spcollection = $spLTG.files
  59.  
  60. # Get the custom list template file
  61. $Templatefile = get-item $listTemplateFileUrl
  62.  
  63. # Add the custom list template file to gallery
  64. $titleHashTbl = @{}
  65. $titleHashTbl.Add("Title", $listTemplateName)
  66. $folderListTemplate = "_catalogs/lt/" + $listTemplateName + ".stp"
  67. $spcollection.Add($folderListTemplate, $Templatefile.OpenRead(),$titleHashTbl,$true)
  68. Write-Host “Custom Template Uploaded to List Template Gallery Successfully”
  69.  
  70. Write-Host “Creating the List based on the Template”
  71. # Get the custom list templates
  72. $CustomlistTemplates = $site.GetCustomListTemplates($web)
  73.  
  74. #Create the custom list using template
  75. $site = Get-SPWeb $listCreationUrl
  76. $site.Lists.Add($listName, $listName, $CustomlistTemplates[$listTemplateName])
  77. Write-Host "Based on the template List Created"
  78. }
  79. catch
  80. {
  81. Write-Host ("Error while creating list. Error -->> " + $_.Exception.Message) -ForegroundColor Red
  82. }
  83.  
  84. }
  85.  
  86. $rootUrl = "http://somesite.com"
  87. $url = "http://somesite.com" #Or http://somesite.com/subsite
  88. $credentials = Get-Credential
  89. $listName = "Some List"
  90. $listTemplateFileUrl = "d:whatever"
  91. $listTemplateName = "List Template Name"
  92.  
  93.  
  94. UploadAndCreate-CustomListTemplate $rootUrl $url $credentials $listName $listTemplateFileUrl $listTemplateName
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement