Forum Discussion
Not able to install/deploy app in SharePoint Online
Hello
I am using the following code 1 for deploying the app by passing tenant credentials (user and pass). It is working expected. But when I try to deploy using code 2 using GetContextBySecret(client and secret id are added in app config), but getting an error as mentioned in next code 2. I know this can be done thru PowerShell. But we don't want to depend on any tenant credentials. How can we deploy using GetRealmFromTargetUrl? I googled and noticed that everyone suggested it thru PowerShell, Is this possible or not or is this possible MS Graph for deploying apps using CSOM?
Code 1
string userName = "email address removed for privacy reasons";
string Password = "Asdefg";
//bool response = false;
Guid appId = new Guid("77b53b7c-1403-118f-9c0e-cdb6d12c335b");
var SiteUrl = "https://tenant.sharepoint.com/sites/test";
using (ClientContext context = GetContextBySecret(SiteUrl))
{
var securepwd = ConvertToSecureString(Password);
context.Credentials = new SharePointOnlineCredentials(userName, securepwd);
AppManager manager = new AppManager(context);
if (manager.GetAvailable(appId) != null)
{
try
{
Task installTask = Task.Run(async () => await manager.InstallAsync(appId));
installTask.Wait();
}
catch (Exception ex)
{
logger.WriteLog(ex);
}
}
}
Code 2
Guid appId = new Guid("77b53b7c-1403-118f-9c0e-cdb6d12c335b");
var SiteUrl = "https://tenant.sharepoint.com/sites/test";
using (ClientContext context = GetContextBySecret(SiteUrl))
{
AppManager manager = new AppManager(context);
if (manager.GetAvailable(appId) != null)
{
try
{
Task installTask = Task.Run(async () => await manager.InstallAsync(appId));
installTask.Wait();
}
catch (Exception ex)
{
logger.WriteLog(ex);
}
}
}
public static ClientContext GetContextBySecret(String url)
{
ClientContext cc = null;
try
{
Uri siteuri = new Uri(url);
string realm = TokenHelper.GetRealmFromTargetUrl(siteuri);
var token = TokenHelper.GetAppOnlyAccessToken(TokenHelper.SharePointPrincipal, siteuri.Authority, realm).AccessToken;
cc = TokenHelper.GetClientContextWithAccessToken(siteuri.ToString(), token);
}
catch (Exception ex)
{
// logger.WriteLog(ex);
}
return cc;
}
I can see App is added but in showing disabled mode, when I right click on app, I can see Detail, Monitor and Remove options and after click on monitor, I can see Install Error is showing as 1.
When I click on 1 error, I can see following error message
Can anyone please suggest what I am missing here? How to install app using code 2?
Regards
Avian