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)
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
Hi,
First of all, thank you for being the only one i’ve find talking about “Document Libraries” and how to create it with powershell.
I’m a novice with powershell, and i would like to understand your script as I can.
I can’t find any information about things like that, even on microsoft.com :
$OpenWeb.Lists.Add($row.DocLibs,””,$listTemplate)
$docLib = $OpenWeb.Lists[$row.DocLibs]
$docLib.OnQuickLaunch = $true
$docLib.EnableVersioning = $true
I got a csv file, in which i have web app, site collection, subsite on 2 level, TITLE, DOCUMENT LIBRARIES.
For this two last, i can’t find anything =/ Well, now about document libraries I have you ! 🙂
My project deals with create a new SP 2013, which take back the old infrastructure of our SP2010, which is in a csv file.
Hope i was understandable, and i apologize for my english.
Cheers,
Nico.
Hi,
Thanks for the comment.
The first line of that code adds a list to your SharePoint environment. This list is based on the csv which is loaded inside $row. It takes the DocLib column in the CV’s value and then create a list depending on what you selected as the $listTemplate.
After the list/library has been added we assign it to $docLib. This variable then contains your library. We can then set or change settings of the current list. $docLib.EnableVersioning = $true – Enables Versioning on library.
Kind Regards
Thanks for your fast reply.
I think i’ve pretty understand this. I’m now able to create documents library as i want 🙂
The question that remains in my head is : where do you find all the settings you can change, like :
$docLib.EnableVersioning = $true
$docLib.OnQuickLaunch = $true
or things i’ve found on another blog :
$list.EnableMinorVersions = $true
$list.MajorVersionLimit = 3
$list.MajorWithMinorVersionsLimit = 3
I mean, i’m wondering where i can find all attributes related to $list.XXX ?
And, if I can ask you another question 🙂
I have to create things that are called “title” (in french : titre). In the tree, it’s above “document library”, but same as document library, i can’t find anything =/ If you know parameters, or attributes i can find.
It’s just a title, where i can put my documents libraries. To be more organized.
In graphical mode, it seems that it is required to have a link on this kind of title, but if we can delete it, it would be just perfect.
Hope you understand what i’ve said 🙂 If not, let me know.
Thank you again 🙂
thanks !
Haljaluleh! I needed this-you’re my savior.
Hi there, I just seen that occasionally this page renders a 403 server error. I thought that you would be keen to know. Regards
Hi Mark,
Thanks for the feedback. We are aware of this issue, and it seems like our hosting provider is unstable. We are in the process of moving all sites to a different provider.
Thanks and have a good day..