Get ListTemplate and Create New List in PowerShell

A PowerShell related post today:

To create a list or library in SharePoint, you can do the following:

$web = Get-SPWeb "http://serverUrl"
$web.Lists.Add("List Title", "List description", "Template Name")

Now, say you were creating lists based on the schema from another list:

$web = Get-SPWeb "http://anotherServerUrl"
$list = $eb.Lists.get_Item("List name")
$listSchema = $list.SchemaXml

The SchemaXml from a list only contains the ServerTemplate numeric value, which can not be passed to the Add method in a PowerShell script.

<List ID="..." ServerTemplate="100" Title="...">
    <Fields/>
</List>

Unlike adding a list using C#, where you can get the SPListTemplateType from the ServerTemplate value:

SPListTemplateType templateType = (SPListTemplateType)100; // 100 is Generic List

You can’t pass the numeric value of the list template into the Add method. If you know the template value it’s easy to get the matching object by using the following script:

$web = Get-SPWeb "http://serverUrl"
$template = $web.ListTemplates | Where-Object {$_.Type -eq 100}
# Now add the list using the retrieved template object
$web.Lists.Add("List Title", "List description", $template)

One comment

  1. Panoone says:

    The -ExpandedProperty for Lists has a BaseTemplate property.

    e.g.

    Get-SPWeb http://myweb |
    Select -ExpandProperty Lists |
    # Filter out list template by ID
    Where { $_.BaseTemplate -ne 109 }

Leave a Reply

Your email address will not be published. Required fields are marked *

Solve the maths problem shown below before posting: *

Follow

Get every new post delivered to your Inbox

Join other followers: