function UploadFileInLibrary
{
[CmdletBinding()]
Param([Parameter(Mandatory=$true,ValueFromPipeline=$true)][string]$webUrl,[Parameter(Mandatory=$true)][string]$ImportLibTitle,[Parameter(Mandatory=$true)][string]$FilePath)
Start-SPAssignment -Global
#Get handle of PLM CSS web object
$spSite = Get-SPSite -Identity $webUrl
$spWeb = $spSite.OpenWeb()
try
{
$spWeb.AllowUnsafeUpdates = $true;
#Get List handle
$List = $spWeb.Lists.TryGetList($ImportLibTitle)
#Get File handle
$FileName = $FilePath.Substring($FilePath.LastIndexOf("\")+1)
$File= Get-ChildItem $FilePath
#Read File Stream
$fileStream = ([System.IO.FileInfo] (Get-Item $File.FullName)).OpenRead()
#Add file
write-host -NoNewLine -f yellow "Copying file " $File.Name " to " $List.RootFolder.ServerRelativeUrl "..."
[Microsoft.SharePoint.SPFile]$spFile = $List.RootFolder.Files.Add($List.RootFolder.Url + "/" + $File.Name, [System.IO.Stream]$fileStream, $true)
write-host -f Green "...Success!"
#Close file stream
$fileStream.Close()
#Update Item
write-host -NoNewLine -f yellow "Added file " $spFile.Name "... to the library " $ImportLibTitle
$spFile.Item.Update()
#Success
write-host -f Green "...Success!"
}
catch
{
write-host -f Red "...Failure!"
$ErrorMessage = $_.Exception.Message
Write-Host $ErrorMessage -BackgroundColor Red
}
finally
{
$spWeb.AllowUnsafeUpdates = $false;
$spWeb.Dispose()
$spSite.Dispose()
Stop-SPAssignment -Global
}
}
#Begin Code
cls
asnp "*sh*" #This will add all SnapIns that contain "sh". In most cases, this is just the Microsoft.SharePoint.PowerShell one.
#Read params passed to the powershell
$WebUrl = $args[0] #example: http://*****/sites/YourSite/
$FilePath = $args[1] #example: C:\Users\User\Desktop\FillerFiles\Example.csv
$ImportLibTitle = $args[2] #example: "Title"
#call Import function
UploadFileInLibrary $WebUrl $ImportLibTitle $FilePath
{
[CmdletBinding()]
Param([Parameter(Mandatory=$true,ValueFromPipeline=$true)][string]$webUrl,[Parameter(Mandatory=$true)][string]$ImportLibTitle,[Parameter(Mandatory=$true)][string]$FilePath)
Start-SPAssignment -Global
#Get handle of PLM CSS web object
$spSite = Get-SPSite -Identity $webUrl
$spWeb = $spSite.OpenWeb()
try
{
$spWeb.AllowUnsafeUpdates = $true;
#Get List handle
$List = $spWeb.Lists.TryGetList($ImportLibTitle)
#Get File handle
$FileName = $FilePath.Substring($FilePath.LastIndexOf("\")+1)
$File= Get-ChildItem $FilePath
#Read File Stream
$fileStream = ([System.IO.FileInfo] (Get-Item $File.FullName)).OpenRead()
#Add file
write-host -NoNewLine -f yellow "Copying file " $File.Name " to " $List.RootFolder.ServerRelativeUrl "..."
[Microsoft.SharePoint.SPFile]$spFile = $List.RootFolder.Files.Add($List.RootFolder.Url + "/" + $File.Name, [System.IO.Stream]$fileStream, $true)
write-host -f Green "...Success!"
#Close file stream
$fileStream.Close()
#Update Item
write-host -NoNewLine -f yellow "Added file " $spFile.Name "... to the library " $ImportLibTitle
$spFile.Item.Update()
#Success
write-host -f Green "...Success!"
}
catch
{
write-host -f Red "...Failure!"
$ErrorMessage = $_.Exception.Message
Write-Host $ErrorMessage -BackgroundColor Red
}
finally
{
$spWeb.AllowUnsafeUpdates = $false;
$spWeb.Dispose()
$spSite.Dispose()
Stop-SPAssignment -Global
}
}
#Begin Code
cls
asnp "*sh*" #This will add all SnapIns that contain "sh". In most cases, this is just the Microsoft.SharePoint.PowerShell one.
#Read params passed to the powershell
$WebUrl = $args[0] #example: http://*****/sites/YourSite/
$FilePath = $args[1] #example: C:\Users\User\Desktop\FillerFiles\Example.csv
$ImportLibTitle = $args[2] #example: "Title"
#call Import function
UploadFileInLibrary $WebUrl $ImportLibTitle $FilePath
No comments:
Post a Comment