Exercise 2: Creating a new SPListItem

This exercise builds on top of the

previous Exercise and adds code to the starter solution to show how you can use the Silverlight client object model to create a new 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 create a new item in a SharePoint 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 Create 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 ButtonCreate_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); Web oWebsite = spContext.Web; ListCollection collList = oWebsite.Lists; list = spContext.Web.Lists.GetByTitle("Announcements"); item = list.AddItem(new ListItemCreationInformation()); item["Title"] = "My new item"; item["Body"] = "My new Silverlight item."; item.Update(); spContext.ExecuteQueryAsync(OnCreateSuccess, OnFailure);

  6. This code operates the same as the code we added in Exercise 1 with regard to batching and its asynchronous nature. However, note the use of the ListItemCreationInformation object to actually create the object in SharePoint. All creatable objects will have a corresponding CreationInformation object.
  7. Position your cursor after the closing curly brace for the ButtonCreateClick method and add the following code:

    C#

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

    This code is called when our call to the server succeeds. It finishes off our logic – actually creating the new 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 created SPListItem.

  8. 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.
  9. Click the Create button in the Silverlight web part to call our code. You should see a popup:

    Figure 4

    Displayed popup

Exercise 2 Verification

In order to verify that you have correctly performed all steps of exercise 2, 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.