Some more examples of Silverlight Client object model
1) Iterating through groups and users
namespace SilverlightSPCOM
{
public partial class MainPage : UserControl
{
private ClientContext context = null;
private Web web = null;
private delegate void UpdateUIMethod();
GroupCollection collGroup;
public MainPage()
{
InitializeComponent();
}
private void btnRetrive_Click(object sender, RoutedEventArgs e)
{
LoadData();
}
private void LoadData()
{
context = ClientContext.Current;
web = context.Web;
collGroup = context.Web.SiteGroups;
context.Load(collGroup);
context.Load(collGroup,
groups => groups.Include(
group => group.Users));
context.ExecuteQueryAsync(LoadSucces, LoadFailed);
}
private void DisplayData()
{
foreach (Group groupItem in collGroup)
{
lblOutputLabel.Text += "Group Name: " + groupItem.Title + Environment.NewLine + "________________" + Environment.NewLine;
UserCollection userColl = groupItem.Users;
foreach (User user in userColl)
{
lblOutputLabel.Text += " User Name : " + user.Title + Environment.NewLine;
}
}
}
private void LoadSucces(object sender, ClientRequestSucceededEventArgs e)
{
UpdateUIMethod updateUI = DisplayData;
this.Dispatcher.BeginInvoke(updateUI);
}
private void LoadFailed(object sender, ClientRequestFailedEventArgs e)
{
MessageBox.Show("Request Failed: " + e.Message + ", Stack Trace:" + e.StackTrace);
}
}
}
2) Iterating through groups and users by including only specific properties
Same as above example, only thing to change is instead of
context.Load(collGroup);
context.Load(collGroup,
groups => groups.Include(
group => group.Users));
We can write something like this:
context.Load(collGroup,
groups => groups.Include(
group => group.Title,
group => group.Id,
group => group.Users.Include(
user => user.Title )));
Here what we have done is we have only included groups ID and Title as well as users Title only. It becomes faster than the previous example due to less network traffic.
3) Creating Group
namespace SilverlightSPCOM
{
public partial class MainPage : UserControl
{
private ClientContext context = null;
private Web web = null;
private delegate void UpdateUIMethod();
GroupCollection collGroup;
public MainPage()
{
InitializeComponent();
}
private void btnRetrive_Click(object sender, RoutedEventArgs e)
{
LoadData();
}
private void LoadData()
{
context = ClientContext.Current;
web = context.Web;
GroupCreationInformation FinanceGroup = new GroupCreationInformation();
FinanceGroup.Title = "Finance Group";
FinanceGroup.Description = "This group has been added through SCOM";
web.SiteGroups.Add(FinanceGroup);
context.ExecuteQueryAsync(LoadSucces, LoadFailed);
}
private void DisplayData()
{
lblOutputLabel.Text = "Group has been created successfully";
}
private void LoadSucces(object sender, ClientRequestSucceededEventArgs e)
{
UpdateUIMethod updateUI = DisplayData;
this.Dispatcher.BeginInvoke(updateUI);
}
private void LoadFailed(object sender, ClientRequestFailedEventArgs e)
{
MessageBox.Show("Request Failed: " + e.Message + ", Stack Trace:" + e.StackTrace);
}
}
}
4) Adding users to a group
namespace SilverlightSPCOM
{
public partial class MainPage : UserControl
{
private ClientContext context = null;
private Web web = null;
private delegate void UpdateUIMethod();
GroupCollection collGroup;
public MainPage()
{
InitializeComponent();
}
private void btnRetrive_Click(object sender, RoutedEventArgs e)
{
LoadData();
}
private void LoadData()
{
context = ClientContext.Current;
web = context.Web;
GroupCollection collGroup = context.Web.SiteGroups;
Group FinanceGroup = collGroup.GetById(3);
UserCreationInformation userCreationInfo = new UserCreationInformation();
userCreationInfo.Email = "name@domain.com";
userCreationInfo.LoginName = @"DOMAIN\name";
userCreationInfo.Title = "Michael";
User User = FinanceGroup.Users.Add(userCreationInfo);
context.ExecuteQueryAsync(LoadSucces, LoadFailed);
}
private void DisplayData()
{
lblOutputLabel.Text = "Group has been created successfully";
}
private void LoadSucces(object sender, ClientRequestSucceededEventArgs e)
{
UpdateUIMethod updateUI = DisplayData;
this.Dispatcher.BeginInvoke(updateUI);
}
private void LoadFailed(object sender, ClientRequestFailedEventArgs e)
{
MessageBox.Show("Request Failed: " + e.Message + ", Stack Trace:" + e.StackTrace);
}
}
}
Keep reading Part 11 to explore Client Object Model more.
No comments:
Post a Comment