Miscellaneous Debris

Avner Kashtan's Frustrations and Exultations

Binding to Sharepoint List Items (Part 2 of 2)

In the first part of the post we went over binding an SPListItemCollection to a DataGrid for quick'n'dirty binding of Sharepoint list data. The problem with this approach is that extensibility is a problem. To paraphrase the specific request I got: "Remember that webpart where you put the List items in a grid? Now I want you to have the grid show the items from two other sites too".

In our first approach, we would be somewhat screwed - the SPListItemCollection represents items in a single list, and I can't merge several of those to one object. I would have to start writing code to take the data out of my SPListItems and putting them into DataRows and do a lot of dirty work - but luckily Sharepoint does that dirty work for us already:

DataSet allItems = new DataSet();
allItems.Merge (myItems.GetDataTable());

This code effortlessly puts a DataTable (with the same name as our List) into a DataSet - no fuss, no mess.
If I now have several lists with the same name and structure but in different sites, I can run the Merge for each and have them all fit in one nice table, and then bind to them in the usual fashion:

<%# DataBinder.Eval (Container.DataItem, "MyCustomProperty") %>

In my case, I wanted the grid to show the items for the selected site's list and for all its subsites, so a simple recursive function got me a DataTable with all items included:

private DataSet getAllItems (SPWeb web, string listName)
{
   DataSet items = new DataSet();
   items.Merge (web.Lists[listName].Items.GetDataTable());
   foreach (SPWeb subweb in web.Webs)
   {
      items.Merge(getAllItems(subWeb));
   }   
   return items;
}

Note: For brevity's sake I've omitted try/catch blocks, but don't forget that most SPS collections don't have a Contains function, and collection accesses like web.Lists[] must be protected with try/catch.

A further thing I can do with this approach is add more data - like a computed column - into the table:

private DataSet getItemsWithUrl (SPWeb web, string listName)
{
   DataSet items = new DataSet();
   SPList list = web.Lists[listName];
   string displayUrl = web.Url + "/" + list.Forms[PAGETYPE.PAGE_DISPFORM].Url + "?ID=";
   DataTable itemTable = list.Items.GetDataTable();
   DataColumn UrlColumn = new DataColumn ("Url", typeof(string), "'" + displayUrl + "' + ID");
   itemTable.Columns.Add(UrlColumn);

   items.Merge (itemTable);
   return items;
}

This variant on the previous function gets the list's default Display Item Form from the Forms collection and builds the URL. The computed column we added concats this base URL to the ID of the item, and presto - we have a link to display the ListItem:

<asp:HyperLink NavigateUrl='<%# DataBinder.Eval (Container.DataItem, "Url") %>'>
   <%# DataBinder.Eval (Container.DataItem, "Title") %>
</asp:HyperLink>

I hope this helps to bind Sharepoint to ASP.NET quickly and painlessly. :)

Posted: Sep 22 2004, 09:00 PM by AvnerK | with 13 comment(s) |
Filed under: ,

Comments

Donato said:

Yes! Finally something about Entertainment.

# August 23, 2012 8:11 PM

Starling said:

I am sure this article has touched all the internet people, its

really really good paragraph on building up new blog.

# August 31, 2012 7:49 AM

bookmarking submission said:

mMAUTC A round of applause for your blog post.Really looking forward to read more. Really Great.

# September 9, 2012 6:14 AM

cheap nopalea said:

I loved your article post. Will read on...

# September 16, 2012 7:01 AM

cheap bookmarking service said:

Z296HY Wow, great blog.Much thanks again. Will read on...

# September 20, 2012 1:02 AM

crork said:

mfSni2 Thanks for sharing, this is a fantastic blog article.Really looking forward to read more. Really Cool.

# September 20, 2012 6:22 PM

accident injury law said:

Sorry for the off-topic, could you tell where I can get such a nice pattern for my blog ?!...

# October 3, 2012 5:43 AM

icon designs said:

<a href="www.mobiledone.com/index.php It is a pity, that now I can not express - it is very occupied. But I will return - I will necessarily write that I think.</a>

# October 9, 2012 5:17 AM

Whitehead said:

Hello There. I discovered your weblog the usage of

msn. This is an extremely smartly written article.

I'll be sure to bookmark it and come back to read extra of your useful information. Thanks for the post. I'll definitely return.

# December 25, 2012 7:50 AM

lose weight pills said:

bsKfAi Say, you got a nice blog post.Really looking forward to read more. Awesome.

# February 1, 2013 1:26 PM

Whiteboard Marker said:

Thanks so much for the blog.Much thanks again.

# February 3, 2013 10:58 AM

Bubble Wrap said:

This is one awesome post.Much thanks again. Much obliged.

# February 4, 2013 6:35 AM

Tam said:

Fantastic beat ! I wish to apprentice even as you amend your website, how can i subscribe for a

blog website? The account helped me a appropriate deal.

I had been a little bit familiar of this your broadcast offered bright transparent

concept

# April 16, 2013 10:39 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)