Thursday, August 18, 2011

Client Object Model – Part 9

Let us continue our series of client object model and explore more on Silverlight client object model. Silverlight client object model and managed client object model is almost similar. It’s just that managed client object model works on a .net framework installed on machine where it runs and does not require by default asynchronous processing which is mandatory for Silverlight client object model.

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:




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