1

I am checking permissions if a user has permissions (at least read) to a calendar page on sharepoint online. This code does not seem to always work meaning give Access Denied even though I can see the calendar by going to the URL. Code always breaks at last Executequery so I know it sees the list. Also, not sure if it checks if Everyone as access to and therefor the user does. CalendarUrl: https://microsoft.sharepoint.com/stuff/stuffer/Lists/theCal/calendar.aspx siteUrl = "https://microsoft.sharepoint.com/stuff/stuffer"

try
{
    ClientContext clientContext = new ClientContext(siteUrl);
    string userLogin = "i:0#.f|membership|" + owner.ToLower();
    List oList = clientContext.Web.Lists.GetByTitle(listName);
    clientContext.Load(oList);
    clientContext.ExecuteQuery();

    User usr = clientContext.Web.EnsureUser(userLogin);
    clientContext.Load(usr);
    clientContext.ExecuteQuery();

    ClientResult<BasePermissions> result = item.GetUserEffectivePermissions(usr.LoginName);
    clientContext.ExecuteQuery();

    if(result.Value.Has(PermissionKind.ViewListItems))
    {
        return CallStatus.Ok;
    }
}
catch (Exception ex)
{
    //User has no permission
}

What should I do to get an 'OK' for a user to have permission on the calendar page either directly or through "Everyone" setting on the calendar page? Thanks again.

1 Answer 1

0

The following example demonstrates how to verify whether the user has access to page using HttpResponse.Status property:

public static bool CanAccessPage(string pageUrl,ICredentials credentials)
{
    var request = WebRequest.Create(pageUrl);
    request.Credentials = credentials;
    request.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f");
    try
    {
        using (request.GetResponse())
        {
           return true;
        }
    }
    catch (WebException e)
    {
       using (var response = e.Response)
       {
          var httpResponse = (HttpWebResponse)response;
          if (httpResponse.StatusCode == HttpStatusCode.Forbidden)
               return false;
          throw;
       }
    }
}

Example

var pageUrl = "https://contoso.sharepoint.com/Pages/LatestNews.aspx";
var credentials = new SharePointOnlineCredentials(userName, securePassword);
if (CanAccessPage(pageUrl,credentials))
{
    //...            
}
4
  • I am able to get the username from username from _spPageContextInfo.userLoginName. How would I get the password? Before I was using a service credentials to check the user.
    – pomo
    Commented Nov 3, 2014 at 3:28
  • Also, on a calendar page, you might be able to view the page itself but not the data so I don't see how this would work.
    – pomo
    Commented Nov 3, 2014 at 5:02
  • I'm a little bit confused.. _spPageContextInfo.userLoginName..are you doing it on the client-side using JavaScript? Commented Nov 3, 2014 at 15:21
  • I am using a webservice to interact with the calendars. I use javascript on the front end to collect data. I can no use the reset service api in javascript due to cross domain issues.
    – pomo
    Commented Nov 3, 2014 at 16:10

Your Answer

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

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