Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upload file to SharePoint Document library using powershell

I want to upload the same file to multiple site collections with the same hierarchy in all the site collections. I want to use PowerShell and include auto check-in/check-out functionality.

I have able to upload the file in SharePoint. Below is the code. :

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") > $null

# create the Variable Path and Pass the source folder path
$path = “D:\ABC\DEF\26Nov\”;

# create the Variable destination and pass the URL of the SharePoint List
$destination = "complete URL of File will be mentioned here";

# Store the current user default credentials in the Variable Credentials
$credentials = [System.Net.CredentialCache]::DefaultCredentials;

# Create the object of the Webclient
$webclient = New-Object System.Net.WebClient;

# Pass the user credentials
$webclient.Credentials = $credentials; Get-ChildItem

# “For Each” loop will upload all of the files one by one onto the destination using the UploadFile method
Get-ChildItem $path | ForEach-Object { $webclient.UploadFile($destination + “/” + $_.Name, “PUT”, $_.FullName)};

By this code the file is uploaded but checked out. I want it to be checked in automatically. In case the file is there then first automatically check-out and then check in.

like image 432
Kishan Avatar asked Nov 27 '13 08:11

Kishan


People also ask

How do I bulk upload to SharePoint?

Navigate to the files that you want to bulk upload to your SharePoint account, select all the files and click “Open” in the pop-up window. Then the multiple files will be uploaded to your SharePoint library as soon as possible.


1 Answers

Here is simple script in layman style which is tested and working fine to upload files from your drive to SharePoint document library

http://soreddymanjunath.blogspot.in/2014/07/add-file-to-document-library-using.html

cls

asnp "*sh*"

$url=Read-Host "Enter Site Url" 

$web=Get-SPWeb -Identity $url

if($web)
{
try
{
$list = $web.Lists.TryGetList("Documents")

$files = Get-ChildItem -Path "D:\Manju" -Force -Recurse

    foreach ($file in $files)
    {
      $stream = $file.OpenRead()

      $done= $list.RootFolder.Files.Add($file.Name, $stream, $true)

      Write-Host $done.Name  "Uploaded into the Site" -BackgroundColor Green         

    }
}
catch
{
$ErrorMessage = $_.Exception.Message
Write-Host $ErrorMessage
}
}

else
{
Write-Host "Site Doesn't exist"
}

$list.Update();
like image 142
Soreddy Manjunath Avatar answered Nov 15 '22 05:11

Soreddy Manjunath