Tuesday, August 23, 2011

Client Object Model – Part 10

We are going to continue to dive in for further exploration in Silverlight Client object model. If you have not gone through Part 1 to Part 9, I would recommend you reading them first and the continue reading from here.

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:




Share your SharePoint Experiences with us...
As good as the SharePointKings is, we want to make it even better. One of our most valuable sources of input for our Blog Posts comes from ever enthusiastic Visitors/Readers. We welcome every Visitor/Reader to contribute their experiences with SharePoint. It may be in the form of a code stub, snippet, any tips and trick or any crazy thing you have tried with SharePoint.
Send your Articles to sharepointkings@gmail.com with your Profile Summary. We will Post them. The idea is to act as a bridge between you Readers!!!

If anyone would like to have their advertisement posted on this blog, please send us the requirement details to sharepointkings@gmail.com