Building a Windows Sidebar Gadget with WPF against SPS 2010

I've been working on creating a solution for the internal MS sales teams here that has some really interesting components/code that I thought I would share. Basically, I was asked to create a windows sidebar gadget that displayed data that was maintained in some SharePoint 2010 lists. There were of course a few twists. The gadget had to continue to work while the sales folks were offline and where possible, I needed to incorporate photo's from people's my sites. I chose to build the solution in WPF. This was after someone else had made an attempt with Silverlight only to realize that on a 64bit machine, Silverlight sidebar gadgets are not supported since the sidebar is run by IE 64bit. So the only real choices were WPF and Javascript. I have done Javascript in the past, but was interested in the challenge of a WPF solution. Here are a few of the major code elements.

Setup of the Solution

The first issue is just really the setup and configuration of how to get a WPF project to render as a gadget. A few tricks I picked up. First of all the main project in the solution was creating using the WPF Browser Application Template. I then added a second project to the solution that I called GadgetDeployment. This second project has the gadget.xml file, a default.html that points to the xbap file created by the WPF app. The trick is to use the Publish Now click-once functionality to place the xbap file and the other application files into the deployment project. Then all you have to do is zip everything up there so that the gadget.xml file is in the root and change the extention from .zip to .gadget. Here are a few code snippets to help you out with some of the details.

First, the gadget.xml file (notice the full security permission since the gadget will have to call some web services):

 <?xml version="1.0" encoding="utf-8" ?>
<name>StuFinder WPF</name>
<author name="Ed Hild">
<info url="" />
<description>StuFinder XBAP Gadget</description>
<host name="sidebar">
<base type="HTML" apiVersion="1.0.0" src="default.html" />
<platform minPlatformVersion="0.3" />


And then the default.html file which delivers the WPF browser application:

 <title>StuFinder - WPF</title>
  body {

        <iframe height="273px"
                src="StuFinder.xbap" />


So after you have done a click-once publish of the application, the results should look like the picture below. You only need to include the Application Files folder, the html file, the gadget xml file, and the xbap file in the deployed gadget. Note that if you actually publish more than once the Application Files folder will get subfolders for each version. You can delete these so you are only deploying the most current version.


Querying the SharePoint Site and use of Isolated Storage

So I had a few SharePoint lists that I needed to pull down into the gadget. I made the choice to use the client-side API for SharePoint 2010 to query for the items. I then created my own (cleaner) XML representation of those items that I stored in isolated storage. Since there were three lists, this resulted in 3 XML files in isolated storage. I'm not doing anything fancy like only looking for updated items on a sync. Since there isn't a tremendous amout of data, I just retrieve all of the items. Here are some code snippets for one of the lists. By the way, I am doing all of this at the App object level of the solution.

public partial class App : Application
public const string STUSPECIALTIES_ISOLATED_FILE_NAME = "STUSpecialties.xml";
public const string SITEURL = "http://sharepoint/siteurl";
 public static XmlDataDocument StuSpecialties = null;
public static IsolatedStorageFile isoStore = null;

 public static void InitData()
//try to retrieve from storage
isoStore = IsolatedStorageFile.GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Assembly, null, null);



Here we see if there is already a file in isolated storage and if so retrieve it. Otherwise, start querying the SharePoint site.

private static void EstablishStuSpecialtiesData()
string[] fileNames = isoStore.GetFileNames(STUSPECIALTIES_ISOLATED_FILE_NAME);
bool isFound = false;

  foreach (string file in fileNames)
isFound = true;
if (isFound)
//read from Isolated Storage
IsolatedStorageFileStream iStream = new IsolatedStorageFileStream(STUSPECIALTIES_ISOLATED_FILE_NAME, System.IO.FileMode.Open, isoStore);
StuSpecialties = new XmlDataDocument();


 And then here is the actual query part where I am using the client-side API.

