in

Dot Net Mafia

Group site for developer blogs dealing with (usually) .NET, SharePoint 2013, SharePoint 2010, Office 365, SharePoint Online, and other Microsoft products, as well as some discussion of general programming related concepts.

Kyle Kelin on .Net

My First Attempt At Using the SharePoint 2010 Client OM from WPF

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 Microsoft.SharePoint.Client;
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:

        ClientContext context;

        IEnumerable<SP.List> listsCollection;

 

        public MainWindow()

        {

            InitializeComponent();

 

            try

            {

                context = new ClientContext("http://sp2010/");

                Web site = context.Web;

                context.Load(site, osite => osite.Title);

                context.ExecuteQuery();

 

                Title = site.Title;

                ListCollection lists = site.Lists;

                listsCollection = context.LoadQuery(lists.Include(l => l.Title, l => l.Id));

                context.ExecuteQuery();

 

                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;

            list.Update();

 

            context.ExecuteQuery();

        }

 

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.

Side Note:

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.

Comments

No Comments

Leave a Comment

(required)  
(optional)
(required)  
Add

About KyleKelin

Kyle Kelin has been implementing software in the Microsoft space for the past 6 years mainly as a consultant. His interests are SharePoint, .NET, JQuery, and Silverlight.
2012 dotnetmafia.
Powered by Community Server (Non-Commercial Edition), by Telligent Systems