PowerShell Script To Uninstall App From SharePoint Online


Sriram Varadarajan
Enterprise Architect
Published On :   23 Apr 2017
Visit Count
Today :  4    Total :   1298
Plan, Migrate, Secure, Report
SharePoint & Office 365 Tool. Simple & Easy to Use. 15-Day Trial!

Sharegate: Kick-Ass Tool
Think Your SharePoint & Office 365 Are Secure ? Find Out Now!


In the recent past we added some third party apps from SharePoint Store to SharePoint Online App catalog site. But they where not available for users on SPO site's under "Apps You can Add" section.
So, we  tried uninstalling the APP and got the following error messages

The Url '/sites/TSTAppCatalogue/APP NAME' cannot be resolved to a web. The web may be temporarily unavailable. And received the following error too.

An expected exception was thrown while running task 'GetMyApps'. Microsoft.SharePoint.SPException: Error in the application.     at Microsoft.SharePoint.ApplicationPages.StorefrontBase.TaskGetMyApps()

We suspect that the app was not properly installed because of the presence of a stale entry and we are looking for possibilities to completely remove the app from appcatalog and then re-install it again.

Manual un-installing wasn’t fixing the issue. But removing it Via script work.

Uninstallation Script

 [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")
 [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")
 
 
 Function Get-ClientContext([string]$Url,[string]$UserName,[string]$Password)
 {
     $SecurePassword = $Password | ConvertTo-SecureString -AsPlainText -Force
     $context = New-Object Microsoft.SharePoint.Client.ClientContext($Url)
     $context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $SecurePassword)
     return $context
 }
 
 
 Function Uninstall-AppInstance([Microsoft.SharePoint.Client.ClientContext]$Context,[Guid]$AppInstanceId)
 {
     $appInst = $Context.Web.GetAppInstanceById($AppInstanceId)
     $appInst.Uninstall()
     $context.ExecuteQuery()
  }
 
 
 $UserName = "USER ID"
 $Password = Read-Host -AsSecureString "Enter the password"    
 $Url = " appcatalog URL"
 $AppInstanceid = New-Object Guid("xxxx-xx-xx-xx-xxxx")  #specify App Instance Id here
 
 $creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $Password)
 
 $context = New-Object Microsoft.SharePoint.Client.ClientContext($Url)  
 $context.credentials = $creds  
 
 Uninstall-AppInstance -Context $context -AppInstanceId $AppInstanceid
 $context.Dispose()
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Categories

KWizCom Forms App