Skip to content

Instantly share code, notes, and snippets.

@vgrem
Created April 10, 2015 11:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vgrem/ba2cec615e1fbe0c2711 to your computer and use it in GitHub Desktop.
Save vgrem/ba2cec615e1fbe0c2711 to your computer and use it in GitHub Desktop.
SharePoint App Manager
using System;
using System.Linq;
using Microsoft.SharePoint.Client;
namespace SharePoint.Client.Provisioning
{
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");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment