SharePoint Blogs / SharePoint University
SharePoint Blogs and SharePoint University - all in one place!
Need SharePoint Training? Attend a SharePoint Bootcamp!

Please delete cookies related to sharepointblogs.com and sharepointu.com to resolve login issues!

How to render Views of Lists

This is one of those cases where I went down several dead ends and spent way too much time chasing after all kinds of reasonable, yet ineffective, approaches, only to collapse in a heap, look up, and see the solution, glimmering above me, all shiny and simple.

The problem at hand: Now that I've got a hold of a SharePoint list (SPList) object, and I also have the View I want to use to display this list (SPView), what are my options for rendering?

Stated more clearly: I have a particular View that is configured on the site that I want to use to drive which items, columns, and sort rules are used when I display my SPList. I want to do all this in a loosely-coupled way so that future changes to the site's view will be reflected in my rendered SPList. 

Dead End #1: Bind the SPList to a GridView. This is surprisingly ugly, though it does reveal a lot of details about what's going on under the hood of the SPListItems. Try it some time just for fun. It's educational, but not what you want.

Dead End #2: Attempt to create some other object that I can then bind to a GridView. How about a DataTable? Simple enough: build a bunch of columns, then populate the rows. Tried this, would have worked, but the effort to do so was a lot more steep than I'd expected, and it seemed like it would be error prone to continue going that route.

The shiny, simple, hindsight-is-20-20 solution: ahem...

myListObject.RenderAsHTML(myQuery);

d'oh.

Ok, here's what I ended up doing (roughly, my actual code is sprinkled across several methods, much of the error handling is removed for clarity):

// SPSite = site collection 

SPSite mySite = SPControl.GetContextSite(Context);

// You really need to set the following CatchAccessDeniedException = false setting! 

// In the event the current logged in user does not have access to the Web

//  (or any other resources) you are attempting to open later,

// this prevents SharePoint from automatically redirecting them to a

// "Authorization Required" page. Instead you will throw a catch-able exception

// which is not ideal as the answer to UserHasPermission() but it seems to be

// all we have.

mySite.CatchAccessDeniedException = false; // now we won't redirect on error

SPWeb myWeb = mySite.AllWebs["sitename"];
SPList myList =  myWeb.GetList(SPEncode.UrlEncodeAsUrl("/Lists/MyListName"));
SPView myFavoriteView = myWeb.GetViewFromUrl(SPEncode.UrlEncodeAsUrl("/Lists/MyListName/MyFavView.aspx"));
SPQuery query = new SPQuery(myFavoriteView);
Literal litOutputGoesHere = new Literal();
litOutputGoesHere.Text = myList.RenderAsHtml(query); // easy!
 
 
 



Posted 05-31-2007 2:18 AM by Unclaimed Blog

Comments

Eric Bartels wrote re: How to render Views of Lists
on 08-09-2007 4:37 PM

Does the toolbar appear when using this method?

I tried RenderAsHtml on SPView and SPList. Both times the toolbar (New item ...) is missing!

BU wrote re: How to render Views of Lists
on 12-07-2007 9:13 AM

Steve:

Were you successful in the next chunk of code that is needed which is to make the sort actually function?

Thx...

Fedor wrote re: How to render Views of Lists
on 02-25-2008 1:07 AM

I fortunately discovered this method in the beginning before moving into several dead ends. However, I do not want the toolbar, only the view items, so now I am considering a more custom way of doing this. However, SPview doesn't reveal any methods to iterate its items!?

Nick wrote re: How to render Views of Lists
on 03-04-2008 8:49 AM

Hi Steve,

Have you tried using RenderAsHtml() multiple times in a web part or on a page?

When I do this, all the drop down links on any list refer to the last list displayed. This seems to be due to RenderAsHtml() always setting the name of the javascript contextInfo() object to ctx1 so each list overwrites the previousone.

I've posted this question with more detail and an attempted solution here:

forums.microsoft.com/.../ShowPost.aspx

Just wondered if you had any ideas?

Thanks,

Nick

Add a Comment

(required)  
(optional)
(required)  
Remember Me?
Need SharePoint Training? Attend a SharePoint Bootcamp!
Posts (c) their respective authors. Everything else (c) 2009 SharePoint Experts, Inc.