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

Sathish Nadarajan
SharePoint MVP
Published On :   11 Oct 2016
Visit Count
Today :  2    Total :   6013

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)
         public static void DeployAPPDeveloperFeature()
             OfficeDevPnP.Core.AuthenticationManager authMgr = new OfficeDevPnP.Core.AuthenticationManager();
             string userName = "*******@*****";
             string password = "********";
             string siteUrl = "https://*******";
             using (var clientContext = authMgr.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))
                 Web web = clientContext.Web;
                 var appInstance = Deploy(clientContext, "D:\\PRACTICE SOURCE CODE\\PNP\\Office365.PHA.OnPrem.APP\\Office365.PHA.OnPrem.APP\\bin\\Debug\\app.publish\\\\");
                 if (appInstance != null && appInstance.Status == AppInstanceStatus.Initialized)
                     System.Console.WriteLine("App was installed.");
         public static AppInstance Deploy(ClientContext context, string appFullPath)
             using (var packageStream = System.IO.File.OpenRead(appFullPath))
                 var appInstance = context.Web.LoadAndInstallApp(packageStream);
                 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));
             if (result.Any()) return;
             var feature = ctx.Site.Features.Add(DeveloperFeatureId, true, FeatureDefinitionScope.None);
         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.