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 += new EventHandler<GetGroupCollectionFromUserCompletedEventArgs>(service_GetGroupCollectionFromUserCompleted);
service.GetGroupCollectionFromUserAsync(UserLoginName); //see this link to get user
  
//event to get the groups====================================
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());   }
 }

Comments

Popular posts from this blog

Upload Single/Multiple file by using the REST API and jQuery SharePoint 2013

Add content type to SharePoint List/Library using REST API

A type named 'SP.Data. could not be resolved by the model error