Tuesday 12 June 2012

How To get all the users From sharepoint groups in client object model

In the below example i have retrieved all the sharepoint users and populated it in a combobox.


Silverlight Main Page Program

  
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint;

namespace users

{

  public partial class MainPage : UserControl

  {

    string user;
 
    GroupCollection Groupcoll;
 
    ClientContext site;

  public  MainPage()
    {

      InitializeComponent();
      site = new ClientContext(ApplicationContext.Current.Url);

      site.Load(site.Web);
      Groupcoll= site.Web.SiteGroups;
      site.Load(Groupcoll, groups => groups.Include(group => group.Users));

      site.ExecuteQueryAsync(new ClientRequestSucceededEventHandler(OnRequestSucceeded), null);

    }

  private void OnRequestSucceeded(Object sender, ClientRequestSucceededEventArgs args)
    {
       Dispatcher.BeginInvoke(datacon);
    }



  private void datacon()
    {
        foreach (Group Grp in Groupcoll)
         {
           UserCollection collUser = Grp.Users;
           foreach (User Usr in collUser)
            {
              user = Convert.ToString(Usr.Title);
              combobox1.Items.Add(user);
            }
         }
     }
  }
}

OUTPUT

No comments:

Post a Comment