Creating SharePoint Document Libraries is normally quick and easy.  We came across this client who wanted near hundred libraries and each library had 3 of the same custom columns in.

Problem:
You need to create more than one Document Library in SharePoint and all of them should have the same custom columns added.

Solution:
For the columns that needs to be added in the SharePoint Document Library; add the columns as site columns if they look the same and have the same data-type.  Microsoft’s office website have a good tutorial on this. (Office Site – Site Column Tutorial).

After you have done this you need to create a CSV file for all the SharePoint Document  Libraries that needs to be created and you will also have to provide the site name.  The CSV looks like this: (Also available in the download)

CSV File for SharePoint Document Libraries - Corvitech

Layout of the CSV file required (DocLib.csv)

You will have to change a few items in the provided PowerShell script.

Here are the script:

param([switch]$delete)
 
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
$url = "http://localhost:30444/engagementcollaboration/GIS/"
$listTemplate = [Microsoft.SharePoint.SPListTemplateType]::DocumentLibrary
 
function CreateSitesAndDocLibs() {
 $data = Import-Csv "C:\DocLib.csv"
 
 $web.Dispose()
 $site.Dispose()
 
 foreach($row in $data)
 {
 	$site = CheckSiteName -siteName $row.Site
 	$webUrl = $url + $site
 
	#Check if site exists
	$OpenWeb = Get-SPWeb $webUrl -ErrorVariable err -ErrorAction SilentlyContinue -AssignmentCollection $assignmentCollection 
	if($err)
	{
		#Create the site
		$OpenWeb = New-SPWeb -url $webUrl -name $row.Site -template "STS#0"
		Write-Host Site $row.Site created.
	}
	else
	{
		write-Host Site $row.Site exists.
	}
 
	$OpenWeb.Lists.Add($row.DocLibs,"",$listTemplate) 
	$docLib = $OpenWeb.Lists[$row.DocLibs]
	$docLib.OnQuickLaunch = $true
	$docLib.EnableVersioning = $true
 
	$SecurityClassification=$OpenWeb.Fields.GetFieldByInternalName("SecurityClassification")
	$docLib.Fields.Add($SecurityClassification)
 
	$DocumentType=$OpenWeb.Fields.GetFieldByInternalName("DocumentType")
	$docLib.Fields.Add($DocumentType)
 
	$Client=$OpenWeb.Fields.GetFieldByInternalName("Client")
	$docLib.Fields.Add($Client)
 
	$EngagementCode=$OpenWeb.Fields.GetFieldByInternalName("EngagementCode")
	$docLib.Fields.Add($EngagementCode)
 
	$Sensitivity=$OpenWeb.Fields.GetFieldByInternalName("Sensitivity")
	$docLib.Fields.Add($Sensitivity)
 
	$Team=$OpenWeb.Fields.GetFieldByInternalName("Team")
	$docLib.Fields.Add($Team)
 
	$Department=$OpenWeb.Fields.GetFieldByInternalName("Department")
	$docLib.Fields.Add($Department)
 
	$docLib.Update()
 
	$view = $docLib.DefaultView
	$view.ViewFields.add("SecurityClassification")
 	$view.ViewFields.add("DocumentType")
	$view.ViewFields.add("Client")
	$view.ViewFields.add("EngagementCode")
	$view.ViewFields.add("Sensitivity")
	$view.ViewFields.add("Team")
	$view.ViewFields.add("Department")
 
	$view.Update()
 
	Write-Host Created document library $row.DocLibs.
 }
 
}
 
function CheckSiteName([string]$siteName) {
	return $siteName.Replace("&","and")
}
 
function DeleteSites() {
 $data = Import-Csv "C:\Book1.csv"
 foreach($row in $data)
 {
 	$site = CheckSiteName -siteName $row.Site
 	$webUrl = $url + $site
	#Check if site exists
	$OpenWeb = Get-SPWeb $webUrl -ErrorVariable err -ErrorAction SilentlyContinue -AssignmentCollection $assignmentCollection 
	if($err)
	{
		#Do nothing
		Write-Host Site $row.Site not found.
	}
	else
	{
		Remove-SPWeb $webUrl -Confirm:$false
		write-Host Site $row.Site deleted.
	}
 }
}
 
if($delete) {
	DeleteSites
}
else {
	CreateSitesAndDocLibs
}

 

You will have to change the line that starts with $url to your SharePoint site where the SharePoint Document Libraries needs to be added.  After this change make sure the csv file is still the same as the one you created.

The last thing that you will have to edit is to change the values to your site columns you created. Fine the lines that look like the following:

$SecurityClassification=$OpenWeb.Fields.GetFieldByInternalName("SecurityClassification")
$docLib.Fields.Add($SecurityClassification)

Change the “SecurityClassification”to what ever you called your site columns.  Make sure you have this two lines for each site column you want to add.  Delete the lines you are not using.

Open PowerShell and make sure you are running in SharePoint Administration mode. Run the code provided with that changes you have made.  If all goes well you will have those SharePoint Document Libraries up and running in no time at all.

Good luck and please, I’m always open for questions and suggestions. You can contact me on Skype @ corvitech
Download