Hi All,
Again got a chance to play around Power Shell.
My requirement was to copy Files and Folders(including files) to a SharePoint Document Library.
Snippet File Name : CopyShellScript.ps1
Location of Snippet in C: (C drive )
Parameters:
1. webUrl - give your web url here
2. docLibraryName - Name of Document Library (e.g. Shared Documents)
3. docLibraryUrlName - URL name of Document Library(e.g. Shared%20Documents)
4. localFolderDataPath - Location of the folder from where you want to copy(local machine path.)
Script Execution:
PS C:\> .\CopyShellScript.ps1 -webUrl "http://yourURL.local" -docLibraryName "TargetLib" -docLibraryUrlName "TargetLib" -localFolderDataPath "C:\temp"
Below is the complete script. Do work around on $localFolderPath if you want to go for Nth level depth. Iam showing here till nesting of folder level 1.
Param(
[string]$webUrl,
[string]$docLibraryName,
[string]$docLibraryUrlName,
[string]$localFolderDataPath
)
Add-PSSnapin Microsoft.SharePoint.PowerShell
#Script settings
#Open web and library
$web = Get-SPWeb $webUrl
$docLibrary = $web.Lists[$docLibraryName]
$files = ([System.IO.DirectoryInfo] (Get-Item $localFolderDataPath)).GetFiles()
copyfileInFolder $files
$Items = Get-ChildItem -Path $localFolderDataPath
# enumerate the items array
foreach ($item in $Items)
{
# if the item is a directory, then process it.
if ($item.Attributes -eq "Directory")
{
$create_folder = $docLibrary.AddItem("", [Microsoft.SharePoint.SPFileSystemObjectType]::Folder,$item)
$create_folder.Update();
Write-Host $item.Name "is created"
$localFolderPath = $localFolderDataPath+"\"+$item.Name
$files = ([System.IO.DirectoryInfo] (Get-Item $localFolderPath)).GetFiles()
copyfileInFolder $files $item
}
}
function copyfileInFolder
{
$files=$args[0];
$item=$args[1];
ForEach($file in $files)
{
#Open file
$fileStream = ([System.IO.FileInfo] (Get-Item $file.FullName)).OpenRead()
#Add file
$folder = $web.getfolder($docLibraryUrlName+"/"+$item.Name)
$spFile = $folder.Files.Add($folder.Url + "/" + $file.Name, [System.IO.Stream]$fileStream, $true)
#Close file stream
$fileStream.Close();
}
}
#Dispose web
$web.Dispose()
Again got a chance to play around Power Shell.
My requirement was to copy Files and Folders(including files) to a SharePoint Document Library.
Snippet File Name : CopyShellScript.ps1
Location of Snippet in C: (C drive )
Parameters:
1. webUrl - give your web url here
2. docLibraryName - Name of Document Library (e.g. Shared Documents)
3. docLibraryUrlName - URL name of Document Library(e.g. Shared%20Documents)
4. localFolderDataPath - Location of the folder from where you want to copy(local machine path.)
Script Execution:
PS C:\> .\CopyShellScript.ps1 -webUrl "http://yourURL.local" -docLibraryName "TargetLib" -docLibraryUrlName "TargetLib" -localFolderDataPath "C:\temp"
Below is the complete script. Do work around on $localFolderPath if you want to go for Nth level depth. Iam showing here till nesting of folder level 1.
Param(
[string]$webUrl,
[string]$docLibraryName,
[string]$docLibraryUrlName,
[string]$localFolderDataPath
)
Add-PSSnapin Microsoft.SharePoint.PowerShell
#Script settings
#Open web and library
$web = Get-SPWeb $webUrl
$docLibrary = $web.Lists[$docLibraryName]
$files = ([System.IO.DirectoryInfo] (Get-Item $localFolderDataPath)).GetFiles()
copyfileInFolder $files
$Items = Get-ChildItem -Path $localFolderDataPath
# enumerate the items array
foreach ($item in $Items)
{
# if the item is a directory, then process it.
if ($item.Attributes -eq "Directory")
{
$create_folder = $docLibrary.AddItem("", [Microsoft.SharePoint.SPFileSystemObjectType]::Folder,$item)
$create_folder.Update();
Write-Host $item.Name "is created"
$localFolderPath = $localFolderDataPath+"\"+$item.Name
$files = ([System.IO.DirectoryInfo] (Get-Item $localFolderPath)).GetFiles()
copyfileInFolder $files $item
}
}
function copyfileInFolder
{
$files=$args[0];
$item=$args[1];
ForEach($file in $files)
{
#Open file
$fileStream = ([System.IO.FileInfo] (Get-Item $file.FullName)).OpenRead()
#Add file
$folder = $web.getfolder($docLibraryUrlName+"/"+$item.Name)
$spFile = $folder.Files.Add($folder.Url + "/" + $file.Name, [System.IO.Stream]$fileStream, $true)
#Close file stream
$fileStream.Close();
}
}
#Dispose web
$web.Dispose()
No comments:
Post a Comment