SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need to script the creation of data in SharePoint, and part of this requires that I upload a slide library list template using PowerShell. Is this possible?

share|improve this question

WebDav might work for others, but I found a way to do it using the SharePoint object model. (I used the folders indicated by Steve B--thanks.)

$site = get-spsite "http://myteam.lab/sites/team"
$web = $site.OpenWeb()
$spFolder = $web.getfolder("List Template Gallery")
$spfilecollection = $spfolder.files
$file = get-item c:\temp\MyTemplate.stp
$spfilecollection.Add("_catalogs/lt/MyTemplate.stp", $file.OpenRead(), $true)

I saw the template in the "Create" page, so everything seemed to work ok.

share|improve this answer

You can cheat using Webdav :

Copy-Item 'yourTemplate.stp' '\\yourspwebapp\DavWWWRoot\sites\yoursitecoll\_catalogs\lt'

[edit] according your comment, here is a way to find the correct url.

  1. Navigate using the browser to the list template catalog list. The link should look like : http://yourwebapp/sites/yoursitecoll/_catalogs/lt
  2. as it is a document library, you can, from the ribbon's tab "Library", click on Open in windows explorer
  3. an windows explorer windows pop ups, but with "http://" address.
  4. Right click in the window, click properties, and you will find the actual address.

This worked for me, with a SP application having a custom AAM (yourspwebapp is not the hostname, but a virtual hostname). I checked locally... did you want to do this remotely ?

[Edit2] if installing Webdav is an issue on your server, you can use the following snippet :

$wc = new-object System.Net.WebClient
$file = (Get-Item '.\New Text Document.txt')
$dest = "http://yourspwebapp/sites/yoursitecoll/_catalogs/lt"
$wc.UseDefaultCredentials = $true
$wc.UploadFile($dest +"/" + $file.Name, $file.FullName)

Or you can use the sp OM, as Jimmarq suggests

share|improve this answer
    
Hmm. I'm getting WebDav errors. I can't even map a drive to My Site. I must have a problem doing this from a 2008 R2 box. – jimmarq Jun 28 '11 at 13:28
    
I'm also running on a 2008 R2 box – Steve B Jun 28 '11 at 13:38
    
When I click "Open with Exporer" I get an error message saying "Your client does not support opening this list with Windows Explorer." I'm guessing I'm missing either office components, or the Desktop Experience feature. – jimmarq Jun 28 '11 at 13:47
    
so this is another issue. Maybe this can help : mcgeeky.blogspot.com/2010/02/… or blogs.technet.com/b/seanearp/archive/2010/07/08/… – Steve B Jun 28 '11 at 13:49

To add to this, if you would like to add the list template to all of your site collections, use this instead...

$GetSiteCollections = Get-SPSite -Limit ALL
## Add the List Template to the all the Site Collections
foreach($GetSiteCollection in $GetSiteCollections)
{
$web = $GetSiteCollection.OpenWeb();
$spfolder = $web.getfolder("_catalogs\lt")
$spfileCollection = $spfolder.Files
$file = Get-Item c:\Templates\MyListTemplate.stp
$spfileCollection.Add("MyListTemplate.stp", $file.OpenRead(), $true)
}
share|improve this answer

You use the below function which uploads the list template and also creates a list based on that.

Explanation of Parameters:

  1. $rooturl: its the url of the root web
  2. $listCreationUrl: is the url in which you would want to create a list
  3. $listName: Name of the list as you wish
  4. $listTemplateFileUrl: file path where the list template .stp file is located on your machine
  5. $listTemplateName: Important, What ever name was given while saving list as a template.

Function:

function UploadAndCreate-CustomListTemplate()
{
param(
        [Parameter(Mandatory=$true)][string]$rooturl,
        [Parameter(Mandatory=$true)][string]$listCreationUrl,
        [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
    }

}

Usage:

$rootUrl = "http://somesite.com"
$url = "http://somesite.com" #Or http://somesite.com/subsite
$listName = "Some List"
$listTemplateFileUrl = "d:\whatever"
$listTemplateName = "List Template Name"


UploadAndCreate-CustomListTemplate $rootUrl $url $listName $listTemplateFileUrl $listTemplateName
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.