2014-01-23 173 views
0

我想列出每個Outlook文件夾的權限。我可以添加權限,但是我的代碼會刪除所有舊權限並設置新權限。EWS文件夾權限

這是我的代碼添加權限:

private void updateFolderPermission(Folder folder, String mail, int per) 
{ 
    UserId delegateUser = new UserId(mail); 
    FolderPermission permission = new FolderPermission(delegateUser, FolderPermissionLevel.None); 

    switch (per) 
    { 
    case 0: 
     { 
     permission = new FolderPermission(delegateUser, FolderPermissionLevel.Owner); 
     break; 
     } 
    case 1: 
     { 
     permission = new FolderPermission(delegateUser, FolderPermissionLevel.PublishingEditor); 
     break; 
     } 
    case 2: 
     { 
     permission = new FolderPermission(delegateUser, FolderPermissionLevel.Editor); 
     break; 
     } 
    case 3: 
     { 
     permission = new FolderPermission(delegateUser, FolderPermissionLevel.PublishingAuthor); 
     break; 
     } 
    case 4: 
     { 
     permission = new FolderPermission(delegateUser, FolderPermissionLevel.Author); 
     break; 
     } 
    case 5: 
     { 
     permission = new FolderPermission(delegateUser, FolderPermissionLevel.NoneditingAuthor); 
     break; 
     } 
    case 6: 
     { 
     permission = new FolderPermission(delegateUser, FolderPermissionLevel.Reviewer); 
     break; 
     } 
    case 7: 
     { 
     permission = new FolderPermission(delegateUser, FolderPermissionLevel.Contributor); 
     break; 
     } 
    case 8: 
     { 
     permission = new FolderPermission(delegateUser, FolderPermissionLevel.None); 
     break; 
     } 
    }  
    folder.Permissions.Add(permission); 
    folder.Update(); 
} 

我已經調試代碼,但功能_permissions_顯示我_count = 0_

我的問題是現在: - 如何添加權限到現有的PermissionSet - 如何可以列出此文件夾的所有權限

+0

沒有人有想法嗎? – TuVi

+0

這裏是如何獲得一個文件夾的權限: '屬性集propset命令=新的屬性集(BasePropertySet.IdOnly,FolderSchema.DisplayName,FolderSchema.EffectiveRights);'' 文件夾calfld = Folder.Bind(服務,WellKnownFolderName.Calendar,propset命令);' 'Console.WriteLine(「+ calfld.DisplayName +」文件夾的有效權利是「+ calfld.EffectiveRights);' –

+0

對不起,我看到你問的是燙髮而不是權利。我會看看我能否弄清楚。 –

回答

0

TuVi-

要列出的權限?一個文件夾,創建一個屬性集,其中包括FolderSchema.Permissions,然後綁定到該文件夾​​與屬性設置:

PropertySet propSet = new PropertySet(BasePropertySet.FirstClassProperties, FolderSchema.Permissions); 
Folder folder = Folder.Bind(service, folderid, propSet); 

權限添加到現有權限集,我有我的方式t的工作(以及這裏推薦的:這裏:http://social.msdn.microsoft.com/Forums/exchange/en-US/96da6ca5-9756-4f15-a57c-6a2962820727/ews-get-read-folder-permissions?forum=exchangesvrdevelopment)是刪除當前與用戶關聯的權限,然後添加新的權限。如果您不這樣做,並且您只是嘗試添加新的權限,你會得到一個關於在DACL中複製用戶ID的錯誤。所以下面的代碼刪除舊的燙髮並添加新的燙髮(PublishingAuthor)。

 PropertySet propSet = new PropertySet(BasePropertySet.FirstClassProperties, FolderSchema.Permissions); 
     Folder folder = Folder.Bind(service, folderid, propSet); 

     if (folder.Permissions.Count != 0) 
     { 
      for (int t = 0; t < folder.Permissions.Count; t++) 
      { 
       // Find any permissions associated with the specified user and remove them from the DACL 
       if (folder.Permissions[t].UserId.DisplayName != null || folder.Permissions[t].UserId.PrimarySmtpAddress != null) 
       { 
        folder.Permissions.Remove(folder.Permissions[t]); 
       } 
      } 
     } 

     //Now add the new permissions to the DACL 
     FolderPermission fldperm = new FolderPermission("[email protected]", FolderPermissionLevel.PublishingAuthor); 
     folder.Permissions.Add(fldperm); 
     folder.Update(); 

希望有幫助!如果確實如此,請不要忘記將此答案標記爲已接受。

+0

你好咪咪,謝謝你的回答。我用一段代碼解決了我的問題,就像你的問題一樣。在下面看我的評論。 – TuVi

0
private void updateFolderPermission(Folder folder, String mail, int per) 
{ 
    UserId delegateUser = new UserId(mail); 
    FolderPermission permission = new FolderPermission(delegateUser, FolderPermissionLevel.None); 

    switch (per) 
    { 
    case 0: 
     { 
     permission = new FolderPermission(delegateUser, FolderPermissionLevel.Owner); 
     break; 
     } 
    case 1: 
     { 
     permission = new FolderPermission(delegateUser, FolderPermissionLevel.PublishingEditor); 
     break; 
     } 
    case 2: 
     { 
     permission = new FolderPermission(delegateUser, FolderPermissionLevel.Editor); 
     break; 
     } 
    case 3: 
     { 
     permission = new FolderPermission(delegateUser, FolderPermissionLevel.PublishingAuthor); 
     break; 
     } 
    case 4: 
     { 
     permission = new FolderPermission(delegateUser, FolderPermissionLevel.Author); 
     break; 
     } 
    case 5: 
     { 
     permission = new FolderPermission(delegateUser, FolderPermissionLevel.NoneditingAuthor); 
     break; 
     } 
    case 6: 
     { 
     permission = new FolderPermission(delegateUser, FolderPermissionLevel.Reviewer); 
     break; 
     } 
    case 7: 
     { 
     permission = new FolderPermission(delegateUser, FolderPermissionLevel.Contributor); 
     break; 
     } 
    case 8: 
     { 
     permission = new FolderPermission(delegateUser, FolderPermissionLevel.None); 
     break; 
     } 
    } 

    Folder myFolder = Folder.Bind(this.service, folder.Id); 
    FolderPermissionCollection fpc = myFolder.Permissions; 

    folder.Permissions.Add(permission); 

    foreach (FolderPermission fp in fpc) 
    { 
    if (fp.UserId.DisplayName != null) 
    { 
     //folder.Permissions.Add(oldPer); 
     if (fp.UserId.PrimarySmtpAddress != mail) 
     { 
     oldUser = new UserId(fp.UserId.PrimarySmtpAddress); 
     oldPer = new FolderPermission(oldUser, fp.PermissionLevel); 

     folder.Permissions.Add(oldPer); 
     } 
    } 
    } 

    try 
    { 
    folder.Update(); 
    } 
    catch (Exception ex) 
    { 
    Console.WriteLine(ex.ToString()); 
    } 
}