MOSS&RMS技术验证方案2

我需要找到文档库中每个文件,用户都有哪些操作权限,写个代码段把文档相关的权限、组、用户都打印出来。测试代码,写的够乱的。 

 

 static void Main(string[] args)
  {
  
   SPSecurity.CodeToRunElevated code = new SPSecurity.CodeToRunElevated(TestCode);
     SPSecurity.RunWithElevatedPrivileges(code);
  }

  public static void TestCode()
  {
   SPSite site = new SPSite("http://mcs-dustyn/Docs");
   SPWeb web = site.AllWebs["Docs"];//打开需要的文档库站点
   // 遍历站点下的每个文档库
   foreach (SPList list in web.Lists)
   {
    if (list.BaseType == SPBaseType.DocumentLibrary)
    {
     SPDocumentLibrary docLibrary = (SPDocumentLibrary)list;
     
     // 就找一个文档库吧
     if (!docLibrary.IsCatalog && list.BaseTemplate != SPListTemplateType.XMLForm && list.Title == "文档")
     {
      // 遍历所有文件夹
      foreach (SPListItem item in docLibrary.Folders)
      {
       Microsoft.SharePoint.SPFolder folder = item.Folder;
       // 遍历文档库中的每个文件
       foreach(SPFile file in folder.Files)
       {
        Console.WriteLine("FileName:" + file.Name + "\tURL:" + file.Url);
        // 搜索文件的所有角色
        foreach (SPRoleAssignment ra in file.Item.RoleAssignments)
        {
         SPPrincipal sp = ra.Member;
         // 遍历文件所属的所有用户角色组
         Console.WriteLine("SPPrincipal[Name:" + sp.Name + ";ID:" + sp.ID+"]");
         // 遍历角色组中的所有角色
         foreach (SPRole role in sp.Roles)
         {
Console.WriteLine("  SPRole[Name:" + role.Name + ";UserCount:"+role.Users.Count.ToString()+";GroupCount:"+role.Groups.Count.ToString()+";ID:" + role.ID + ";Type:" + role.Type.ToString());
          // 遍历角色中的所有用户
          foreach (SPUser user in role.Users)
          {
           Console.WriteLine("    SPUser[Name:" + user.Name+ ";LoginName:" + user.LoginName+";ID:" +user.ID+";Email:" + user.Email + ";Sid:" +user.Sid.ToString() +";RawSid:"+ user.RawSid.ToString());
          }
          foreach (SPGroup group in role.Groups)
          {
           Console.WriteLine("    SPGroup[Name:" + group.Name + ";ID:" + group.ID+ "]");
           foreach (SPUser user1 in group.Users)
           {
            Console.WriteLine("      SPUser[Name:" + user1.Name + ";LoginName:" + user1.LoginName + ";ID:" + user1.ID + ";Email:" + user1.Email + ";Sid:" + user1.Sid.ToString() + ";RawSid:" + user1.RawSid.ToString());
           }
          }
         }
         
        }

       }
      }
     }
    }
   }

   return;

 }

你可能感兴趣的:(OS)