#Task performed in this PowerShell script.
#=============================================================
#Replicate Local folder with subfolder and files in SharePoint document library
#=============================================================
#Prerequisite
#Document Library for uploading files should be alresdy created
#=============================================================
#Author : Satish R Gadekar
#Date : 11/13/2014
#=============================================================
Add-PSSnapin "Microsoft.SharePoint.Powershell" -ErrorAction SilentlyContinue
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)
try{$SubFolder.Update()}catch{}
$Fullname = $obj.FullName
SubFolder $Fullname $SubFolder $spDocumentLibrary
}
Else
{
if($obj -ne $null)
{
$fileStream = ([System.IO.FileInfo]$obj).OpenRead()
$FolderObj = $spDocumentLibrary.ParentWeb.GetFolder($SPFolder.ServerRelativeUrl)
$SpFile = $FolderObj.Files.Add($FolderObj.Url + "/"+$obj.Name, $fileStream, $true)
$spItem = $SpFile.Item
}
}
}
}
$Siteurl= Read-Host "Enter web url:"
#Get sp site
$spWeb = Get-SPWeb $siteurl -ErrorAction SilentlyContinue
If($spWeb)
{
$Library=Read-Host "Enter Library name to upload al files:"
#Get the specified library
$spDocumentLibrary = $spWeb.Lists[$Library]
If($spDocumentLibrary)
{
$Path=Read-Host "Enter Folder Path:"
#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. Existing folder will be renamed with Vesrion"
$deleteFolder=Read-Host "Type Y to proceed or Type N to stop"
if($deleteFolder -eq "Y")
{
$todayDate=Get-Date -Format o
$todayDate = $todayDate.Replace(":","")
$todayDate = $todayDate.Replace(" ","")
$todayDate = $todayDate.Replace("-","")
$todayDate = $todayDate.Replace(".","")
$result.Item["FileLeafRef"]="CodeFiles_V"+$todayDate
$result.Item.Update()
Write-Host "Existing folder is renamed to CodeFiles_V"$todayDate
#$result.Delete()
#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-Host "Import Stoped"
}
}
else
{
$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"
}
#=============================================================
#Replicate Local folder with subfolder and files in SharePoint document library
#=============================================================
#Prerequisite
#Document Library for uploading files should be alresdy created
#=============================================================
#Author : Satish R Gadekar
#Date : 11/13/2014
#=============================================================
Add-PSSnapin "Microsoft.SharePoint.Powershell" -ErrorAction SilentlyContinue
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)
try{$SubFolder.Update()}catch{}
$Fullname = $obj.FullName
SubFolder $Fullname $SubFolder $spDocumentLibrary
}
Else
{
if($obj -ne $null)
{
$fileStream = ([System.IO.FileInfo]$obj).OpenRead()
$FolderObj = $spDocumentLibrary.ParentWeb.GetFolder($SPFolder.ServerRelativeUrl)
$SpFile = $FolderObj.Files.Add($FolderObj.Url + "/"+$obj.Name, $fileStream, $true)
$spItem = $SpFile.Item
}
}
}
}
$Siteurl= Read-Host "Enter web url:"
#Get sp site
$spWeb = Get-SPWeb $siteurl -ErrorAction SilentlyContinue
If($spWeb)
{
$Library=Read-Host "Enter Library name to upload al files:"
#Get the specified library
$spDocumentLibrary = $spWeb.Lists[$Library]
If($spDocumentLibrary)
{
$Path=Read-Host "Enter Folder Path:"
#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. Existing folder will be renamed with Vesrion"
$deleteFolder=Read-Host "Type Y to proceed or Type N to stop"
if($deleteFolder -eq "Y")
{
$todayDate=Get-Date -Format o
$todayDate = $todayDate.Replace(":","")
$todayDate = $todayDate.Replace(" ","")
$todayDate = $todayDate.Replace("-","")
$todayDate = $todayDate.Replace(".","")
$result.Item["FileLeafRef"]="CodeFiles_V"+$todayDate
$result.Item.Update()
Write-Host "Existing folder is renamed to CodeFiles_V"$todayDate
#$result.Delete()
#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-Host "Import Stoped"
}
}
else
{
$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"
}
No comments:
Post a Comment