Power Shell
Upload local folder structure and files recursively to SharePoint

Upload local folder structure and files recursively to SharePoint

The below Power Shell command script enables us to upload the local folder structure in SharePoint including Sub folders and relative files.

Function Import-OSCFolder
{
[CmdletBinding()]
param 
(
[Parameter(Mandatory=$true,Position=0)]
[string]$Siteurl,
[Parameter(Mandatory=$true,Position=1)]
[string]$Library,
[Parameter(Mandatory=$true,Position=2)]
[string]$Path
)

if ((Get-PSSnapin | Where-Object {$_.Name -eq 'Microsoft.SharePoint.Powershell'})-eq $null) 
    {
    Add-PSSnapin "Microsoft.SharePoint.Powershell"
    }
#Get sp site
$spWeb = Get-SPWeb $siteurl -ErrorAction SilentlyContinue
If($spWeb)
{
#Get the specified library
$spDocumentLibrary = $spWeb.Lists[$Library]  
If($spDocumentLibrary)
{
#verify if the path is valid
If(Test-Path -Path $Path)
{
#Get the folder
$Fol = Get-Item -Path $Path 
If($Fol.PSIsContainer)
{
$result = $spDocumentLibrary.ParentWeb.GetFolder($spDocumentLibrary.RootFolder.ServerRelativeUrl +"/"+ $Fol.Name )
If($result.Exists -eq "True")
{
Write-Warning "There is a folder existing on site $siteUrl."
#$SPFol = $spDocumentLibrary.ParentWeb.GetFolder($spDocumentLibrary.RootFolder.ServerRelativeUrl + "/" +$_.Name)
#SubFolder  $path $SPFol $spDocumentLibrary
}
Else
{
#Import the folder to site library.
$SPFol = $spDocumentLibrary.AddItem("",[Microsoft.SharePoint.SPFileSystemObjectType]::Folder,$Fol.Name) 
$SPFol.Update()
SubFolder  $path $SPFol $spDocumentLibrary 

Write-Host "Import '$Path' folder to site $siteurl successfully."
}
}
Else
{
Write-Error "The object is not a folder."
}
}
Else 
{
Write-Error "Invalid path,try again."
}
}
Else
{
Write-Warning "There is no library named $Library on site $siteurl."
}
}
Else
{
Write-Error "Not find the specified site $siteurl"
}
}

Function SubFolder($Folder,$SPFol,$spDocumentLibrary)
{
#Import the folder and subfolders to site library.
$SPFolder = $spDocumentLibrary.ParentWeb.GetFolder($SPFol.Folder.ServerRelativeUrl)
$Objects = Get-ChildItem -Path $Folder 
Foreach($obj in $Objects)
{
If($obj.PSIsContainer)
{
$SubFolder = $spDocumentLibrary.AddItem($SPFolder.ServerRelativeUrl,[Microsoft.SharePoint.SPFileSystemObjectType]::Folder,$obj.Name)
$SubFolder.Update()
$Fullname = $obj.FullName
SubFolder  $Fullname $SubFolder $spDocumentLibrary
}
Else
{
$fileStream = ([System.IO.FileInfo]$obj).OpenRead() 
$contents = new-object byte[] $fileStream.Length 
$FolderObj = $spDocumentLibrary.ParentWeb.GetFolder($SPFolder.ServerRelativeUrl)
$SpFile = $FolderObj.Files.Add($FolderObj.Url + "/"+$obj.Name, $contents, $true)
$spItem = $SpFile.Item
            $SpFile.CheckIn("Checked In By Administrator")            
}
}
} 

Function BeginUpload($DirectoryFullName)
{
    $siteCollUrl = "http://[webname]/sites/[sitecollectionname]"    
    $libraryName = "Style Library"
    Get-ChildItem $DirectoryFullName  | % {
                if ($_.Attributes -eq "Directory")
                    {           
                        Import-OSCFolder -siteurl $siteCollUrl -Library $libraryName -path  $_.FullName
                    }
            }
} 

$filesLocation  = "C:\Library\"
cls  

BeginUpload -DirectoryFullName $filesLocation

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.