Get SharePoint Groups a user is member of using SharePoint Client Object Model(silverlight & web service)
GET SHAREPOINT GROUPS A USER IS MEMBER OF USING SHAREPOINT CLIENT OBJECT MODEL(SILVERLIGHT & WEB SERVICE)
- Here i will explain how to get all groups for a specific user(who is member of those group) using silverlight and web service.
- In this code we are calling GetGroupCollectionFromUserAsync web method of the SharePoint UserGroup.asmx web service.
- You can see all kind of methods by typing the following url in browser
http://<site url>/_vti_bin/UserGroup.asmx
Steps:
- For detiled steps see this link
- In your silverlight program add a service reference, and type the following url
http://<site url>/_vti_bin/UserGroup.asmx - Give a name(I gave as UGService)
- Add the following Code.
C# Code:
// Create a object of the UserGroupSoapClient class
UGService.UserGroupSoapClient service = new UGService.UserGroupSoapClient();
// Define the Event handler method and Async event
service.GetGroupCollectionFromUserCompleted += newEventHandler<GetGroupCollectionFromUserCompletedEventArgs>(service_GetGroupCollectionFromUserCompleted);
service.GetGroupCollectionFromUserAsync(UserLoginName); //see this link to get user
service.GetGroupCollectionFromUserCompleted += newEventHandler<GetGroupCollectionFromUserCompletedEventArgs>(service_GetGroupCollectionFromUserCompleted);
service.GetGroupCollectionFromUserAsync(UserLoginName); //see this link to get user
void service_GetGroupCollectionFromUserCompleted(object sender,GetGroupCollectionFromUserCompletedEventArgs e)
{
try
{
// e.Result will hold the xml with the User Groups returned by the web method
var eGroups = e.Result.Elements().Where(c => c.Name.LocalName == "Groups").First().Elements().Where(c => c.Name.LocalName == "Group").AsEnumerable();
foreach (XElement ele in eGroups)
{
string GroupName = ele.Attribute("Name").Value;
ComboBox1.Items.Add(GroupName);
}
}
catch (Exception ex)
{ MessageBox.Show(ex.Message.ToString()); }
}
{
try
{
// e.Result will hold the xml with the User Groups returned by the web method
var eGroups = e.Result.Elements().Where(c => c.Name.LocalName == "Groups").First().Elements().Where(c => c.Name.LocalName == "Group").AsEnumerable();
foreach (XElement ele in eGroups)
{
string GroupName = ele.Attribute("Name").Value;
ComboBox1.Items.Add(GroupName);
}
}
catch (Exception ex)
{ MessageBox.Show(ex.Message.ToString()); }
}
No comments:
Post a Comment