I have a custom app name timeSheet.app which is SharePoint hosted app. Now i want to add or install it one of my(4-7) site collection by the console application.
I don't want to upload my app in app catalog.
How can i achieve this? Please Help me!
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It only takes a minute to sign up.
Sign up to join this communityTry the below code:
// Unique ID for side loading feature
Guid sideloadingFeature = new Guid("AE3A1339-61F5-4f8f-81A7-ABD2DA956A7D");
// Prompt for URL
string url = GetUserInput("Please provide URL for the site where app is being installed: \n");
// Prompt for Credentials
Console.WriteLine("Enter Credentials for {0}", url);
string userName = GetUserInput("SharePoint username: ");
SecureString pwd = GetPassword();
// Get path to the location of the app file in file system
string path = GetUserInput("Please provide full path to your app package: \n");
// Create context for SharePoint online
ClientContext ctx = new ClientContext(url);
ctx.AuthenticationMode = ClientAuthenticationMode.Default;
ctx.Credentials = new SharePointOnlineCredentials(userName, pwd);
// Get variables for the operations
Site site = ctx.Site;
Web web = ctx.Web;
try
{
// Make sure we have side loading enabled.
// Using PnP Nuget package extensions.
site.ActivateFeature(sideloadingFeature);
try
{
// Load .app file and install that to site
var appstream = System.IO.File.OpenRead(path);
AppInstance app = web.LoadAndInstallApp(appstream);
ctx.Load(app);
ctx.ExecuteQuery();
}
catch
{
throw;
}
// Disable side loading feature using
// PnP Nuget package extensions.
site.DeactivateFeature(sideloadingFeature);
}
catch (Exception ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(string.Format("Exception!"), ex.ToString());
Console.WriteLine("Press any key to continue.");
Console.Read();
}
Reference - Automating add-in/app installation to SharePoint sites using CSOM
Github reference - PnP Core.SideLoading
For running this code, you need to install the PnP core dll which you can install via Nuget packages as below. You should install the Online dll if targeting for SPO, else install the one for 2013/2016 as per your environment.
This code, activates the side loading feature in the site collection which will allow you to install the app.
Vadim's excellent answer - Install SharePoint app with CSOM
If you can use PowerShell instead of console app, you should think about using Import‑PnPAppPackage
cmdlet from PnP-PowerShell which would be much easier.