4

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?

7

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.

5

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

  • 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
1

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)
}
0

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
0

This post shows you how to upload files to a document library using CSOM.

What the script does:

  1. Connects to your sharepoint site
  2. Loads the document library into which the .stp files have to be uploaded.
  3. Traverses the folder that contains your .stp files & uploads them to the document library

Here is the script:

# Specify tenant admin and site URL
$User = "admin@tenant.onmicrosoft.com"
$SiteURL = "https://tenant.sharepoint.com/sites/site"
$Folder = "C:\FilesToUpload" # folder that contains the .stp files
$DocLibName = "List Template Gallery"; # This is where you want to upload your files

# Add references to SharePoint client assemblies and authenticate to Office 365 site – required for CSOM
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
$Password = Read-Host -Prompt "Please enter your password" -AsSecureString

# Bind to site collection
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User, $Password)
$Context.Credentials = $Creds

# Retrieve list
$List = $Context.Web.Lists.GetByTitle($DocLibName)
$Context.Load($List)
$Context.ExecuteQuery()

# Upload files from folder
Foreach ($File in (dir $Folder -File))
{
    $FileStream = New-Object IO.FileStream($File.FullName,[System.IO.FileMode]::Open)
    $FileCreationInfo = New-Object Microsoft.SharePoint.Client.FileCreationInformation
    $FileCreationInfo.Overwrite = $true
    $FileCreationInfo.ContentStream = $FileStream
    $FileCreationInfo.URL = $File
    $Upload = $List.RootFolder.Files.Add($FileCreationInfo)
    $Context.Load($Upload)
    $Context.ExecuteQuery()
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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