Using the SharePoint ClientOM for Silverlight to populate SP list data into a SL DataGrid
We’ll go over the code/projecct in a future post.
So this one is a bit complicated. I set out to do display an entire list in a data grid. But not a fixed list. Not a subset of data. It was the WHOLE list. ANY list.
First, we want to create a grid that will display all lists, and all columns within those lists. That way the user can see all the lists and get an idea as to what columns are in each one, without loading the whole list.
Here’s what we’re after.
Notice the size of that horizontal scrollbar? It’s small (thus the list is wide) and the only column on the right is “fields”. These lists have a lot of hidden fields. For the first round, we’ll show all of them. You’ll see how to show just the visible ones if you wish to do so.
Next, we want to show all items in any particular list, with the content data in a grid form.
This is difficult, because each list has a different schema. We’re not out to show just the fields they all have in common or just a subset like Title and Author. We’re out to show all of the data for each list! And we want it all in a nice grid with scrollbars.
To use the DataGrid, we need to set its ItemSource property. To do so, we must have an object that implements IEnumerable. The best way to do this is to use a ListArray. We can iterate thru each list, grab the data, and stuff it into an object. We’ll then add our object to the ListArray.
Do you see the problem we have? To implement IEnumerable, we need a class. But each list has a different structure. Now, we could go thru each field of every list and try to come up with an uber class. Chances are, we’d miss something. And that’s not to mention custom lists. We’d like to have everything.
So, the first thing I tried was a class with two properties, a Title and an array. The array contained the lists’ fields. No luck. When I set the ItemSource, it treated the Array as an object and gave me “garbage.”
So the second thing I tried was a class where I dynamically add properties. I couldn’t figure that out. Maybe someone smarter than I could figure that out. As far as I can tell, you can’t modify a class that was compiled at design-time.
So finally I thought I might be able to dynamically create an entire class, and obviously creating the properties for each type of list. Creating a class at run-time wasn’t that difficult. And it worked!
So we now have a grid that changes with each list schema. Beautiful!!
So here is the result.
Notice the Style Library view is different than the tasks view.