Thursday, 23 August 2012

Script to Create Multiple Libraries using Custom Template


Creating a document library is very simple. But always adding columns & setting the view of that library same as other becomes a time consuming task. So, to avoid these tasks I performed all the necessary tasks on library & saved that document library as template (CustomLibraryTemplate.stp) & then while creating a new document library I selected the same template(CustomLibraryTemplate) while creating new document library. This solution made it easier to create document libraries with the specified columns & the default view.

Now creating limited libraries is possible, but creating multiple document libraries using the same template again became the time consuming task as I had to create each document library manually. Then I searched for the powershell script which can create these libraries on One click.

The below given script gets the name of the Document Library & the name of the template using which the document library is to be created from the CreateDocumentLibraries.csv file & creates the libraries or lists as per the information stored in the csv file. 

This script creates the libraries & lists using the custom template as well as the default templates(DocumentLibrary, PictureLibrary).



$url = "http://servername/sites/sitename"
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") > $null
$spSite = New-Object Microsoft.SharePoint.SPSite($url)
$spWeb = $SPSite.OpenWeb()
$listTemplates = $spSite.GetCustomListTemplates($spWeb)
$listTemplate1 = [Microsoft.SharePoint.SPListTemplateType]::DocumentLibrary
$listTemplate2 = [Microsoft.SharePoint.SPListTemplateType]::PictureLibrary
$filePath= "FilePaht\CreateDocumentLibraries.csv"
$data = Import-Csv $filePath
$spListCollection = $spWeb.Lists
 foreach($line in $data)
 {
 $DocLibs=$line.DocLibs.trim()
 #$DocDescription=$line.DocDescription.trim()
 $listTemplate=$line.listTemplate.trim()
 $spWeb.ListTemplates | ForEach{ write-host $_.Name }
 if($listTemplate -eq "DocumentLibrary")
 {
    $spWeb.Lists.Add($DocLibs,"",$listTemplate1)
 }
 if($listTemplate -eq "PictureLibrary")
 {
    $spWeb.Lists.Add($DocLibs,"",$listTemplate2)
 }
 if($listTemplate -eq "CustomLibraryTemplate")
 {
    $spWeb.Lists.Add($DocLibs,"",$listTemplates["CustomLibraryTemplate"])
 }

 if($listTemplate -eq "CustomListTemplate")
 {
    $spWeb.Lists.Add($DocLibs,"",$listTemplates["CustomListTemplate"])
 }

 if($listTemplate -eq "CustomAnnouncementTemplate")
 {
    $spWeb.Lists.Add($DocLibs,"",$listTemplates["CustomAnnouncementTemplate"])
 }

 if($listTemplate -eq "CustomDiscussionBoardTemplate")
 {
    $spWeb.Lists.Add($DocLibs,"",$listTemplates["DiscussionBoardTemplate"])
 }

 $docLib = $spWeb.Lists[$DocLibs]
 $docLib.Update()
 Write-Host Created document library $line.DocLibs
 }


The entries in the csv file are as follows:

DocLibs,listTemplate
DocumentLibrary,CustomLibraryTemplate
List,CustomListTemplate
Pictures,PictureLibraryTemplate
AnnouncementList,CustomAcnnouncementTemplate
DiscussionBoardList,CustomDiscussionBoardTemplate


 When we have finished creating both the files, now open the Powershell Management tool & run a single command which contains only the path of the powershell script. e.g.- FilePaht\CreateDocumentLibraries.ps1.

The script will stop after all the lists & libraries are created within the site collection.


No comments:

Post a Comment