Wednesday 11 March 2015

Powershell for uploading documents in Document library

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

#Script settings

$Url = "http://sp2013:5559/"

$docLibraryName = "Documents"
$docLibraryUrlName = "Shared Documents"

$floorLayoutImagesPath = "C:\Images"

#Open web and library

$web = Get-SPWeb $Url

$docLibrary = $web.Lists[$docLibraryName]

$files = ([System.IO.DirectoryInfo] (Get-Item $floorLayoutImagesPath)).GetFiles()

ForEach($file in $files)
{

    #Open file
    try
    {
    $fileStream = ([System.IO.FileInfo] (Get-Item $file.FullName)).OpenRead()

    #Add file
    $folder =  $web.getfolder($docLibraryUrlName)

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

    #Close file stream
    $fileStream.Close();
    }
    catch
    {
 
        continue;
     
    }
}

#Dispose web

$web.Dispose()

No comments:

Post a Comment