Create document library using Power Shell with xml data as input
We had a business requirement to automate Creating of Same document library's, Folders and fields inside that in more 110 sites which will be repeated for year.
Hence we created an xml file with all the document library names and folder names and fields inside that .
Powershell script will read the xml file contents and based on that we are creating the folder structure in document library.
PowerShell Script :
clear
# Add the Powershell snapin,
if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null)
{
Add-PSSnapin "Microsoft.SharePoint.PowerShell"
}
# Load the SharePoint Assembly
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
# Read the Site Url from User
$url = Read-host " Enter Name of the Site to Add Document
Library"
$listTemplate =
[Microsoft.SharePoint.SPListTemplateType]::DocumentLibrary
# xml file contains list of documentlibrary's , folders, fields to be created in the site
[xml]$file = Get-Content "C:\XmlDocumentLibFolderStructure.xml"
function CreateDocLibs()
{
#Check if the
site exists
$OpenWeb = Get-SPWeb $url -ErrorVariable err -ErrorAction SilentlyContinue -AssignmentCollection $assignmentCollection
if($err)
{
write-host " Site is
Not Exisiting Kindly contact Administrator"
}
else
{
write-Host "Site
"
$url "
exists"
}
foreach( $ls in $file.Structure.Lists.List)
{
$doclibname=$ls.ListName
# Creating
Document Library
Write-Progress " Please
wait writing Document Library" $ls.ListName
$checkDoclib=$OpenWeb.Lists[$doclibname]
# Check if the Document is null i.e
document libary is not created.
if($checkDoclib -eq $null)
{
$OpenWeb.Lists.Add($doclibname,"",$listTemplate)
Write-Progress "Created
Document Library successfully"
Write-Progress "Creating
Fields"
$lib=$OpenWeb.Lists[$doclibname]
Write-Host $lib.Url
$lib.EnableVersioning
= $true
Write-Host "Document
library with " $doclibname " Created"
Write-Progress " Please
wait Adding Fields to " $doclibname
$fldXml = "<Field
Type='MultiChoice' DisplayName='Document Status' Name='DocumentStatus' >
<CHOICES>
<CHOICE>Review
Required</CHOICE>
<CHOICE>Review in
Progress</CHOICE>
<CHOICE>Review
Completed</CHOICE>
</CHOICES>
</Field>"
$lib.Fields.AddFieldAsXml($fldXml,$true,[Microsoft.SharePoint.SPAddFieldOptions]::AddFieldToDefaultView)
$lib.Update()
$spFieldTypeDocNum
=[Microsoft.SharePoint.SPFieldType]::Text
$lib.Fields.Add("Doc
No",$spFieldTypeDocNum,$false)
$lib.Update()
# get the folder
option value from " XmlDocumentLibFolderStructure.xml"
Write-Progress "Creating
Folder Structure ........ Please wait..."
sleep 1
$foldername = $file.Structure.FolderOptions.Folder
foreach($folder in $ls.FolderOption)
{
$values = $foldername | where {$_.option -eq $folder}
$splitvalues= $values."#text" -split ","
foreach($i in $splitvalues)
{
$folder1 = $lib.AddItem("",[Microsoft.SharePoint.SPFileSystemObjectType]::Folder,$i)
$folder1.Update()
}
}
}
# If Document
Library is Already Existing skip it
else
{
Write-Host "Document
Library al
ready
Exists with " $doclibname " can not be overrided"
}
}
}
CreateDocLibs
============================
XML File :
<Structure>
<FolderOptions>
<Folder Option="1">2014 01,2014 02,2014 03,2014 04,2014 05,2014 06,2014 07,2014 08,2014 09,2014 10,2014 11,2014 12</Folder>
<Folder Option="2">2014 03,2014 06,2014 09,2014 12</Folder>
<Folder Option="3">2014</Folder>
</FolderOptions>
<Lists>
<List>
<ListName>2.1 Statements 2014</ListName>
<FolderOption >1</FolderOption>
<Fields>
<Field Type="Text" Name="Doc No"></Field>
<Field Type="Choice" Name="Document Status"></Field>
</Fields>
</List>
<List>
<ListName>2.2 Other Documents 2014</ListName>
<FolderOption>1</FolderOption>
<Fields>
<Field Type="Text" Name="Doc No"></Field>
<Field Type="Choice" Name="Document Status"></Field>
</Fields>
</List>
<List>
<ListName>3.1 Account Reconciliations 2014</ListName>
<FolderOption>1</FolderOption>
<Fields>
<Field Type="Text" Name="Doc No"></Field>
<Field Type="Choice" Name="Document Status"></Field>
</Fields>
</List>
<List>
<ListName>3.2 BS Open Items Reports 2014</ListName>
<FolderOption>1</FolderOption>
<Fields>
<Field Type="Text" Name="Doc No"></Field>
<Field Type="Choice" Name="Document Status"></Field>
</Fields>
</List>
<List>
<ListName>3.3 Other Closing Material 2014</ListName>
<FolderOption>1</FolderOption>
<Fields>
<Field Type="Text" Name="Doc No"></Field>
<Field Type="Choice" Name="Document Status"></Field>
</Fields>
</List>
<List>
<ListName>3.4 Sign-Off Package 2014</ListName>
<FolderOption>1</FolderOption>
<Fields>
<Field Type="Text" Name="Doc No"></Field>
<Field Type="Choice" Name="Document Status"></Field>
</Fields>
</List>
<List>
<ListName>5.2 FA - Fixed Asset Sales 2014</ListName>
<FolderOption>1</FolderOption>
<Fields>
<Field Type="Text" Name="Doc No"></Field>
<Field Type="Choice" Name="Document Status"></Field>
</Fields>
</List>
</Lists>
No comments:
Post a Comment