Tom Clarkson is a SharePoint consultant and entrepreneur based in Sydney, Australia.

Contact Details

Links



Recent Searches



Archives




Past Posts







RSS Feed

Dataview Web Parts and XSL as an Alternative to Custom Code

SharePoint 2007
Tuesday September 23 2008

One of the clients I work with has a policy of not allowing any custom code. This is because the SharePoint environment is shared by 40,000 users and poorly designed code could cause serious performance issues - understandable, but no less inconvenient. Working within this environment I have had to come up with some workarounds to create the functionality users are asking for.

   

One of these workarounds is in making use of the dataview webpart. Most documentation tells you to create these using SharePoint Designer. I don't like that approach due to the fact that it is quite easy to break your site with SPD. Central IT is comfortable with that risk, since you can only break one site at a time - 99% of their users are unaffected, but I don't want to break the site that supports 100% of my users.

   

Fortunately, you can do quite a lot with the dataview web part using just the browser interface. You do need designer to create your first dataview, but you only need to do that once - you can change almost

anything later through the browser.

   

Creating the initial dataview can be done on a virtual machine, so there is no risk of breaking anything (and development is easier when you don't have to deal with the VPN). In SharePoint Designer, create a dataview on any list and convert it to xsl. Don't make any changes before the conversion, as the conversion doesn't always work that well.

   

To make the dataview configurable you will need to make some minor changes to the parameters it takes. By default the dataview is associated with a list using a guid which will not work on another site.

   

<SelectParameters><asp:Parameter DefaultValue="{9B7A603A-466F-474B-9509-BB8423823525}" Name="ListID"></asp:Parameter>

</SelectParameters>

   

This needs to be changed to

   

<SelectParameters><WebPartPages:DataFormParameter Name="ListName" ParameterKey="ListName" PropertyName="ParameterValues" />

</SelectParameters>

   

In the browser, use export from the web part menu to produce a web part file that can be added to the web part gallery of the real site. You now have a dataview web part that can be added to a page without

having to use SharePoint Designer.

   

Although the web part definition does have a list of included columns that will only match the list it was originally created for, they appear to be ignored when you connect it to another list. That means you have complete control of the dataview using the parameters and xsl available on the tool pane.

   

First the parameters. This is where you point the web part to a specific list by changing the default value to match the list name. You can also add additional parameters such as the username. However, it is worth noting that only the parameters included in SelectParameters will affect the query - additional parameters will simply be available in the XSL. The ServerVariable location setting can be quite useful here.

   

Next is the XSL. This is fairly standard. The default XSL from designer gives you a loop over all items in the list, and within that you can display the values of whichever columns you want by using <xsl:value-of

select="@ColumnName" /> or <element attribute="{@ColumnName}" /> if you need to set an attribute of an html element. Note that whatever html you include has to be well formed XHTML to prevent errors.

   

The column name used in the xsl is the internal name of the column. The easiest way to find this when it is not obvious (such as for names containing spaces or columns that have been renamed) is to edit the

column in list settings and look at the end of the query string. Parameter values are declared at the top of the xsl using xsl:param and can be displayed using {$ParameterName}.

   

Although just displaying the content is usually enough, you can also make use of any of the more advanced features available in standard xsl such as sorting.

   

When setting up the XSL, I usually use the XSL property on the tool pane, copying and pasting to visual studio so I get syntax highlighting etc when editing.

If you want to be able to reuse the web part in multiple places, you will want to use XSL Link instead. Save the xsl file in a library somewhere (I usually use the style library of the parent site) and set the XSL Link property to the url of that file. That way you can change the xsl in just one place and not need to update the individual web parts.

   

The parameters (and the xsl link url) do need to be set for individual web parts though - if you are creating a lot of similar web parts you should get those settings right first and export/import the web part so that those settings are used when it is added to the page.

Comments

On 11 Oct 2008 03:25, dick penny said:
Could you eMail me a way to shown a lookup field in a custom edit form AND have it be not editable?  Setting the field to "display" in SPD causes an error. Seems like all lookup fields must be editable. It's a custom list on a MOSS site.

Thanks.
On 11 Oct 2008 11:58, Tom Clarkson said:
For that sort of thing javascript and css tricks can work well. If hiding the editable control on the server causes errors because the value is then not returned when you submit the form, hide it with css - as far as the server can tell it is still there, but you can show the user whatever you want.
On 15 Oct 2008 06:42, Chris said:
Tom,

Is it possible to create a DataView in SPD, export the .aspx file, and then deploy it as a feature within Visual Studio?
On 15 Oct 2008 10:24, Tom Clarkson said:
It is possible, though in most cases just copying the data view is a better option.
On 15 Oct 2008 12:34, Chris said:
I have tried several times to perform the export and add it to the feature however I get the "Webpart is not part of the page" error. Any suggestions?
On 15 Oct 2008 12:38, Chris said:
I have tried several times to perform the export and add it to the feature however I get the "Webpart is not part of the page" error. Any suggestions?
On 15 Oct 2008 12:40, Chris said:
I have tried several times to perform the export and add it to the feature however I get the "Webpart is not part of the page" error. Any suggestions?
On 15 Oct 2008 12:41, Chris said:
Sorry about that...I accidentally refreshed the page while looking at a few more articles.
On 15 Oct 2008 02:02, Tom Clarkson said:
Some of the structures produced by SharePoint Designer don't move between sites that well. 

You could try treating it as a control rather than as a web part - if you don't need to edit it through the browser UI that takes out quite a bit of complexity.
On 15 Oct 2008 02:18, Chris said:
Thanks
On 16 Apr 2009 03:52, Jen said:
I tried following your example but I keep getting your web part can not be displayed. Do you know what I could be doing wrong?
On 29 Apr 2009 09:07, Daniel Walker said:
I used SPD to create a really nice dataview webpart. I used the solution generator to create a wsp package for visual studio and of course the .aspx page is carried into the wsp package, but not the dataview webpart. What do I need to do to get this webpart back into the page??
On 26 Jun 2009 10:18, paisleygo said:
I have been trying to use the fun dvwp styling - that you can do in SPD - but I have been consistently running into 2 roadblocks 

1. First I try using the UI to filter

If I insert the list view webpart onto my page - and then filter it and get it close to how I want it to look (grouped etc...) then go into SPD and "convert to xslt" I get the following error

      "The server returned an non-specific error when trying to get data from the datasource. 
       Check the format and content of your query and try again. 
       If the problem persists, contact the server administrator"

2. Then I try creating the DVWP in SPD

I create a DVWP straight on the page - without using the ui - style it like I want it, and then go to the rendered page and export it - then try to import it back onto the page I want it on...
I get the

System.ArgumentException: The specified view is invalid. 

Any tips on how to debug-troubleshoot what it is that is breaking the thing?

3. And further - once I have the webpart - does anyone know how to alter the code - so I can switch which list it looks at? (in case I want to use it in another site with the same type of list)
On 27 Jun 2009 05:49, austindev said:
I am having similar issue to paisleygo...anytime I export a dvwp that has the sharepoint list toolbar turned on, I get "specified view is invalid".  Anyone know why?
On 01 Jul 2009 01:45, AndyC said:
I get the same error when trying to convert User Information List to a DVWP using approach suggested, ie,   "The server returned an non-specific error when trying to get data from the datasource. 
       Check the format and content of your query and try again. 
       If the problem persists, contact the server administrator"
I want to create a staff lookup by typing Name or part of and thought UIL as a web part would work for this?
On 07 Jul 2009 09:32, said:
fvd
On 07 Aug 2009 06:38, Sara Tenenbaum said:
Hi Tom,

Would you have a sample of how the XSL would look? I am confused as to how to pass the parameters in. Any help or direction you can give me would be greatly appreciated. (I have a very nice web part done in XSLT that uses a lightbox). 

Thanks,

Sara
On 16 Oct 2009 02:26, keval said:
I am getting an issue when migrating the web part page developed in SPD from one site to another .. according to your instruction i have changed the ListId to ListName
Below is the updated code 
<SelectParameters><WebPartPages:DataFormParameter ParameterKey="ListName" PropertyName="ParameterValues" Name="ListName"></WebPartPages:DataFormParameter>
</SelectParameters>
Do i need to make changes at any other places for example in the below code?
<ParameterBinding Name="ListID" Location="None" DefaultValue="{D5F20364-E7CE-493F-BE3B-A29D90A7E386}"/>

Thanks in advance.
On 07 Nov 2009 06:51, said:

Leave a comment