SharePoint provider-hosted app licence check

317 views Asked by At

I have an SP app, which I am hoping to release to the store.

The last thing I want to implement in it is a licence check, which I am doing on the server side (C#).

I found that to do this I need to first retrieve the current licences from SharePoint, which I am trying to do with this code:

            ClientResult<AppLicenseCollection> licenseCollection = Utility.GetAppLicenseInformation(clientContext, productId);
            clientContext.Load(clientContext.Web);
            clientContext.ExecuteQuery();

However executing this code gives me an error: The remote server returned an error: (403) Forbidden.

I tried running this with various permissions granted to the app (up to full control over web/site/tenant) still with the same error.

Does anyone know what's missing?, I'm taking the product ID from the app catalog and the context seems ok, as it returns results for other types of requests.

2

There are 2 answers

0
tsw_mik On

Ok, I've got it returning the collection of licences. It looks like the context was incorrectly initialised after some of my recent tweaks and the Read Web permissions seems to be enough to load the licences.

However what I'm seeing now is that the licence collection I'm getting is empty. My app is not in store yet and I've only deployed it to my app catalog, does it need to be in store to get the actual licence returned? If so what's the best way forward, I don't really want to release an unfinished app.

Update: I was able to upload a test licence using SPAppLicenseHelper.GenerateTestToken from this project: https://code.msdn.microsoft.com/officeapps/SharePoint-2013-Import-f5f680a6/

1
UBK On

Do you need to Load the clientContext.Web? I was checking one of the codes that I have used earlier, and it worked just running the clientContext.ExecuteQuery();

try
{
var contextToken = TokenHelper.GetContextTokenFromRequest(Page.Request);
var hostWeb = Page.Request["SPHostUrl"];
var appWeb = Page.Request["SPAppWebUrl"];
using (var clientContext =
TokenHelper.GetClientContextWithContextToken(
hostWeb, contextToken, Request.Url.Authority))
{
 ClientResult<AppLicenseCollection> licenses =
 Microsoft.SharePoint.Client.Utilities.Utility.
 GetAppLicenseInformation(clientContext, _productID);

 clientContext.ExecuteQuery();

 if (licenses.Value.Count == 0)
     throw new InvalidOperationException("No license");
 string licenseString = licenses.Value[0].RawXMLLicenseToken;
 }
}
catch (Exception ex)
{
 Response.Write("Licensing failed: " + ex.Message);
}