2

I am installing apps using powershell script and I want my script as rerunnable.

try
{
    $web.GetAppInstanceById()
    $appIoStream = New-Object IO.FileStream($AppFilePath,[System.IO.FileMode]::Open)
    $appInstance = $web.LoadAndInstallApp($appIoStream) | Out-Null
    $context.ExecuteQuery()
    Write-Host $appInstance.Id
}
catch
{
    $appIoStream.Dispose();
    Write-Host "Unable to Install App -" $AppName ": $_.Exception.Message" -foregroundcolor black -backgroundcolor Red
    return
}

Using this I can able to achieve the apps to be installed in the site collection.But when I am again running the powershell script I got an error that "an instance of this app is already exists at specified location". How can I handle this?

  • Can please update your question with requirements around what your really need to achive? – Sergei Sergeev Dec 2 '16 at 11:59
  • Sure...I will update – Kumaraguru Dec 2 '16 at 11:59
  • Hi @kai..Updated the question – Kumaraguru Dec 2 '16 at 12:04
  • Thanks for an update! Other users will have a better understanding of your issue. – Sergei Sergeev Dec 2 '16 at 12:07
  • Nice @Kumaraguru, after deleting my app in my dev site, I had to remove it from the recycle bin. Works like a charm now! You da man! – Eric S. Jun 15 '17 at 19:34
1

For that purpose I would recommend PnP-PowerShell module.

This module already contains a lot of helpful commands for managing SharePoint, including apps.

For example take a look at the apps cmdlets:

  • Uninstall‑PnPAppInstance
  • Get-PnPAppInstance
  • Import-PnPAppPackage

With help of PnP you can solve your issue in a following way:

$app = Get-PnPAppInstance | ? {$_.Title -eq "<your app title here>"}
if($app -ne $null){
  Uninstall‑PnPAppInstance -Identity $app
}    
Import-PnPAppPackage -Path c:\files\my_app_file.app 

Only 5 lines of code.

UPD
Before running the script you need to connect to SharePoint using Connect-PnPOnline cmdlet. It asks for credentials, if you want to store credentials in your script (not or good approach and bad practice, but ok for development), you can instantiate a new site with PnP-Powershell using below code:

$username = "user@organization.onmicrosoft.com"
$password = "pass"
$secstr = New-Object -TypeName System.Security.SecureString
$password.ToCharArray() | ForEach-Object {$secstr.AppendChar($_)}
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $secstr

Connect-PnPOnline -Url https://yoursite.sharepoint.com/sites/dev -Credentials $cred
  • Thank You Man...It is working when deploying one app....But I have another problem when deploying second app..Error:(a different version of this app is already installed with the same version number).Can you help me! – Kumaraguru Dec 2 '16 at 14:22
  • For your second app, did you change ProductId param? – Sergei Sergeev Dec 2 '16 at 14:25
  • No..But i am already deployed the app with different version using Visual Studio in same site collection.Is that cause problem? If so, how can I delete that? – Kumaraguru Dec 2 '16 at 14:28
  • Yes, that might be an issue. You can delete it using UI, from Site Content, then from recycle bin. – Sergei Sergeev Dec 2 '16 at 14:29
  • I am deleted in both site contents and recycle bin...But still causing problem – Kumaraguru Dec 2 '16 at 14:30
0

You can try below options:

  1. Change the ProductId in your appmanifest.xml in your VS project.
  2. You need to remove the app from apps catalog or from site content and then upload it again.
  3. Remove the app reference from recycle bin of SharePoint site.

After that you can run your powershell command and verify.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.