If you have not gone through Part 1 to Part 8, I would recommend you reading them first and then continue reading.
Let’s explore more examples
1) Querying a list and return selected fields only. Here I am retrieving only three fields that I want to show.
namespace SilverlightSPCOM
{
public partial class MainPage : UserControl
{
private ClientContext context = null;
private Web web = null;
private delegate void UpdateUIMethod();
ListItemCollection listItems ;
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 camlQuery = new CamlQuery();
camlQuery.ViewXml =
@"<View>
<Query>
<Where>
<Eq>
<FieldRef Name='Title'/>
<Value Type='Text'>Andre Pal</Value>
</Eq>
</Where>
</Query>
<RowLimit>100</RowLimit>
</View>";
listItems = lstEmployees.GetItems(camlQuery);
context.Load(
listItems,
items => items
.Include(
item => item["Title"],
item => item["Last_x0020_Name"],
item => item["EmployeeID"]));
context.ExecuteQueryAsync(LoadSucces, LoadFailed);
}
private void DisplayData()
{
foreach (ListItem listItem in listItems)
{
lblOutputLabel.Text += "Title:" + listItem["Title"] + " Last Name : " + listItem["Last_x0020_Name"] + " Employee ID : " + listItem["EmployeeID"] + 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) Update items in a list. Here I have used the same above code. We need to take care what when we are dealing with Silverlight, we cannot block UI and hence for every operation that we perform, we need to pass on asynchronous callback functions. Hence when we are updating the record, we have passed on another function which updates and then call appropriate method once succeeded or failed.
namespace SilverlightSPCOM
{
public partial class MainPage : UserControl
{
private ClientContext context = null;
private Web web = null;
private delegate void UpdateUIMethod();
ListItemCollection listItems ;
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 camlQuery = new CamlQuery();
camlQuery.ViewXml =
@"<View>
<Query>
<Where>
<Eq>
<FieldRef Name='Title'/>
<Value Type='Text'>Andre Pal</Value>
</Eq>
</Where>
</Query>
<RowLimit>100</RowLimit>
</View>";
listItems = lstEmployees.GetItems(camlQuery);
context.Load(
listItems,
items => items
.Include(
item => item["Title"],
item => item["Last_x0020_Name"],
item => item["EmployeeID"]));
context.ExecuteQueryAsync(LoadSucces, LoadFailed);
}
private void DisplayData()
{
foreach (ListItem listItem in listItems)
{
listItem["Title"] = listItem["Title"] + " Updated From Code";
listItem.Update();
}
context.ExecuteQueryAsync(LoadSuccesAfterUpdate, LoadFailed);
}
private void UpdateSuccess()
{
MessageBox.Show("All records updated");
}
private void LoadSuccesAfterUpdate(object sender, ClientRequestSucceededEventArgs e)
{
UpdateUIMethod updateUISuccess = UpdateSuccess;
this.Dispatcher.BeginInvoke(updateUISuccess);
}
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) Delete items from list
Deleting list items is pretty easy stuff. However there is one very important thing that we need to take care of. When we get the list items object and when we loop through items to delete them one by one, then collection keep losing one element from it and then we end up having exception thrown from it or finishes without removing certain items. Hence we need to change list items collection with the help of ToList method like shown below.
namespace SilverlightSPCOM
{
public partial class MainPage : UserControl
{
private ClientContext context = null;
private Web web = null;
private delegate void UpdateUIMethod();
ListItemCollection listItems ;
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 camlQuery = new CamlQuery();
camlQuery.ViewXml =
@"<View>
<Query>
<Where>
<Eq>
<FieldRef Name='Title'/>
<Value Type='Text'>Andre Pal</Value>
</Eq>
</Where>
</Query>
<RowLimit>100</RowLimit>
</View>";
listItems = lstEmployees.GetItems(camlQuery);
context.Load(
listItems,
items => items
.Include(
item => item["Title"],
item => item["Last_x0020_Name"],
item => item["EmployeeID"]));
context.ExecuteQueryAsync(LoadSucces, LoadFailed);
}
private void DisplayData()
{
foreach (ListItem listItem in listItems.ToList())
{
listItem.DeleteObject();
}
context.ExecuteQueryAsync(LoadSuccesAfterUpdate, LoadFailed);
}
private void UpdateSuccess()
{
MessageBox.Show("All records deleted");
}
private void LoadSuccesAfterUpdate(object sender, ClientRequestSucceededEventArgs e)
{
UpdateUIMethod updateUISuccess = UpdateSuccess;
this.Dispatcher.BeginInvoke(updateUISuccess);
}
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 10 to explore more on Silverlight client object model.
No comments:
Post a Comment