0

following this article: Powershell script to transfer files from local folder to document library

is it possible to add more properties to a file when uploading from a file share to sharepoint,

Forr example، I have an Enterprise Keyword column, and if the file is being copied from a folder X to sharepoint I want it to have the enterprise keyword "X" added as a property to that file.

1

Yes, you can do that.

First add a column in SharePoint document library for example you named the column "Keyword"

Use the following updated script to update managed metadata column:

if((Get-PSSnapin "Microsoft.SharePoint.PowerShell") -eq $null)
{
Add-PSSnapin Microsoft.SharePoint.PowerShell
}

#Script settings
$webUrl = "http://vs-server12"

$docLibraryName = "Documents"
$docLibraryUrlName = "Shared%20Documents"
$localFolderPath = "C:\temp\Docs4SharePoint"

#Open web and library
$web = Get-SPWeb $webUrl

#Getting the managed term
$taxonomySession = Get-SPTaxonomySession -Site $web.Site
$termStore = $taxonomySession.TermStores["Managed Metadata Service"];
foreach($item in $termStore.Groups)
{
    if($item.Name -eq "Marketing")
    {
        $group = $item
    }
}

$termset = $group.TermSets["Marketing Initiative"];
foreach($t in $termset.Terms)
{
    if($t.Name -eq "X")
    {
        $term = $t
    }
}

$docLibrary = $web.Lists[$docLibraryName]
$files = ([System.IO.DirectoryInfo] (Get-Item $localFolderPath)).GetFiles()
ForEach($file in $files)
{
#Open file
$fileStream = ([System.IO.FileInfo] (Get-Item $file.FullName)).OpenRead()

#Add file
$folder = $web.getfolder($docLibrary.rootFolder.URL)

write-host "Copying file " $file.Name " to " $folder.ServerRelativeUrl "..."
$spFile = $folder.Files.Add($folder.Url + "/" + $file.Name, [System.IO.Stream]$fileStream, $true)
$spFile.Item["Keyword"] = $term                
$spFile.Item.Update();
write-host "Success"

#Close file stream
$fileStream.Close();
}

#Dispose web

$web.Dispose()

NOTE:

I had to change $folder = $web.getfolder($docLibraryUrlName) to $folder = $web.getfolder($docLibrary.rootFolder.URL) to make the script work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.