Friday, April 22, 2011

Find Users and Groups for Group Owner

SharePoint treat the domain group as a user. A new entry will be created in the userinfo list(hidden list) as soons as a new domain group added in sharepoint site collection or in the sub site directly. This entry will be created only one time for site collection. The userinfo list view accessable by passing _catelogs/users/simple.aspx in the URL (http://sitecollection:8080/_catelogs/user.simple.aspx).
In SharePoint SPGroup or Domain group can become a owner of another group in SharePoint(SPGroup and Domain Group). To find out the Onwed groups of a SPgroup or Domain Group use below source.


static void Main(string[] args)
{
try
{
using (SPSite siteColl = new SPSite("http://sitecollection/"))
{

//For nested SharePoint groups

SPGroupCollection groups = siteColl.RootWeb.Groups;

foreach (SPGroup group in groups)
{

if(group.Owner.ToString()=="GroupA" )
{
Console.WriteLine("\nGroup Name: " + group.Name + " Group Type: " + group.LoginName);
}
}

//For nested domain groups
SPUser usr=siteColl.RootWeb.AllUsers["domain\groupName"];

foreach (SPGroup domainGroup in usr.OwnedGroups)
{
Console.WriteLine("\nGroup Name: " + domainGroup.Name + " Group Type: " + domainGroup.LoginName);
}


}
}
catch (Exception ex)
{
Console.WriteLine("Error :"+ex.Message);
}

Console.WriteLine("Done");
Console.ReadLine();
}