Aug 23, 2015

How to get all the installed apps in a sharepoint online site programatically

Hello guys,

Here I am writing a small post on how to get the apps from a SharePoint Online site. The property "Status" of the AppInstance class tells us the app's status like whether it is installed/uninstalling/disabled etc..

Here I took a console app with managed CSOM code.

  1.        string siteUrl = "https://msplab.sharepoint.com/sites/SPOLPUB/";  
  2.        string username = "purna@msplab.onmicrosoft.com";  
  3.        string password = "mspcloud"; 
  4.        // Ref: Microsoft.SharePoint.Client
  5.        ClientContext ctx = new ClientContext(siteUrl);  
  6.       // Ref :System.Security
  7.        SecureString encryptedSecureString = new SecureString();
  8.        password.ToList().ForEach(encryptedSecureString.AppendChar);  
  9.        ctx.Credentials = new SharePointOnlineCredentials(username, encryptedSecureString); 
  10.       // Ref: Microsoft.SharePoint.Client        
  11.        Site site = ctx.Site;  
  12.        ctx.Load(site);  
  13.        ctx.ExecuteQuery();     
  14.        Console.WriteLine("Successfully Connected to Site "+site.Url);  
  15.        Console.WriteLine("Loading Web.. ");  
  16.        Web web = ctx.Web;  
  17.        ctx.Load(web, w => w.Url);  
  18.        ctx.ExecuteQuery();     
  19.        Console.WriteLine("Web Loaded.." + web.Url);  
  20.        Console.WriteLine("Getting AppInstance Collection ...");        
  21.        //CRC is from CC, so typecasting from cc         
  22.        ClientRuntimeContext ctxRuntime = (ClientContext)web.Context;
  23.        //Getting appinstances from appcatalog with webcontext  
  24.        ClientObjectCollection appInstanceCollection =AppCatalog.GetAppInstances(ctxRuntime, web);
  25.        ctxRuntime.Load(appInstanceCollection);  
  26.        ctxRuntime.ExecuteQuery();  
  27.    
  28.        Console.WriteLine("Got AppInstance Collection...");        
  29.        foreach (AppInstance appinstance in appInstanceCollection)  
  30.        {  
  31.          ctxRuntime.Load(appinstance);  
  32.          ctxRuntime.ExecuteQuery();  
  33.          Console.WriteLine("==================Retrieving App Details =================");  
  34.          Console.WriteLine("App Title: " + appinstance.Title);  
  35.          Console.WriteLine("Principle Id: " + appinstance.AppPrincipalId);  
  36.          Console.WriteLine("Web Url: " + appinstance.AppWebFullUrl);  
  37.          Console.WriteLine("Start Page: " + appinstance.StartPage);  
  38.          Console.WriteLine("Installation Status: " + appinstance.Status);  
  39.          Console.WriteLine("Remote App Url: " + appinstance.RemoteAppUrl);  
  40.          Console.WriteLine("Settings Page: " + appinstance.SettingsPageUrl);  
  41.          Console.WriteLine("Error Details: " + appinstance.GetErrorDetails());  
  42.          Console.WriteLine("==================Completed App Details =================");
  43.        }     
  44.        Console.WriteLine("Completed...");  
  45.        Console.ReadLine();



Here is the output :



If you observe the above code, we have created a object called appInstanceCollection for the ClientObjectCollection class. It needs ClientRuntimeContext and the web.

The ClientRuntimeContext represents the runtime context for  accessing data from remote objects and invoking methods on remote objects. This can be safely typecasted from ClientContext class as shown above.

Note : In the new version of the CSOM dll  you can find a new method called "GetAppDetails()" from the APPcatalog class. Please refer this link.

Hope this helps,

-Purna

1 comment: