Wednesday 8 June 2016

Powershell Script For Apps

Automatic App versioning and packaging

The PowerShell script which will automatically update the app version and upload it to app catalogue.

We just need to set the following

    1)      App Catalogue site url
    2)      Username
    3)      Password
    4)      SharePoint App Project path(.csproj)

Below is the sample ,

$Password = "xxx@123"
$ProjectPath="C:\Users\spadmin\Documents\visual studio 2013\Projects\SampleApp\SampleApp\SampleApp.csproj"

After configuring the above details it’s all set to go, you only need to make the code changes ,Save it and run this script .it automatically updates the App version, Packages it and upload it to app catalogue site. After running this script you just need to update the app in your site.


Add below script as PS1 file: 

#Specify tenant admin and URL
$User = "xxx@xxx.onmicrosoft.com"
$SiteURL = "https://xxx.sharepoint.com/sites/AppCatalogue"

#Add references to SharePoint client assemblies and authenticate to Office 365 site - required for CSOM
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.UserProfiles.dll"

$Password = "xxx@123"
$Password = ConvertTo-SecureString $Password -AsPlainText -Force
$ProjectPath="C:\Users\spadmin\Documents\visual studio 2013\Projects\SampleApp\SampleApp\SampleApp.csproj"

function Get-SPAppPackageMetadata {
   [CmdLetBinding()]
   param([Parameter(Mandatory=$true)][string]$AppProjectPath)
   $AppManifestPath=$AppProjectPath.Replace($AppProjectPath.Split("\")[-1],"AppManifest.xml") 
   $manifest = @{ "Name" = "AppManifest.xml" }   
   $manifest.Xml = [xml](get-content $AppManifestPath)   
   $AppCurrentVersion= $manifest.Xml.App.Version
   $SplittedVersion=$AppCurrentVersion.split(".")
   $UpdatedVersion = [convert]::ToInt32($AppCurrentVersion.split(".")[3])+1
   $UpdatedVersion= $SplittedVersion[0]+"."+$SplittedVersion[1]+"."+$SplittedVersion[2]+"."+$UpdatedVersion
   $manifest.Xml.App.Version=$UpdatedVersion
   $manifest.Xml.Save($AppManifestPath)
   $msbuild = "C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe"  
&  $msbuild /p:VisualStudioVersion=12.0 /t:Package $AppProjectPath
   $FileToDelete=$AppProjectPath.Replace($AppProjectPath.Split("\")[-1],"")+"bin\debug\"+($AppProjectPath.Split("\")[-1]).Replace(".csproj",".wsp")
   Remove-Item $FileToDelete
#the folder which contains the .app files ready to be uploaded
$Folder = $AppProjectPath.Replace($AppProjectPath.Split("\")[-1],"")+"bin\debug\app.publish\$UpdatedVersion"
Write-Host $folder

#"App for SharePoint" library in App Catalog site
$DocLibName = "Apps for SharePoint"

$Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User,$Password)

#Bind to Site Collection
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Context.Credentials = $Creds

#Retrieve list
$List = $Context.Web.Lists.GetByTitle($DocLibName)
$Context.Load($List)
$Context.ExecuteQuery()

#Upload file
Foreach ($File in (dir $Folder -File))
{
    $FileStream = New-Object IO.FileStream($File.FullName,[System.IO.FileMode]::Open)
    $FileCreationInfo = New-Object Microsoft.SharePoint.Client.FileCreationInformation
    $FileCreationInfo.Overwrite = $true
    $FileCreationInfo.ContentStream = $FileStream
    $FileCreationInfo.URL = $File
    $Upload = $List.RootFolder.Files.Add($FileCreationInfo)
    $Context.Load($Upload)
    $Context.ExecuteQuery()
}
}
Get-SPAppPackageMetadata -AppProjectPath $ProjectPath

No comments:

Post a Comment