Exercise 3: Editing an existing SPListItem

This exercise builds on top of the previous two Exercises and adds code to the starter solution to show how you can use the Silverlight client object model to change an exisiting SPListItem.

Task 1 – Adding the Client OM Code

In this task, you will add more Silverlight client object model code to the starter solution. This code will change some of the column values on an existing item in the Announcements list.

  1. If the browser is still open from Exercise 1, close it and return to Visual Studio.
  2. From Solution Explorer, in the SilverlightApplication1 project, double-click the file MainPage.xaml to open it and display the Silverlight design canvas.
  3. Double-click on the Edit button in the canvas to open up the MainPage.xaml.cs code-behind file for the Silverlight page.
  4. Make sure that the cursor is located inside the ButtonEdit_Click event that was added when we double-clicked the button from the XAML.
  5. Add the following code

    C#

    ClientContext spContext = new ClientContext(siteUrl); list = spContext.Web.Lists.GetByTitle("Announcements"); spContext.Load(list); item = list.GetItemById(2); spContext.Load(item); item["Body"] = "My edited Silverlight item."; item.Update(); spContext.ExecuteQueryAsync(OnUpdateSuccess, OnFailure);

  6. This code operates the same as the code we added in the previous exercises with regard to batching and its asynchronous nature. In many ways, this Exercise is a combination of the previous two – read an existing item, set some column values in a ListItem object and then save the newly edited item back to SharePoint.
  7. Position your cursor after the closing curly brace for the ButtonEditClick method and add the following code:

    C#

    private void OnUpdateSuccess(object sender, ClientRequestSucceededEventArgs args) { Dispatcher.BeginInvoke(() => { MessageBox.Show("Updated Item Title: " + item["Title"]); }); }

  8. This code is called when our call to the server succeeds. It finishes off our logic – setting new values into the columns of the retrieved list item. This code also utilizes the Dispatcher.BeginInvoke method to tell the UI thread to show the message we want shown to the user. In this case, the message we display includes the Title value retrieved from the newly edited SPListItem.
  9. This completes the coding, so hit F5 to test out the solution. After Visual Studio launches the browser, navigate to https://intranet.contoso.com/sites/IntroducingSLCOM/SitePages/SilverlightApplication1WebPartPage.aspx.
  10. Click the Edit button in the Silverlight web part to call our code. You should see a popup:

    Figure 5

    Displayed popup

Exercise 3 Verification

In order to verify that you have correctly performed all steps of exercise 3, proceed as follows:

Verification 1

In this verification, you can compare the anticipated output shown in step 8 with the actual output shown on your screen. If the two match, you have completed the exercise successfully.