Monday, 4 July 2016

File Upload from Local Drive to Sharepoint Document Library

<#
Purpose of this script:
-----------------------
To upload documents from local drive to the sharepoint document library. Upload process done for On-Premises and also for Office365 environment.

Run Process
-----------
Open this FileUpload.ps1 file --> Hit f5 button --> Ask On-Premises (or) Office365 --> Answer the following questions --> AtLast it shows uploaded file names in the output screen.

No Need of CSV file.

#>


######################### Add SharePoint PowerShell Snapin ###############################################
 
 if ( (Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null )
 {
 
 Add-PSSnapin Microsoft.SharePoint.Powershell
 
 }
 
########################### End of Add SharePoint PowerShell Snapin ##################################
 
 $question = Read-Host "Are you proceed with On-Premises or Office365? If On-Premises Type ON Else Type OFF for Office365"


 If (($question -eq "ON") -or ($question -eq "on") -or ($question -eq "On") -or ($question -eq "oN"))
{
#Script for On-Premises

$webUrl = Read-Host -Prompt "Please enter the Site URL"

$docLibraryName = Read-Host -Prompt "Please enter the Document Library Name"

$docLibraryUrlName = Read-Host -Prompt "Please enter the Document Library URL Name"

$localFolderPath = Read-Host -Prompt "Please enter the Folder Path"
 try
 {

#Open web and library

$web = Get-SPWeb $webUrl

$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($docLibraryUrlName)

    Write-Host "Uploading file.." -ForegroundColor Green

    $spFile = $folder.Files.Add($folder.Url + "/" + $file.Name, [System.IO.Stream]$fileStream, $true)

    Write-Host $file "Uploaded Successfully" -ForegroundColor Green

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

#Dispose web

$web.Dispose()
}
catch
{
}

 }

 Elseif (($question -eq "OFF") -or ($question -eq "off") -or ($question -eq "Off") -or ($question -eq "oFF"))
{

#Specify tenant admin and site URL
$User = Read-Host -Prompt "Please enter your Login Name"
$Password = Read-Host -Prompt "Please enter your password" -AsSecureString
$SiteURL = Read-Host -Prompt "Please enter the Site URL"
$Folder=Read-Host -Prompt "Please enter the Folder Path"
$DocLibName = Read-Host -Prompt "Please enter the Document Library Name"

#Add references to SharePoint client assemblies and authenticate to Office 365 site - required for CSOM
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

try
{
#Bind to site collection
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User,$Password)
$Context.Credentials = $Creds

#Retrieve list
$List = $Context.Web.Lists.GetByTitle($DocLibName)
$Context.Load($List)
$Context.ExecuteQuery()
}
catch
{
}
try
{
#Upload file
Foreach ($File in (dir $Folder -File))
{

 Write-Host "Uploading file.." -ForegroundColor Green
$FileStream = New-Object IO.FileStream($File.FullName,[System.IO.FileMode]::Open)
$FileCreationInfo = New-Object Microsoft.SharePoint.Client.FileCreationInformation
$FileCreationInfo.Overwrite = $true
$FileCreationInfo.ContentStream = $FileStream
$FileCreationInfo.URL = $File
$Upload = $List.RootFolder.Files.Add($FileCreationInfo)
$Context.Load($Upload)
$Context.ExecuteQuery()
 Write-Host $File "Uploaded Successfully" -ForegroundColor Green
}

$FileStream.Close();
}
catch
{
}
}
Else
{
    Write-Host -BackgroundColor White -ForegroundColor Red "INVALID ENTRY! Please try again."
    Break
    #Exit
}

No comments:

Post a Comment