1

I am trying to create a document library from a template. The template name has spaces in it through.

#Add the SharePoint snapin
write-host "Loading powershell module" -ForegroundColor Cyan
Add-PSSnapin Microsoft.SharePoint.Powershell -ea SilentlyContinue

# specify the source and destination sites
$sourceWeb = "http://mysite";
$DestinationWeb = "http://mysite/Search";


# get the source and destination sites web objects
$sourceWebObj = Get-SPWeb $sourceWeb;
$DestinationWebObj = Get-SPWeb $DestinationWeb; 



$listTemplate = [Microsoft.SharePoint.SPListTemplateType]::"My Enterprise Documents"
$DestinationWebObj.Lists.Add("Documents_Archieve","My Doc Library",$listTemplate);

The error:

Exception calling "Add" with "3" argument(s): "Value cannot be null.
Parameter name: Invalid argument 'template' to SPListCollection.Add() method.  Please pass an argument of the appropriate type."
At C:\script\archive3.ps1:32 char:29
+ $DestinationWebObj.Lists.Add <<<< ("Documents_Archieve","My Doc Library",$listTemplate);
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

Does anyone know what's wrong?

Thanks

1

Your variable $listtemplate will be null as you can only get the default templates in this manner. To get your custom template you will need to do something like this:

$site = Get-SPSite http://yoursite
$web = Get-SpWeb http://yoursite/yourweb
$custTemplate = $site.GetCustomListTemplates($web)

You can confirm your template is available:

$custTemplate | select name

Then create a list like this:

$DestinationWebObj.Lists.Add("Documents_Archieve","My Doc Library",$custTemplate["My Enterprise Documents"]);

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.