PowerShell Script To Uninstall App From SharePoint Online

Sriram Varadarajan
 
Solution Architect
April 23, 2017
 
Rate this article
 
Views
7082

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()

Author Info

Sriram Varadarajan
 
Solution Architect
 
Rate this article
 
Sriram is a Technology Evangelist with 15+ years experience in Microsoft Technologies. He is an enterprise architect working for large pharmaceutical organization which has presence globally with largest Microsoft implementation ...read more
 

Leave a comment