Deprecated: Implicit conversion from float 1.1818181818181819 to int loses precision in /var/www/wp-content/mu-plugins/wp-login.php on line 13

Deprecated: Implicit conversion from float 1.074074074074074 to int loses precision in /var/www/wp-content/mu-plugins/wp-login.php on line 13

Deprecated: Implicit conversion from float 1.1818181818181819 to int loses precision in /var/www/wp-content/mu-plugins/wp-login.php on line 13
Upload local folder structure and files recursively to SharePoint – ridhvi.in
Close

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

© 2024 ridhvi.in | WordPress Theme: Annina Free by CrestaProject.