1

I have created and packaged a custom application. The app has been uploaded to App catalog. I can add it manually to a SharePoint Online site and it functions without an issue.

Is there a way to add the app to a particular site collection programmatically?

I have the app on my local computer, so I tried:

        using (var appStream = System.IO.File.OpenRead(appFilePath))
        {
            var appInstance = ctx.Web.LoadAndInstallApp(appStream);
            ctx.Load(appInstance);
            ctx.ExecuteQuery();

            Console.WriteLine(appInstance.AppWebFullUrl);
        }

I received an error: side-loading is not enabled I found the feature, the feature guid "AE3A1339-61F5-4f8f-81A7-ABD2DA956A7D" and the solution here: http://blogs.msdn.com/b/officeapps/archive/2013/12/10/enable-app-sideloading-in-your-non-developer-site-collection.aspx

However when I am enabling the feature, I receive an error: The feature is not installed on this farm. I have no issues installing other features using the same code.

Question: How to add an app that is already in app catalog to a SharePoint Online site?

1 Answer 1

2

In order to publish a SharePoint hosted app to a site collection one of the following conditions should be met:

  • the site must be based on the developer site template
  • Developer Feature (e374875e-06b6-11e0-b0fa-57f5dfd72085) is activated

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");
} 

Gist: SPAppManager.cs

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.");
     }
 }
4
  • 1
    Works, of course it works -it's Vadim's! Thank you, you are a star of this Community.
    – grisha
    Apr 10, 2015 at 16:09
  • Great, l'm glad it worked :) Apr 10, 2015 at 16:24
  • 1
    @grisha, please tell me if your app is also trusted after this process. In my case App gets installed but manually permissions have to be granted.
    – Prakash
    Nov 25, 2015 at 13:07
  • @VadimGremyachev its working great, but i want to update the app. How can i do this? its giving me an error :-An instance of this App already exists at the specified location. First time i uploaded the 1.0.0.1 and Next time 1.0.0.2 Still getting the same issue. Feb 23, 2017 at 11:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.