Tom Clarkson

SharePoint, Startups and some other stuff

Dataview Web Parts and XSL as an Alternative to Custom Code

with 14 comments

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.

 
 


 
 

This needs to be changed to

 
 


 
 

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 `__abENT__lt;xsl:value-of select=__abENT__quot;@ColumnName__abENT__quot; __abENT__#8260;__abENT__gt;` or `__abENT__lt;element attribute=__abENT__quot;{@ColumnName}__abENT__quot; __abENT__#8260;__abENT__gt;` 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.

Advertisement

Written by Tom Clarkson

September 19, 2008 at 4:05 am

Posted in SharePoint

14 Responses

Subscribe to comments with RSS.

  1. 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.

    dick penny

    October 11, 2008 at 10:25 am

  2. 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.

    Tom Clarkson

    October 11, 2008 at 6:58 pm

  3. Tom,

    Is it possible to create a DataView in SPD, export the .aspx file, and then deploy it as a feature within Visual Studio?

    Chris

    October 15, 2008 at 1:42 pm

  4. It is possible, though in most cases just copying the data view is a better option.

    Tom Clarkson

    October 15, 2008 at 5:24 pm

  5. 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?

    Chris

    October 15, 2008 at 7:34 pm

  6. 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.

    Tom Clarkson

    October 15, 2008 at 9:02 pm

  7. Thanks

    Chris

    October 15, 2008 at 9:18 pm

  8. 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?

    Jen

    April 16, 2009 at 10:52 am

  9. 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??

    Daniel Walker

    April 29, 2009 at 4:07 pm

  10. 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)

    paisleygo

    June 26, 2009 at 5:18 pm

  11. 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?

    austindev

    June 27, 2009 at 12:49 pm

  12. 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?

    AndyC

    July 1, 2009 at 8:45 am

  13. 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

    Sara Tenenbaum

    August 7, 2009 at 1:38 pm

  14. 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

    Do i need to make changes at any other places for example in the below code?

    Thanks in advance.

    keval

    October 16, 2009 at 9:26 am


Leave a Reply

Fill in your details below or click an icon to log in:

Gravatar
WordPress.com Logo

Please log in to WordPress.com to post a comment to your blog.

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.