Tuesday, August 16, 2011

Client Object Model – Part 8

Let us continue exploring client object model. From this post we will explore Silverlight client object model and explore different examples. I would recommend reading part 1 to part 7 first to get to know about client object model and then continue reading from here.

1) Iterating through list items



namespace SilverlightSPCOM
{
public partial class MainPage : UserControl
{
private ClientContext context = null;
private Web web = null;
ListItemCollection listItems;
private delegate void UpdateUIMethod();


public MainPage()
{
InitializeComponent();
}

private void btnRetrive_Click(object sender, RoutedEventArgs e)
{
LoadData();
}

private void LoadData()
{

context = ClientContext.Current;

web = context.Web;

List lstEmployees = web.Lists.GetByTitle("Employees");

CamlQuery Query = new CamlQuery();
Query.ViewXml = "<View/>";

listItems = lstEmployees.GetItems(Query);

context.Load(lstEmployees);
context.Load(listItems);

context.ExecuteQueryAsync(LoadSucces, LoadFailed);

}

private void DisplayData()
{
foreach (ListItem listItem in listItems)
{
lblOutputLabel.Text += Environment.NewLine + "\n" +
"ID: " + listItem.Id + " Title:" + listItem["Title"] + " First Name : " + listItem["First_x0020_Name"]
+ " Last Name" + listItem["Last_x0020_Name"];
}
}

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) Getting only specific properties

Instead of getting all properties of any specific object, if we want to get only specific properties that we are interested in, we can use lambda expression in query. This will result in less network traffic.


namespace SilverlightSPCOM
{
public partial class MainPage : UserControl
{
private ClientContext context = null;
private Web web = null;
ListItemCollection listItems;
private delegate void UpdateUIMethod();


public MainPage()
{
InitializeComponent();
}

private void btnRetrive_Click(object sender, RoutedEventArgs e)
{
LoadData();
}

private void LoadData()
{

context = ClientContext.Current;

web = context.Web;

context.Load(web,
s => s.Title,
s => s.Description);


context.ExecuteQueryAsync(LoadSucces, LoadFailed);

}

private void DisplayData()
{

lblOutputLabel.Text += "Web Title : " + web.Title + Environment.NewLine + "\n" + " Description:" + web.Description ;


}

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);
}


}
}



3) Create a list and list items


namespace SilverlightSPCOM
{
public partial class MainPage : UserControl
{
private ClientContext context = null;
private Web web = null;
private delegate void UpdateUIMethod();


public MainPage()
{
InitializeComponent();
}

private void btnRetrive_Click(object sender, RoutedEventArgs e)
{
LoadData();
}

private void LoadData()
{

context = ClientContext.Current;

web = context.Web;

ListCreationInformation lstCreationListFromSL =
new ListCreationInformation();
lstCreationListFromSL.Title = "List created from silverlight";
lstCreationListFromSL.TemplateType = (int)ListTemplateType.GenericList;
List lstFromSL = web.Lists.Add(lstCreationListFromSL);

Field fldFirstName = lstFromSL.Fields.AddFieldAsXml(@"<Field Type='Text' DisplayName='First Name'/>", true, AddFieldOptions.DefaultValue);
Field fldLastName = lstFromSL.Fields.AddFieldAsXml(@"<Field Type='Text' DisplayName='Last Name'/>", true, AddFieldOptions.DefaultValue);
Field fldAge = lstFromSL.Fields.AddFieldAsXml(@"<Field Type='Number' DisplayName='Age'/>", true, AddFieldOptions.DefaultValue);


ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();

ListItem listItem = lstFromSL.AddItem(itemCreateInfo);
listItem["Title"] = "1 Item created From SL";
listItem["First_x0020_Name"] = "John";
listItem["Last_x0020_Name"] = "Desilva";
listItem["Age"] = "25";
listItem.Update();

listItem = lstFromSL.AddItem(itemCreateInfo);
listItem["Title"] = "2 Item created From SL";
listItem["First_x0020_Name"] = "Michelle";
listItem["Last_x0020_Name"] = "Alberto";
listItem["Age"] = "24";
listItem.Update();


context.ExecuteQueryAsync(LoadSucces, LoadFailed);

}

private void DisplayData()
{

lblOutputLabel.Text += "List Created successfully and items are also 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);
}


}
}



Many more examples to come. Keep reading Part-9 of this series.

2 comments:

Anonymous said...

I am using 2) Getting only specific properties in Silver light and my silver light app renders correctly from documents library but it is failing in button click event to get web title using Lamda expression.

UnHandled error in SilverLight Application The query expression is not supported at Microsoft.SharePoint.Client.DataRetrieval.ProcessMemberAccessQueryExpression

SharePoint Kings said...

Anonymous,
it should not be, if possible please share code so we get some idea.




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