Quantcast
Channel: SharePoint 2013 - Development and Programming forum
Viewing all articles
Browse latest Browse all 7589

Powershell: How do I upload files with metadata from a manifest file?

$
0
0

I am using the script from this blog to try to upload files into SharePoint, using a manifest file to specify the metadata associated with each file. Right now, the script works, but is not populating the metadata from the manifest (xml) file. When I write the $metadataManifest variable out to the console, I get the contents of my xml file, so the code is reading the file when it should. However when it gets to the line in bold ($metadataManifest.Columns.Column), it does not go into that block...it just skips on to checkin the file. Again the $metadataManifest variable shows all the content in my xml manifest file. Here is what the script looks like...

#Get web and document library objects
$web = Get-SPWeb "http://companySite"
$Library = "My Library"
$docLibrary = $web.Lists[$Library]
$ManifestFilePath = "C:\PowerShellScripts\Manifest.xml"
$LocalPath = "C:\Upload\Test Upload\My Upload Directory\"

if ($ManifestFilePath)
{
    $metadataManifest = (Get-Content ($ManifestFilePath))
    write-host "Manifest file: " $metadataManifest
}
else
{
    write-host "Manifest file not specified for categorising uploaded documents"
}

#Check for the LibraryStartFolder parameter to specify a root folder
if ($PSBoundParameters.ContainsKey('LibraryStartFolder')) {
	$folder = $web.GetFolder($docLibrary.Title + $LibraryStartFolder)
}
else
{
	$folder = $docLibrary.RootFolder
}

#Attach to local folder and enumerate through all files
if($IncludeSubFolders) {
	$files = Get-ChildItem $LocalPath -Recurse
}
else
{
	$files = Get-ChildItem $LocalPath
}

$files | ForEach-Object {

	#Check if the object is a folder - if so, create it in doc library
	if ($_.PSIsContainer) {
		if (($IncludeSubFolders) -and (!$FlattenStructure)) {
			#Generate folder path for creation in SharePoint
			#by looking at the parent folder on the local path
			$spFolderPath = ($_.Parent.FullName.Replace($LocalPath,"")).Replace("\","/")

			#Get the folder into which the new folder will be created
			#by adding the folder path generated above, if one existed
			if ($spFolderPath -eq "") {
				$currentFolder = $web.GetFolder($folder.Url)
			}
			else
			{
				$currentFolder = $web.GetFolder($folder.Url + $spFolderPath)
			}

			#Check to see if subfolder already exists
			#and create it if not
			$testFolder = $currentFolder.SubFolders[$_.Name]
			if ($testFolder -eq $null) {
				write-host "`nAdding folder" $_.Name "to" $docLibrary.Title "in" $web.Title "..." -foregroundcolor Green
				$newFolder = $currentFolder.SubFolders.Add($_.Name)
			}
			else
			{
				write-host "`nFolder" $_.Name "already exists in" $docLibrary.Title "and shall not be created" -foregroundcolor Red
			}
		}
	}
	else
	{
		#Generate file path for upload into SharePoint
		if ($FlattenStructure) {
			$spFilePath = ("/" + $_.Name)
		}
		else
		{
			$spFilePath = ($_.FullName.Replace($LocalPath,"")).Replace("\","/")
		}

		$spFullPath = $folder.Url + "/" + $spFilePath

		#Check if the file exists and the overwrite option is selected before adding the file
		if ((!$web.GetFile($spFullPath).Exists) -or ($Overwrite)) {
			#Add file
			write-host "`nCopying" $_.Name "to" $spFullPath.Replace("/" + $_.Name,"") "in" $web.Title "..." -foregroundcolor Green
			$spFile = $folder.Files.Add($spFullPath, $_.OpenRead(), $true)
            #$spFile = $folder.Files.Add($folder.Url + "/" + $file.Name, [System.IO.Stream]$fileStream, $true)
			$spItem = $spFile.Item

			#Walk through manifest XML file and configure column values on the file
			$metadataManifest.Columns.Column | ForEach-Object {
				#Single value text columns
				try
				{
					if (($_.Type -eq "Text") -or
						($_.Type -eq "Choice") -or
						($_.Type -eq "Boolean") -or
						($_.Type -eq "Number") -or
						($_.Type -eq "Currency")) {
						$columnName = $_.Name
						write-host "Setting value on column"$columnName "..." -foregroundcolor Blue
						$_.Values.Value | ForEach-Object {
							$spItem[$columnName] = $_
							write-host "Value set to"$_
						}
					}
				}
				catch {}

				#Multiple line text column
				try
				{
				}
				catch {}

				#Multiple choice columns
				try
				{
				}
				catch {}

				#Hyperlink columns
				try
				{
				}
				catch {}

				#Single User column
				try
				{
				}
				catch {}

				#Multiple User column
				try
				{
				}
				catch {}

				#Single value Managed Metadata column
				try
				{
				}
				catch {}

				#Multi value Managed Metadata column
				try
				{
				}
				catch {}

				#Update document with new column values
				$spItem.SystemUpdate($false)
			}

			#Check in file to document library (if required)
			#MinorCheckIn=0, MajorCheckIn=1, OverwriteCheckIn=2
			if ($CheckIn) {
				if ($spFile.CheckOutStatus -ne "None") {
					$spFile.CheckIn("File copied from " + $filePath, 1)
					write-host $spfile.Name"checked in"
				}
			}

			#Approve file (if required)
			if ($Approve) {
				if ($spItem.ListItems.List.EnableModeration -eq $true) {
					$spFile.Approve("File automatically approved after copying from " + $filePath)
					if ($spItem["Approval Status"] -eq 0) { write-host $spfile.Name"approved" }
				}
			}
		}
		else
		{
			write-host "`nFile"$_.Name "already exists in" $spFullPath.Replace("/" + $_.Name,"") "and shall not be uploaded" -foregroundcolor Red
		}
	}
}


$web.Dispose()
    

And here is what the manifest file looks like...

<?xml version="1.0" encoding="utf-8"?><Columns><Column Name="Column1" Type="Text"><Values><Value>First File</Value></Values></Column><Column Name="Column2" Type="Text"><Values><Value>12585</Value></Values></Column><Column name="Column3" type="Text"><Values><Value>Some Data</Value></Values></Column><Column name="Column4" type="Text"><Values><Value>More Data</Value></Values></Column></Columns><Columns><Column Name="Column1" Type="Text"><Values><Value>Second File</Value></Values></Column><Column Name="Column2" Type="Text"><Values><Value>9876</Value></Values></Column><Column name="Column3" type="Text"><Values><Value>Some Data2</Value></Values></Column><Column name="Column4" type="Text"><Values><Value>More more Data</Value></Values></Column></Columns>

I can't figure out what am doing wrong...why my script is not iterating through the manifest file to upload the document along with the metadata. I would really appreciate any help. Thanks


Viewing all articles
Browse latest Browse all 7589

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>