Thursday 25 October 2012

How to Get Users from AD Group & Sharepoint Group ?

AD Group Users :-
**************
Add Reference from .NET tab or Browse the following location & add the dll.

C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI\microsoft.office.workflow.tasks.dll

using Microsoft.Office.Workflow.Utility; 



Contact c = Contact.FromName("Server Name\\Group Name", Myweb);
if (c != null)
{               
Contact[] contacts = new Contact[] { c };
bool reachedMaxCount = false;
contacts = Contact.ExpandGroups(Myweb, contacts, 100, out reachedMaxCount);
}

The Above Contact Array getting memeber of the group.

****************************************************************************************
Sharepoint Group Users :-
**********************


private static void GetUsersGroups()
{ 
    using (SPSite site = new SPSite("http://sitename"))
    {

        SPWeb web = site.OpenWeb();
        SPGroupCollection groupCollection = web.SiteGroups;
                
        foreach (SPGroup group in groupCollection)
        {
            SPUserCollection userCollection = group.Users;
            Console.WriteLine("Group Name :" + group.Name+"\n");
                    
            foreach (SPUser user in userCollection)
            {
                Console.WriteLine("User Name: " + user.Name + " Email: " + user.Email + " Login: " + user.LoginName);
            }
        }
        //Iterate the owners group
        SPGroup ownerGroup = web.AssociatedOwnerGroup;              
        foreach (SPUser ownerUser in ownerGroup.Users)
        {
            Console.WriteLine("User Name: " + ownerUser.Name + " Email: " + ownerUser.Email + " Login: " + ownerUser.LoginName);
        }
    }
    Console.ReadLine();
}


 
 

No comments:

Post a Comment