Search code examples
c#powershelloffice365exchange-server

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


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 [email protected]:\Calendar -User "[email protected]"

Solution

  • 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", "[email protected]:\Calendar");
        command.AddParameter("User", "[email protected]"));
        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, "[email protected]"), propSet);