How to Deploy Provider Hosted Apps (Add-Ins) programmatically using CSOM in SharePoint Office 365 by Activating Developer Site Feature

Sathish Nadarajan
 
Solution Architect
October 11, 2016
 
Rate this article
 
Views
7340

ArticleEarlierIn the earlier Article, we saw how to install an APP by activating the Side Loading Feature. In the same manner, let us see how to deploy the app by activating the Developer Site Feature.

Usually, this method will not be accepted by most of the Clients, because activating the developer feature will not be suitable on the Production environment as most of the production environments will be of team, publishing, Community Sites.

But even, let us know, how the thing can be achieved. The code is very straight forward.

 namespace Console.Office365
 {
     using Microsoft.SharePoint.Client;
     using Newtonsoft.Json.Linq;
     using System;
     using System.Collections.Generic;
     using System.IO;
     using System.Linq;
 
     class Program
     {
         static void Main(string[] args)
         {
             DeployAPPDeveloperFeature();
         }
 
         public static void DeployAPPDeveloperFeature()
         {
             OfficeDevPnP.Core.AuthenticationManager authMgr = new OfficeDevPnP.Core.AuthenticationManager();
 
             string userName = "*******@*****.onmicrosoft.com";
             string password = "********";
             string siteUrl = "https://*******.sharepoint.com/sites/CommunitySite/";
 
             using (var clientContext = authMgr.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))
             {
                 Web web = clientContext.Web;
                 clientContext.Load(web);
                 clientContext.ExecuteQueryRetry();
 
                 var appInstance = Deploy(clientContext, "D:\PRACTICE SOURCE CODE\PNP\Office365.PHA.OnPrem.APP\Office365.PHA.OnPrem.APP\bin\Debug\app.publish\1.0.0.0\Office365.PHA.OnPrem.APP.app");
                 if (appInstance != null && appInstance.Status == AppInstanceStatus.Initialized)
                 {
                     System.Console.WriteLine("App was installed.");
                 }
             }
         }
 
         public static AppInstance Deploy(ClientContext context, string appFullPath)
         {
             EnsureDeveloperFeature(context);
             using (var packageStream = System.IO.File.OpenRead(appFullPath))
             {
                 var appInstance = context.Web.LoadAndInstallApp(packageStream);
                 context.Load(appInstance);
                 context.ExecuteQuery();
                 return appInstance;
             }
         }
 
 
         /// <summary>
         /// Ensure Developer Feature 
         /// </summary>
         /// <param name="ctx"></param>
         private static void EnsureDeveloperFeature(ClientContext ctx)
         {
             var result = ctx.LoadQuery(ctx.Site.Features.Where(f => f.DefinitionId == DeveloperFeatureId));
             ctx.ExecuteQuery();
             if (result.Any()) return;
             var feature = ctx.Site.Features.Add(DeveloperFeatureId, true, FeatureDefinitionScope.None);
             ctx.ExecuteQuery();
         }
 
 
         private static readonly Guid DeveloperFeatureId = new Guid("e374875e-06b6-11e0-b0fa-57f5dfd72085");
 
 
     }
 
 }
 
 

In this approach also, we will be facing the TRUST issue similar to the earlier post. But, even then, there is another alternative approach is there. “APP Stapling”. Let us see what is APP Stapling in the upcoming article.

Happy Coding,

Sathish Nadarajan.

Author Info

Sathish Nadarajan
 
Solution Architect
 
Rate this article
 
Sathish is a Microsoft MVP for SharePoint (Office Servers and Services) having 15+ years of experience in Microsoft Technologies. He holds a Masters Degree in Computer Aided Design and Business ...read more
 

Leave a comment