EWS文件夹权限

时间:2014-01-23 13:43:28

标签: c# permissions exchangewebservices folder-permissions

我想列出每个Outlook文件夹的权限。我可以添加权限,但我的代码会删除所有旧权限并设置新权限。

这是我添加权限的代码:

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_

我现在的问题是: - 如何向现有权限集添加权限? - 如何列出此文件夹的所有权限?

2 个答案:

答案 0 :(得分:0)

TuVi -

要列出文件夹的权限,请创建包含FolderSchema.Permissions的属性集,然后使用该属性集绑定到文件夹:

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

要向现有权限集添加权限,我使用它的方式(以及此处建议的内容:此处:http://social.msdn.microsoft.com/Forums/exchange/en-US/96da6ca5-9756-4f15-a57c-6a2962820727/ews-get-read-folder-permissions?forum=exchangesvrdevelopment)是删除当前与用户关联的权限,然后添加新权限如果你不这样做,并且你只是尝试添加新的权限,那么你将收到有关在DACL中复制用户ID的错误。因此,以下代码删除旧的perms并添加新的perms(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("sadied@contoso.onmicrosoft.com", FolderPermissionLevel.PublishingAuthor);
        folder.Permissions.Add(fldperm);
        folder.Update(); 

希望有所帮助!如果确实如此,请不要忘记将此答案标记为已接受。

答案 1 :(得分: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());
  }
}