How to get user permissions on calendar in exchange server using c#

Viewed 257

I am using exchange calendar in my web application.Before inserting an event to a calendar from my web application to exchange server i would like to get the user permissions on that particular calendar on my c# side.I got the permission by executing the following powershell command .But stuck at getting the same using the c#.

Get-MailboxFolderPermission -Identity john@contoso.com:\Calendar -User "test@test.com"
1 Answers

I could not find any way to it without invoking powershell, but it can be done like this:

var runspace = RunspaceFactory.CreateRunspace();
runspace.Open();

object psSessionConnection;

// Create a powershell session for remote exchange server
using (var powershell = PowerShell.Create())
{
    var command = new PSCommand();
    command.AddCommand("Get-MailboxFolderPermission");
    command.AddParameter("Identity", "john@contoso.com:\Calendar");
    command.AddParameter("User", "test@test.com"));
    powershell.Commands = command;
    powershell.Runspace = runspace;

    var result = powershell.Invoke();
    psSessionConnection = result[0];
}

What you are doing, is to actually run the PS command from the code itself.

I also found the EWS api which probably contains what you are looking for.

Getting the current permissions for a folder by using the Bind method.

    // Create a property set to use for folder binding.
    PropertySet propSet = new PropertySet(BasePropertySet.FirstClassProperties, FolderSchema.Permissions);
    // Bind to the folder and get the current permissions. 
    // This call results in a GetFolder call to EWS.
    Folder sentItemsFolder = Folder.Bind(service, new FolderId(WellKnownFolderName.Calendar, "test@test.com"), propSet);
Related