Friday, December 26, 2014

SharePoint 2013 App Deployment PowerShell

In SharePoint 2013 App Development and Deployment we come across situations to automate the Upload and Install of SharePoint 2013 App to Corporate Catalog. Please find below the PowerShell to automate that process.

#-----Function to Load SharePoint PowerShell Environment----#
function LoadSharePointPowerShellEnvironment
{
    write-host "Setting up PowerShell environment for SharePoint..." -foregroundcolor Yellow
    Add-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue
    write-host "SharePoint PowerShell Snapin loaded." -foregroundcolor Green
}
#-----Function to create output log file--#
function CreateLogFile {
param($logFileName)
 New-Item $logFileName -type File
 Set-Content $logFileName "Starting App Upload and Deployment......"
}
function Main($inputAppCatalogSiteCollectionUrl, $inputFolderLocation, $inputSiteCollectionUrl)
{
    try
    {
        $logFileName = [System.String]::Format("{0}{1}_{2}_{3}{4}", $inputFolderLocation + "\","Output_", $dateSuffix, $timeSuffix,".csv")
       
        $sourceApp = ([microsoft.sharepoint.administration.spappsource]::CorporateCatalog)
       
        $inputAPPName = "Test.app"       
        $inputAPPPath = [System.String]::Format("{0}{1}", $inputFolderLocation + "\", $inputAPPName)
        CreateLogFile $logFileName
       
        #Uploading app in app Catalog       
        UploadFile $inputAppCatalogSiteCollectionUrl "Apps for SharePoint" $inputAPPPath          
            
        Write-Host "APP added in App Catalog -" $inputSiteCollectionUrl;
        Write-Host "Import to Destination Site started"  $inputSiteCollectionUrl;
       
        #Importing app
        $spapp =Import-SPAppPackage -Path  $inputAPPPath -Site $inputSiteCollectionUrl -Source $sourceApp    
        Write-Host "Import to Destination Site Completed"  $inputSiteCollectionUrl;            
        Write-Host "Installation to Destination Site Started"  $inputSiteCollectionUrl;
        #Installing app
        $instance = Install-SPApp -Web $inputSiteCollectionUrl -Identity $spapp
        Write-Host "Installation to Destination Site Completed"  $inputSiteCollectionUrl;
}
catch
{
    Write-Host "Error Occured: $_.Exception.Message"
    Add-Content $logFileName "Error in Method Main."
    Add-Content $logFileName "Error Details: $_.Exception.Message"
}
}

function UploadFile($WebURL, $DocLibName, $FilePath)
{
    try
    {
        $Web = Get-SPWeb $WebURL
        if($Web -ne $null)
        {               
            $List = $Web.GetFolder($DocLibName)
           
            $Files = $List.Files
           
            $FileName = $FilePath.Substring($FilePath.LastIndexOf("\")+1)
           
            $folder = $List.RootFolder
            $SPFilePath = ($folder.URL + "/" + $FileName)
           
            $spFile = $web.GetFile($SPFilePath)
           
            if ($spFile -ne $null)
            {
                Write-Host "Already Exist in App Catalog-" $inputSiteCollectionUrl;
                Write-Host "OverWriting Existing file in App Catalog-" $inputSiteCollectionUrl;
            }
           
            $File= Get-ChildItem $FilePath
            $Files.Add("AppCatalog/" + $FileName, $File.OpenRead(), $true)
        }
        else
        {
            Write-Host "Web not found for URL : -" $inputSiteCollectionUrl;
            write-host "Method: Main, SPWeb Object is null." -foregroundcolor "Yellow"
            Add-Content $logFileName "Method: Main, SPWeb Object is null."
        }
    }
    catch
    {
        Write-Host "Error Occured: $_.Exception.Message"
        Add-Content $logFileName "Error in Method Main."
        Add-Content $logFileName "Error Details: $_.Exception.Message"
    }
    finally
    {
        if($spWeb -ne $null)
        {
            $spWeb.Dispose()
        }
    }
}
# Calling LoadSharePointPowerShellEnvironment Method
LoadSharePointPowerShellEnvironment
Main "http://AppCatalog.qa" "C:\Solutions" "http://abc-pc1873-1/sites/abc/"

Thanks, Prabhat

Security in SharePoint Apps