2010-02-11 103 views

回答

8

快速,骯髒,只是有點測試,但它應該工作。用您自己的網址替換網絡應用程序URL:

static void ListLists() 
     { 
      SPWebApplication webApp = SPWebApplication.Lookup(new Uri("http://mossdev:8060")); 
      foreach(SPSite site in webApp.Sites) 
      { 
       try 
       { 
        PrintWebAndListsRecursive(site.RootWeb, 0); 
       } 
       finally 
       { 
        site.Dispose(); 
       } 
      } 

      Console.ReadLine(); 
     } 

     static void PrintWebAndListsRecursive(SPWeb web, int level) 
     { 
      Console.WriteLine("".PadLeft(level * 3) + "Site: {0} ({1})", web.Title, web.Url); 
      foreach(SPList list in web.Lists) 
      { 
       Console.WriteLine("".PadLeft((level + 1) * 3) + "List: {0}", list.Title); 
       foreach(SPRoleAssignment roleAssignment in list.RoleAssignments) 
       {      
        if(roleAssignment.Member is SPGroup) 
        { 
         var group = (SPGroup) roleAssignment.Member; 
         Console.WriteLine("".PadLeft((level + 2) * 3) + "Group: {0}", group.Name); 
         foreach(SPUser user in group.Users) 
         { 
          Console.WriteLine("".PadLeft((level + 4) * 3) + user.Name); 
         } 
        } 
        else 
        { 
         Console.WriteLine("".PadLeft((level + 2) *3) + "User: {0}", roleAssignment.Member.Name); 
        } 

        foreach(SPRoleDefinition roleDef in roleAssignment.RoleDefinitionBindings) 
        { 
         if (!roleDef.Hidden) 
         { 
          Console.WriteLine("".PadLeft((level + 3) * 3) + "Role Definition: {0}", roleDef.Name); 
         } 
        } 
       } 
      } 

      foreach(SPWeb subWeb in web.Webs) 
      { 
       try 
       { 
        PrintWebAndListsRecursive(subWeb, level+1); 
       } 
       finally 
       { 
        subWeb.Dispose(); 
       } 
      } 
     } 
+0

如何處理網站,同時仍在對其進行枚舉? – 2010-02-13 14:32:08

+1

除非我有一個巨大的金髮時刻,我很確定我讓它在循環結束時處理了站點對象 – zincorp 2010-02-15 16:47:46

+0

好!這幫了我很多! :)謝謝zincorp! – 2011-01-13 10:40:49

相關問題