About Me

Having 12 years experience in Microsoft technologies.Since more than 7 years working in SharePoint technologies. Expert in providing consultation for SharePoint projects. Hands on with development and administration.

Monday, 25 August 2014

Powershell Script - Upload App to AppCatalog store in SharePoint 2013

The following powershell script will upload .App file to App catalog and accept following input parameters

1. WebUrl - App Catalog site url
2.AppCatalogName - 'Apps for Sharepoint'
3.AppName - Your .app name

Note: The .app should be in the same directory where you are running the script.

Steps involved
1. Save following script as psUploadAppToCatalog.ps1


if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null) {
    Add-PSSnapin "Microsoft.SharePoint.PowerShell";
function Get-ScriptDirectory
 $Invocation = (Get-Variable MyInvocation -Scope 1).Value
 Split-Path $Invocation.MyCommand.Path

#Get Current Physical Path
$currentPhysicalPath = Get-ScriptDirectory
$logfile=$currentPhysicalPath + "\log.log"
Start-Transcript $logfile        

    Start-SPAssignment -Global             
    $spWeb = Get-SPWeb -Identity $WebUrl            
    $spWeb.AllowUnsafeUpdates = $true;           
    $List = $spWeb.Lists[$AppCatalogName]           
    $folder = $List.RootFolder
    $FilePath = $currentPhysicalPath + "\" + $AppName    
    #$FileName = $FilePath.Substring($FilePath.LastIndexOf("\")+1)
    $File= Get-ChildItem $FilePath           
    [Microsoft.SharePoint.SPFile]$spFile = $spWeb.GetFile("/" + $folder.Url + "/" + $File.Name)           
    $flagConfirm = 'y'           

    #if($spFile.Exists -eq $true)           
     #   $flagConfirm = Read-Host "File $AppName already exists in library $DocLibName, do you want to upload a new version(y/n)?"            

    #if ($flagConfirm -eq 'y' -or $flagConfirm -eq 'Y')  
    if ($flagConfirm -eq 'y')       
        $fileStream = ([System.IO.FileInfo] (Get-Item $File.FullName)).OpenRead()           
        #Add file           
        write-host -NoNewLine -f yellow "Copying file " $File.Name " to " $folder.ServerRelativeUrl "..."           
        [Microsoft.SharePoint.SPFile]$spFile = $folder.Files.Add($folder.Url + "/" + $File.Name, [System.IO.Stream]$fileStream, $true)           
        write-host -f Green "...Success!"           
        #Close file stream           
        write-host -f Green "Successssfully uploaded the app -" $AppName " to app catalog" -ForegroundColor Green     
$spWeb.AllowUnsafeUpdates = $false;

        $Host.UI.RawUI.WindowTitle = " -- Error --"
        Write-Host -ForegroundColor Red $_.ToString()
        Write-Host -ForegroundColor Red $_.Exception.ToString()

Stop-SPAssignment -Global
Step2: Create batch file to run the script. Save file in .bat format.

cd /d %~dp0
powershell -file ./psUploadAppToCatalog.ps1 -WebUrl "https://catalog.ec.com" -AppCatalogName "Apps for SharePoint" -AppName "xxxxx.app"

Happy powershell coding... please feel free to comment...

No comments:

Post a Comment