The only prerequisite for installing App into Publishing site, to activate Developer Feature (e374875e-06b6-11e0-b0fa-57f5dfd72085)
.
How to install App using SharePoint CSOM
The following class demonstrates how to:
- ensure Developer Feature is activated
- deploy App
Example:
public class SPAppManager
{
/// <summary>
/// Deploy SP App
/// </summary>
/// <param name="context">Client context</param>
/// <param name="appFullPath">Full path to app file (.app)</param>
/// <returns></returns>
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");
}
Usage
using (var ctx = GetContext(webUri,userName,password))
{
var appInstance = SPAppManager.Deploy(ctx, @"C:\Packages\SPApp.app");
if (appInstance != null && appInstance.Status == AppInstanceStatus.Initialized)
{
Console.WriteLine("App was installed.");
}
}