2

How to upload the file into the document library and insert/update the metadata columns.I am having 3 people picker data types/user group columns and one choice field and 2 single line of text columns. I have written the PowerShell to upload the document in the document library but how to get the item which is inserted and update the metadata. Do I need to use the getitembyid()? Also when I ran the script it inserted successfully and its checked-out.

         [System.Reflection.Assembly]::LoadWithPartialName 
        ("Microsoft.SharePoint")
        if((Get-PSSnapin | Where {$_.Name -
        eq "Microsoft.SharePoint.PowerShell"}) -eq $null) {
            Add-PSSnapin Microsoft.SharePoint.PowerShell;
              }

           #Site Collection where you want to upload files
          $siteCollUrl 
            = "http://srvr1:123/sites/R-Engineering/"
          #Document Library where you want to upload files
         $libraryName = "KG"
         #Physical/Network location of files
          $reportFilesLocation  = "E:\Maddy\Scripts\FileUpload"

        $spSourceWeb = Get-SPWeb $siteCollUrl;
        $spSourceList = $spSourceWeb.Lists[$libraryName];

            if($spSourceList -eq $null)
            {
           Write-Host "The Library $libraryName could not be found."
            return;
            }

   $files = ([System.IO.DirectoryInfo] (Get-Item 
    $reportFilesLocation)).GetFiles()
  foreach($file in $files)
  {
    #Open file
   $fileStream = ([System.IO.FileInfo] (Get-Item $file.FullName)).OpenRead()

   #Add file
   $folder =  $spSourceWeb.getfolder($libraryName)

   Write-Host "Copying file $file to $libraryName..."
   $spFile = $folder.Files.Add($folder.Url + "/" + $file.Name,   
   [System.IO.Stream]$fileStream, $true)

   #Close file stream
  $fileStream.Close();
 }
$spSourceWeb.dispose();
Write-Host "Files have been uploaded to $libraryName."
5

Right after you add the file:

$item = $spFile.Item;
$item["TextOrChoiceColumnInternalName"] = "New Value"
$spUser = Get-SPUser -Identity "domain\username" -Web $spSourceWeb;
$item["UserFieldInternalName"] = $spUser;
$item.SystemUpdate($false);

Use SystemUpdate($false) if you don't want to increase the version of the item..

To check in a file use:

$spFile.CheckIn("Checked In By Administrator")

To publish a file:

$spFile.Publish("Automatically published by Powershell")

To approve a file:

$spFile.Approve("Automatically approved by by Powershell");
  • Thanks. Its checked out now.How can I check in this item & publish through PowerShell. – userAZLogicApps May 4 '15 at 5:56
  • Edited my answer. – Paul Strupeikis May 5 '15 at 7:53

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.