I took a crack at building a WPF application that interacts with a SharePoint 2010 site. I’m going to skim over how to create a WPF application since you can find that in other blog posts. After creating your WPF app you need to add two references: Microsoft.SharePoint.Client and Microsoft.SharePoint.Client.Runtime. Both assemblies are in the 14 hive of any server running SharePoint 2010.
Next add two using statements in the code behind class of the main window:
using SP = Microsoft.SharePoint.Client;
Then in the constructor of the MainWindow (still in the code behind class) add the follow lines of code like so:
context = new ClientContext("http://sp2010/");
Web site = context.Web;
context.Load(site, osite => osite.Title);
Title = site.Title;
ListCollection lists = site.Lists;
listsCollection = context.LoadQuery(lists.Include(l => l.Title, l => l.Id));
ListBox1.ItemsSource = listsCollection;
ListBox1.DisplayMemberPath = "Title";
This code assumes that there is a ListBox named ListBox1 in your XAML of MainWindow. So you should go and create that now. The first thing the code snippet does is create a ClientContext object based on the URL to our SP site. The next line is using the Load method of the context object. The lamda expression being passed in as the second parameter allows us to only load that property. So we are getting the site but only loading the title property. It is also important to note that the Load method doesn’t execute until we call the ExecuteQuery. Both of these methods were written this way to improve performance by cutting down on either database calls or payload sizes.
The next 3 lines follow a similar pattern of setup then execute. This code uses the site name to retrieve all the lists for that site. Again we only the title and id to reduce the amount of data coming from the server. And again we call ExecuteQuery, failing to do so would result in listsCollection being null.
The final two lines simple bind our list collection to the ListBox so we can display the titles of the lists.
The next thing I wanted to do was to try out updating a list. So add a textbox and button to your window. Add an event handler to the button like so:
private void button1_Click(object sender, RoutedEventArgs e)
var list = (SP.List)ListBox1.SelectedItem;
list.Title = textBox1.Text;
Again same pattern of setup and then execute. I grab the item that is selected in the ListBox and cast it. Be sure you are casting it to the client List object and not the SPList object. As our requirements grow we could simply set more properties or add columns just like we did in SharePoint 2007. Then we call ExecuteQuery to execute the changes. Note: do not forgot to call list.Update before the ExecuteQuery. If you don’t the code will execute without error but the list title would not get updated.
This is my first blog post where I used Visual Studio 2010. In doing so I needed to install CopyAsHtml so I could paste my code in this post properly. I had forgotten about a previous post that Corey Roth had written about getting this add-in to work in VS 2010. Click Here.