There could be sometime need to deploy your artifacts onto the SharePoint document library through power shell scripts in case of deployment onto different servers. Power shell scripts provide better maintenance of deployment mechanism.
In this post we will talk about the deployment of files onto Site Assets library using power shell script. Here we are going to take all the documents present within the folder Site Assests which has been given in the LocalPath attribute of the configuration XML.
Here we have made the deployment configurable through the artifact.xml. Essentially all the folders which needs to be deployed on to document library on the sharePoint site can be used in the xml file.
Hence the root level of the XML is Configuration and it takes an element for SiteUrl.
SiteUrl - The url for the SharePoint site to which the deployment needs to be done.
Library :
Name - The document library name onto which the documents needs to be doployed.
IsSiteAsset - If the document needs to be deployed onto SiteAssets library or not.
LocalPath - The folder structure from which all the files will be taken.
URL - The URL of the document library.
The Artifacts xml will look something like below -
<?xml version="1.0" encoding="utf-8"?>
<Configuration>
<FileDeployment>Files to be uploaded onto Site</FileDeployment>
<SiteUrl>http://siteurl/</SiteUrl>
<Libraries>
<Library Name="SiteAssets" IsSiteAsset="true" LocalPath="Site Assets" URL="SiteAssets"/>
</Libraries>
</Configuration>
In this post we will talk about the deployment of files onto Site Assets library using power shell script. Here we are going to take all the documents present within the folder Site Assests which has been given in the LocalPath attribute of the configuration XML.
Here we have made the deployment configurable through the artifact.xml. Essentially all the folders which needs to be deployed on to document library on the sharePoint site can be used in the xml file.
Hence the root level of the XML is Configuration and it takes an element for SiteUrl.
SiteUrl - The url for the SharePoint site to which the deployment needs to be done.
Library :
Name - The document library name onto which the documents needs to be doployed.
IsSiteAsset - If the document needs to be deployed onto SiteAssets library or not.
LocalPath - The folder structure from which all the files will be taken.
URL - The URL of the document library.
The Artifacts xml will look something like below -
<?xml version="1.0" encoding="utf-8"?>
<Configuration>
<FileDeployment>Files to be uploaded onto Site</FileDeployment>
<SiteUrl>http://siteurl/</SiteUrl>
<Libraries>
<Library Name="SiteAssets" IsSiteAsset="true" LocalPath="Site Assets" URL="SiteAssets"/>
</Libraries>
</Configuration>
The corresponding Powershell script which will perform the deployment is given below -
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
$rootScriptPath = Split-Path -parent $MyInvocation.MyCommand.Definition
[xml]$configXML = Get-Content ($rootScriptPath + "\ArtifactDeploy.xml")
#Site Collection where you want to upload files
$siteUrl = $configXML.Configuration.SiteUrl
try {
[system.reflection.assembly]::LoadWithPartialName("Microsoft.Sharepoint")
$web = Get-SPWeb $siteUrl
$FilesLocation = $configXML.SelectNodes("//Libraries/Library")
foreach($location in $FilesLocation) {
$files = ([System.IO.DirectoryInfo] (Get-Item ($rootScriptPath + "/" +$location.LocalPath))).GetFiles()
foreach($file in $files){
#Open file
$stream = $file.OpenRead()
#Add file
if($location.IsSiteAsset -eq "true"){
$Assetlist = $web.Lists[$location.URL]
if($Assetlist -ne $null){
$fileCollection = $Assetlist.RootFolder.Files
if($fileCollection -ne $null){
$uploaded = $fileCollection.Add($file.Name, $stream, $TRUE)
}
}
else{
Write-Host -ForegroundColor "Site Assets library specified at " $web.Url "/" $location.URL " doesn't exist."
break
}
}
else {
$folder = $web.GetFolder($location.URL);
$uploaded = $folder.Files.Add($folder.Url + "/" + $file.Name, $stream, $true)
}
#Close file stream
$stream.Close();
if($uploaded -ne $null)
{
"Uploaded " + $file.Name
Write-Host $file have been uploaded to $location.Name
}
}
}
Hope this helps and enjoy doing your deployment.
No comments:
Post a Comment