获取SharePoint中的所有网站,列表和用户权限

时间:2010-02-11 22:21:46

标签: sharepoint moss sharepoint-2007

是否可以使用代码获取SharePoint中所有网站的名称?具体来说,是否可以列出每个站点的列表的所有名称,并列出所有用户及其对列表的访问权限?

1 个答案:

答案 0 :(得分:8)

快速,肮脏,只有一些测试,但它应该工作。将Web应用程序URL替换为您自己的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();
                }
            }
        }