4

Is it possible using code to get all the names of the sites in SharePoint? Specifically, is it possible to list all the names of the lists for each site, and list all the users and their access to the lists?

John Rudy
  • 37,282
  • 14
  • 64
  • 100
Rupert
  • 1,296
  • 8
  • 24
  • 36

1 Answers1

8

Quick, dirty, only somewhat tested but it oughtta work. Replace the web application URL with your own:

 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();
                }
            }
        }
Janis Veinbergs
  • 6,907
  • 5
  • 48
  • 78
zincorp
  • 3,284
  • 1
  • 15
  • 18
  • How can you dispose of a site while your still enumerating over it? – Daniel Revell Feb 13 '10 at 14:32
  • 1
    Unless I'm having a huge blond moment I'm pretty sure I made it dispose the site object at the end of the loop – zincorp Feb 15 '10 at 16:47
  • I'v edited your answer, because you should never dispose site.RootWeb i.e `using(SPWeb web = site.RootWeb)`. "An earlier version of this article indicated that the calling application should dispose of the SPSite.RootWeb property just before disposing of the SPSite object that is using it. This is no longer the official guidance. The dispose cleanup is handled automatically by the SharePoint framework." from http://msdn.microsoft.com/en-us/library/aa973248(v=office.12).aspx – Janis Veinbergs Mar 07 '12 at 06:46