Wednesday, May 28, 2014

SharePoint 2010: Create Document Sets via PowerShell script.

Context: The customer wanted me to create nearly 5000 document sets in a document library of a SharePoint 2010 site by using a 'Csv file'.


Solution: I wrote a power shell script which uses the ‘Microsoft.SharePoint.PowerShell’ to create document sets. The following Power Shell script that I wrote;

if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null)   
{
    Add-PSSnapin Microsoft.SharePoint.PowerShell
}
### Load SharePoint Object Model   
[System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.SharePoint”)   

### Get web and list   
$web = Get-SPWeb http://sp10dev:2011/sites/spsite 
$list = $web.Lists["ListName"]
$cType = $list.ContentTypes["ContentTypeName"]

### Import Csv 
$csv = Import-Csv desktop\SampleData.csv

### Get document name and create document set one by one
foreach($row in $csv)
{
    $docsetname = $row.DocumentSetName
    #write-Host $docsetname

$newDocumentSet = [Microsoft.Office.DocumentManagement.DocumentSets.DocumentSet]::Create($list.RootFolder,$docsetname,$cType.Id,$docsetProperties)
}

$web.Dispose()

No comments:

Post a Comment