Monday 19 August 2013

Import Document set from XML

In my previous post I exported a data set to a file structure utilizing xml to store my meta data (Export document set) now I'm going to post a script to import that document set back into SharePoint, now i warn you this one is a bit quicker and dirtier then the previous, you're going to have tinker with it a bit for your list but it'll get you started.

   CCW Powershell Function to Restore Tenders Document Set
   Powershell functiont that itterates over a folder containing previously extracted tender document sets and restores them from an xml file that contains all required metadata
   Example of how to use this cmdlet
function Restore-ccwTenders
        # the site url of your site collection ie http://wingtipserver
                              HelpMessage="Enter in the site URL where the Tenders Documnet set resides")] 
              [ValidateScript({Get-SPSite -Identity $_ | Select-Object -Property Exists -ErrorAction SilentlyContinue})]
              #The url of the tender document set
                              HelpMessage = "Enter in the Display Name of the Tenders Document set")]
              #The Path on your local hardrive from which to restore the document set from
                              HelpMessage = "Enter the file path where to Load your tenders from")]
              [ValidateScript({Test-Path $_})]

              Add-PSSnapin "Microsoft.Sharepoint.PowerShell" -ErrorAction SilentlyContinue
              #get site using user supplied parameter
              $site = Get-SPSite($siteUrl)

              #get the Root web of the site
              $rootWeb = $site.RootWeb
              #Get the Tenders document set
              $DocumentSetList = $rootWeb.Lists.TryGetList($DocumentSetName)
              if($DocumentSetList -ne $null)
                     #Get the conent type by name,
                     $cType = $DocumentSetList.ContentTypes["Tenders"]
                     Get-ChildItem -Path $ComputerPath |
                     ForEach-Object {
                           $DocSet_HashTable = getTenderMetaData($_) as [System.Collections.Hashtable]
                           $newDocumentSet = [Microsoft.Office.DocumentManagement.DocumentSets.DocumentSet]::Create($tendersList.RootFolder,$, $cType.Id, $DocSet_HashTable)  
                           uploadDocuments $rootWeb $_ $DocumentSetList.RootFolder
                           Write-Host "Created Document set: $_ " -foregroundcolor DarkGreen

function getTenderMetaData($DocSetName)
       $xmlPath = $ComputerPath + "\" + $DocSetName + "\tender.xml"
       Write-Host $xmlPath
       if((Test-Path -Path $xmlPath) -eq $true)
              $Metadata_HashTable = @{}
              Select-Xml -Path $xmlPath -XPath "/DS/Metadata/Data" |
              ForEach {
                     [string] $node = $_.Node | select -ExpandProperty Title
                     [string] $internalName = $_.Node | select -ExpandProperty InternalName
                     [string] $readOnly = $_.Node | select -ExpandProperty ReadOnly
                     [string] $value = $_.Node | % { $_.InnerText }
                     #Write-host ("Title={0},value={1}`n" -f $node, $value)
                     if($readOnly -eq "False")
                                  "Title" {$Metadata_HashTable.Item("Title")=$value}
                                  "Description" {$Metadata_HashTable.Add("DocumentSetDescription", $value)}
                                  "Tender Category" {$Metadata_HashTable.Add("ccwTenderCategoryLookup", $value)}
                                  "Id Number" {$Metadata_HashTable.Add("ccwIdNumber", $value)}
                                  "Request Type" {$Metadata_HashTable.Add("ccwRequestType", $value)}
                                  "Start Time" {$Metadata_HashTable.Add("ccwTenderStartTime", [System.DateTime]$value)}
                                  "End Time" {$Metadata_HashTable.Add("ccwTenderEndTime", [System.DateTime]$value)}
                                  "Extension" {$Metadata_HashTable.Add("ccwTenderExtensionTime", [System.DateTime]$value)}
                                  default {$Metadata_HashTable.Add($internalName, $value)}
              return $Metadata_HashTable

function uploadDocuments($web, $DocSetName, $DocSetRootFolder)
       $xmlPath = $ComputerPath + "\" + $DocSetName + "\tender.xml"
       Write-Host $xmlPath woot  -ForegroundColor DarkCyan
       if((Test-Path -Path $xmlPath) -eq $true)
              Select-Xml -Path $xmlPath -XPath "/DS/Files/File" |
              ForEach {
                     $file_HashTable = @{}
                     $ContentTypeName = $_.Node | select -ExpandProperty ContentType
                     $SortOrder = $_.Node | select -ExpandProperty SortOrder
                     $relativeFilePath = $_.Node | select -ExpandProperty FilePath
                     $AttachmentURLPath = $_.Node | select -ExpandProperty urlPath
                     $AttachmentFilePath = [system.IO.path]::Combine($ComputerPath , $relativeFilePath)
                     write-host $AttachmentFilePath   
                     #Write-Host ("Content type = `"{0}`"`nSort Order = `"{1}`"`nFile Path = `"{2}`"`nUrl Path = `"{3}`"" -f $ContentTypeName, $SortOrder, $AttachmentFilePath, $AttachmentURLPath) -ForegroundColor DarkBlue
                           "Addendum" {
                                  $file_HashTable.add("ContentTypeId", "0x0101001cf9a0ea451d4c49971bce57ae51e3df001bea931548e949eeadeb7945c74f41b2")
                                  $file_HashTable.add("ContentType", "Tender Addendum")
                                  $file_HashTable.add("ccwTenderAddendumSortOrder", $SortOrder)
                           "Request Document"{
                                  $file_HashTable.add("ContentTypeId", "0x0101001CF9A0EA451D4C49971BCE57AE51E3DF")
                                  $file_HashTable.add("ContentType", "Tender Request Document")
                           "Unofficial Results"{
                                  $file_HashTable.add("ContentTypeId", "0x0101001cf9a0ea451d4c49971bce57ae51e3df00e608217df15942e389ca1810df08f8dc")
                                  $file_HashTable.add("ContentType", "Tender Unofficial Results")
                     #Create instance of Folder
                     $spFolder = $web.GetFolder($DocSetRootFolder)
                     Write-Host $AttachmentFilePath -ForegroundColor Magenta
                     $file = Get-Item $AttachmentFilePath
                     $fileStream=([System.IO.FileInfo](Get-Item $file.FullName)).OpenRead()
                     $spfile = $spFolder.Files.Add("Lists" + $AttachmentURLPath, [System.IO.Stream]$fileStream, $file_HashTable, $true)