Get Sharepoint Users using Silverlight and Web Service
Get Sharepoint Users using Silverlight and Web Service :
Hi here I will explain how to get SharePoint users from SharePoint site using Silverlight And Web services, And also explain how to insert those users to SharePoint list.I gave the silverlight program name as MySilverlightWebService.OutPut Screen :
Design Page :
Xaml Code :
Use the following code between User Control<Grid x:Name="LayoutRoot" Background="White">
<Button Content="insert to list" Height="23" HorizontalAlignment="Left" Margin="228,66,0,0" Name="btninsert" VerticalAlignment="Top" Width="91" Click="btninsert_Click" />
<ComboBox Height="23" HorizontalAlignment="Left" Margin="12,66,0,0" Name="comboBox1" VerticalAlignment="Top" Width="179" />
</Grid>
Add Service Reference :
Before you start coding, add service reference.You can get this sharepoint web service service Reference Url from google.Solution explorer=>References/Service References(Right Click)=>Add Service Reference=>And ther type the following url (https://Your Site Url/_vti_bin/UserGroup.asmx)=> give name as UGService (or whatever name u prefer)
C# Code :
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 System.Xml.Linq;
using MySilverlightWebService.UGService; //here we are calling webservice namespace
namespace MySilverlightWebService
{
public partial class MainPage : UserControl
{
ContextMenu ccsite;
List ListName;
ListItemCollection _icoll;
//this is a custom list used to store user details(This is used while inserting to Sharepoint list)
List<UserDetails> lobjFielduserValus = new List<UserDetails>();
// Create a object of the UserGroupSoapClient class
UGService.UserGroupSoapClient service = new UGService.UserGroupSoapClient();
//Here we are loading site and Sharepoint List Where we are going to insert those users=========
public MainPage()
{
InitializeComponent();
ccsite = new ContextMenu ("Your Site Url");
ccsite.Load(ccsite.Web);
ListName= ccsite.Web.Lists.GetByTitle("Your list Name");
ccsite.Load(ListName);
site.ExecuteQueryAsync(new ClientRequestSucceededEventHandler(OnRequestSucceeded), null);
}
private void success(object sender, ClientRequestSucceededEventArgs arg)
{
Dispatcher.BeginInvoke(datacon);
}
//call service fun here=================================================
private void datacon()
{
// Define the Event handler method to get all users
service.GetAllUserCollectionFromWebCompleted += new EventHandler<GetAllUserCollectionFromWebCompletedEventArgs>(service_GetAllUserCollectionFromWebCompleted);
service.GetAllUserCollectionFromWebAsync();
}
//here we will get all user details==========================================
void service_GetAllUserCollectionFromWebCompleted(object sender, GetAllUserCollectionFromWebCompletedEventArgs e)
{
// e.Result will hold the xml with the User Groups returned by the web method
var allusers = e.Result.Elements().Where(c => c.Name.LocalName == "Users").First().Elements().Where(c => c.Name.LocalName == "User").AsEnumerable();
foreach (XElement ele in allusers)
{
try
{
//to Get other properties like name, open camel query builder and click "User Information List"
comboBox1.Items.Add(ele.Attribute("Name").Value);
//inserting user detail in custom list(This is used while Inserting user to sharepoint list)
lobjFielduserValus.Add(new UserDetails() { Name = ele.Attribute("Name").Value, ID = Convert.ToInt32(ele.Attribute("ID").Value) });
}
catch { }
}
}
//insert user to sharepoint list================================================
private void btninsert_Click(object sender, RoutedEventArgs e)
{
//here we are checking combobox selected name and custom list name
foreach (var item in lobjFielduserValus)
{
if (Convert.ToString(comboBox1.SelectedItem) == item.Name)
{
//get relevent user and insert in list
FieldUserValue fu = new FieldUserValue { LookupId = item.ID };
ListItem litem = ListName.AddItem(new ListItemCreationInformation());
litem["ColumnName"] = fu;
litem.Update();
ccsite.ExecuteQueryAsync(new ClientRequestSucceededEventHandler(succed), null);
break;
}
}
}
private void succed(object sender, ClientRequestSucceededEventArgs arg)
{
}
}
//This is the class we used in custom list to get user details
public class UserDetails
{
public string Name { get; set; }
public int ID { get; set; }
}
}
Comments
Post a Comment