user3658298
user3658298

Reputation: 341

Determine which list a provider hosted application has access to

I have created a simple Provider Hosted App ( straight out of the Visual Studio 2013 box )

The App has been granted "Read List Permissions" in the app manifest.

When the app is deployed SharePoint Online asks the User to select the list that the app can access. This should give it permission to read one specific list on the Host Web.

I cannot figure out how the MVC Provider Hosted Web part of the application actually determines which SharePoint list the end user has granted it permission to read.

Presumable it gets some sort of token to tell it which list it can read? Or is their a CSOM/JSOM call that will reveal which list it can access?

Upvotes: 0

Views: 552

Answers (1)

Vadim Gremyachev
Vadim Gremyachev

Reputation: 59358

To determine which list the user has granted permission to read you could consider the following approach:

Example

1)For current user

using (var ctx = new ClientContext(webUri))
{
        var web = ctx.Web;

        ctx.Load(web.Lists,
             lists => lists.Include(list => list.Title,
                                    list => list.EffectiveBasePermissions));

        ctx.ExecuteQuery();
        var currentUserLists = web.Lists.Where(l => l.EffectiveBasePermissions.Has(PermissionKind.OpenItems));
}

2)For any user

using (var ctx = new ClientContext(webUri))
{
     var web = ctx.Web;

     //load lists
     ctx.Load(web.Lists);
     ctx.ExecuteQuery();
     //load lists permissions for a specified user
     var listsPermissionsResults = web.Lists.ToDictionary(list => list, list => list.GetUserEffectivePermissions(loginName));
     ctx.ExecuteQuery();

     //filter lists where user has been granted permissions to open list 
     var userLists = new List<List>();
     foreach (var result in listsPermissionsResults)
     {
           var list = result.Key;
           var listPermissions = result.Value.Value;
           if (listPermissions.Has(PermissionKind.OpenItems))
           {
                userLists.Add(list);
            }
     }
}

Upvotes: 0

Related Questions