public static void RetrieveStuSpecialties()
//if not in storage - query SharePoint site
bool success = QueryStuSpecialties();

 //and then store in isolated storage
if (success)

IsolatedStorageFileStream oStream = new IsolatedStorageFileStream(STUSPECIALTIES_ISOLATED_FILE_NAME, System.IO.FileMode.Create, isoStore);
XmlTextWriter writer = new XmlTextWriter(oStream, Encoding.UTF8);
private static bool QueryStuSpecialties()
using (SPClient.ClientContext ctx = new SPClient.ClientContext(SITEURL))
//query to get the list items
SPClient.Web web = ctx.Web;
SPClient.ListCollection coll = web.Lists;
SPClient.List list = coll.GetByTitle("STU Info Specialties");
SPClient.CamlQuery query = SPClient.CamlQuery.CreateAllItemsQuery();
SPClient.ListItemCollection listItems = list.GetItems(query);

items => items
item => item["ID"],
item => item["Title"]));



      //build XML representation of the results for storage and use
StuSpecialties = new System.Xml.XmlDataDocument();
XmlNode xml = StuSpecialties.CreateNode(XmlNodeType.XmlDeclaration, string.Empty, string.Empty);
//add the root
XmlNode rootNode = StuSpecialties.CreateElement("items");
foreach (SPClient.ListItem item in listItems)
XmlElement itemNode = StuSpecialties.CreateElement("item");
itemNode.AppendChild(CreateElementHelper("id", "ID", StuSpecialties, item));
itemNode.AppendChild(CreateElementHelper("title", "Title", StuSpecialties, item));
//add item to the root

return true;

MessageBox.Show( "Cannot retrieve Specialties");
return false;


Getting Photo's from My Sites

One other neat trick is that one of my lists references data about resources and I wanted to include the pictures of those user's from their my sites. Now these pictures will only show up if the user is online and has line of sight to the SharePoint server. I am not storing the images in isolated storage. I only appended the Urls to those images in my XML file. So during the processing of pulling down the data, I use the user's account name: DOMAIN\USER to get the my site picture url property. This code snippet is here as well for your enjoyment. Just remember that you would need to add a reference to the User Profile Web Service for this code to work.

private static XmlElement GetImageInfo(XmlDataDocument doc, SPClient.ListItem item)
XmlElement elementNode = doc.CreateElement("pictureurl");
XmlText elementText = null;
string unknownUrl = "http://msw/_layouts/images/o14_person_placeholder_96.png";

 string accountName = item["MSDomain"].ToString() + "\\" + item["Title"].ToString();

 //setup the picture

   UserProfileSve.UserProfileServiceSoapClient client = new UserProfileSve.UserProfileServiceSoapClient();
client.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;
UserProfileSve.PropertyData property = client.GetUserPropertyByAccountName(accountName, "PictureURL");
elementText = doc.CreateTextNode(property.Values[0].Value.ToString());
elementText = doc.CreateTextNode(unknownUrl);

return elementNode;

There is also another nice helper method I created for parsing some of the SharePoint item properties for building my own XML representation of them. I thought someone might want to reuse it.

private static XmlElement CreateElementHelper(string elementName, string fieldName, XmlDataDocument doc, SPClient.ListItem item)
XmlElement elementNode = doc.CreateElement(elementName);
XmlText elementText = null;
if (item[fieldName] is SPClient.FieldUrlValue)
//email field
SPClient.FieldUrlValue urlField = (SPClient.FieldUrlValue)item[fieldName];
string urlAddr = urlField.Description;
elementText = doc.CreateTextNode(urlAddr);
else if (item[fieldName] is SPClient.FieldLookupValue)
SPClient.FieldLookupValue lookupField = (SPClient.FieldLookupValue)item[fieldName];
string lookupValue = lookupField.LookupValue;
elementText = doc.CreateTextNode(lookupValue);
elementText = doc.CreateTextNode(item[fieldName].ToString());

return elementNode;