This article describes how to create a Data View Web Part in SharePoint 2010 that displays a list item randomly with auto refresh. It will also cover up how to make the DVWP reusable over the site collection. This web part could be used as a Quote of the day, Tip of the day or randolmly display pictures from a libray.
In order to try this you will create a list with some items, a page, and a DVWP that you are going to modify a bit. You can also download this web part as it is.
Create the list, a column and some quotes
- Use the browser and create a Custom List named Quote in the root site of the site collection.
- Click at List Settings and the Title column. Rename this to Author and click OK.
- Click at Create Column and type the name Quote. Click OK.
- Go to the list and add at least four items in the list.
Create a page and the Data Form Web Part
- Open SharePoint Designer 2010 and open the root site (home). Click at the File tab, add Item, more pages. Select ASPX and click at the create button. Type QuoteWebPart and click OK twice.
- Select the Design View and select the Insert tab in the ribbon. Click at Display Item Form. Select the Quote list.
-
In the option tab:
- Select Paging and then select Display All Items.
- Select Refresh Interval and set this to 15 Seconds.
- Select Add/Remove Columns. Remove the unwanted columns and add Quote and Author. Click OK.
- Select the Quote and the Author cells. In the Table tab in the ribbon, select Delete and Delete Cells.
Use the code view- Move the @Title after the @Quote. Remove the empty TR and use a DIV to wrap the quote and the title field like the images below. Later on you can replace all the table tags in the data view with markup as you prefer.
Before
After -
Add a a random variable and replace the for-each that render the rows with a random variable and a for-each that select this variable. Search for $Rows.
Replace:<xsl:for-each select="$Rows">
with
<xsl:for-each select="$Rows[position()=$Random]"> <xsl:call-template name="dvt_1.rowview" />
The body template should look like this:
<xsl:template name="dvt_1.body"> <xsl:param name="Rows"/> <xsl:variable name="Random" select="ddwrt:Random(1,count($Rows))" /> <xsl:for-each select="$Rows[position()=$Random]"> <xsl:call-template name="dvt_1.rowview" /> </xsl:for-each> </xsl:template>
Save the page and click at the preview browser button on top of the ribbon to make sure everything works ok. In the following section we will make this web part ready for production.
Apply branding to the Web Part and delete parameters that we don’t need
- Put the CSS inline just below the <XSL> tag. To put the CSS classes inline is just for make this example a bit more easy. I would recommend you to put this three classes in a custom CSS.
- Create a Quote image in the size of 48×44 px and download the image here to your folder for images, in this example /Style Library/MiscStyles/quote-mark.png
- Delete the insert, update and delete parameters in the Data View. We only need the SelectParameter.
<style type="text/css"> #quote { font-family: Arial, Helvetica, sans-serif; font-size: 13px; line-height: 18px; color: #373737; background-color:#fbffec; background:url("/Style Library/MiscStyles/quote-mark.png"); background-position:6px 6px; background-repeat:no-repeat ; height:80px; padding-left: 65px; padding-top:10px; border: 1px solid #edffaf; } #quoteText { text-align: left; font-family: Georgia, Times, serif; width: 300px; margin-bottom:10px; } #quoteAuthor { padding-left:10px; font-style: normal; text-align: left; text-transform: uppercase; font-size: 10px; font-weight: bold; letter-spacing: 1px; font-family: Arial, Helvetica, sans-serif; } </style>
Make the Web Part be ready to be reused in the site collection.
At this point you have to modify some of the XSLT in order to use the web part in sub sites. You don’t need to this if you only want to use this web part at a page in a folder at the top site and don’t upload the web part to the gallery.
- It is three parts in the XSLT you have to modify:
- DataFormWebPart : Set the Viewflag to zero
- SelectParameters: Change the Parameter and add one more
- ParameterBindings: Change the value and name
______________________________________________________ <WebPartPages:DataFormWebPart runat="server" IsIncluded="True" AsyncRefresh="True" FrameType="None" NoDefaultStyle="TRUE" Title="Quote" PageType="PAGE_NORMALVIEW" ListName="{2E43057F-5A51-41D2-B250-051D41DF2652}" Default="FALSE" DisplayName="Quote" __markuptype="vsattributemarkup" __WebPartId="{D7B96070-41A4-485E-A9ED-7DDDFF56C3F7}" id="g_d7b96070_41a4_485e_a9ed_7dddff56c3f7" autorefresh="true" autorefreshinterval="15" ViewFlag="0" > ______________________________________________________ <DataSources> <SharePoint:SPDataSource runat="server" DataSourceMode="List" UseInternalName="true" UseServerDataFormat="true" selectcommand="<View></View>" id="Quote4"> <SelectParameters> <WebPartPages:DataFormParameter Name="ListName" ParameterKey="ListName" PropertyName="ParameterValues" DefaultValue="Quote"/> <WebPartPages:DataFormParameter Name="WebURL" ParameterKey="WebURL" PropertyName="ParameterValues" DefaultValue="/" /> </SelectParameters> </SharePoint:SPDataSource> </DataSources> ______________________________________________________ <ParameterBindings> <ParameterBinding Name="ListName" Location="None" DefaultValue="Quote"/> <ParameterBinding Name="dvt_apos" Location="Postback;Connection"/> </ParameterBindings>
- Save the page and switch to the design mode. Click at the Web Part, click at the Web Part tab in the ribbon and click at To Site Gallery. Type a name and a description and hit OK.
- Go to a page in the site collection to verify that everything works as expected.
Download the web part
You can download the web part here and try this out. This version does not use a background image. To get this working in your environment you have to create a custom list at the root site named Quote. Rename the Title column to Author and create a new column named Quote.
You can also download the DVWP as text file, if you need this a bit more readable.
Drop a comment if you have questions.
/ Christian
A very nice article! The fact that you can make it reusable is really going to save a lot of time. Thanks!
thank you very good article ,
i need to refresh the quotes every day not (15 sec, or 60 sec) how can i do it.
thanks in advance.
Hi Christian,
This article is great!!
I am quite new in SharePoint and maybe you could help me because I have a little problem when I try to test the QuoteWebPart.
I get this message in SD:
Failed setting processor stylesheet: 0x80004005. A reference to variable or ‘Random’ cannot be resolved. The variable or parameter may not be defined, or may not be in scope.
And in the IE browser I get this message:
Unable to display this Web Part. To troubleshoot the problem, open this Web page in a Microsoft SharePoint Foundation-compatible HTML editor such as Microsoft SharePoint Designer. If the problem persists, contact your Web server administrator.
Correlation ID:43e97c3b-faeb-4a23-a016-05eb2f839dca
The thing is that this last ID refresh every 15 seconds, just like it was configured.
Could you please help me or give a hint to solve it?
Thank you!!!
Great article!
Hi Christine!
This is great article. Thank you very much
Manju
Thanks for this, it works great.
Though the last bit about making the web part ready for use is a bit confusing.
I changed the view-flag=0 but the other bit was unclear what to change.
Still worked, saved the web-part and uploaded to my page on my Sub-site.
Many thanks
Hi Brett, thanks for you comment. There’s a couple of things you need to do if you want to use a DVWP for example on a subsite to the site where the the list lives, first set the the viewflag to 0 then make sure the select parameter has these two parameters:
(Set the list name as default value, use ListName instead of ListID)
WebPartPages:DataFormParameter Name=”ListName” ParameterKey=”ListName” PropertyName=”ParameterValues” DefaultValue=”Quote”/
(Add this parameter in the select line. If the list is in the root site set a / as default value, if the list is in a subsite you can set for example /subsite/)
WebPartPages:DataFormParameter Name=”WebURL” ParameterKey=”WebURL” PropertyName=”ParameterValues” DefaultValue=”/”
Finally make sure you use this parameterbinding (use ListName not ListID and the default value should be the lists name):
ParameterBinding Name=”ListName” Location=”None” DefaultValue=”Quote”
Hopes this helps you to clearify this
Thanks for a great Article…….
Thank you so much, I have been looking all over for this solution. I was able to change it around a little to make it look the way I wanted and it works beautifully.
Thank you for a great article! I applied it on a page library with an article page layout instead of a custom list. In that way I’ve created a DVWP with a random image and title. The image was made clickable leading to the corresponding article by @FileRef.
Jenny Fergeus, could you go into a little greater detail. I think you’ve solved something we want to do on our site.
I have a list with 7000 items.From this list I need 10 random items to display in dvwp,by following the above procedure.I can able to see only one item.Could you please tell, how to display 10 or more items randomly in dvwp
This download doesn’t seem to be active anymore. Do you have a new link?
When pulling back a “Person/Group field, I am receiving a bunch of HTML instead of just the field. How can I fix this?
Thank you so much for this. Do you have an update for the steps in SharePoint Designer 2013. Some of the items in your how to do not show up in SharePoint Designer 2013, and yes I am a noob to this, so thank you in advnce.
Hi Brian, quite a long time since I wrote this post, about 6 years now. Actually I do think this will work in SPD2013, maybe it’s need to be tweaked a little bit. I can not verifiy this in an easy manner at the moment. Nowadays we have more options for such requierments such as client side coding with JSOM or REST with jS/jQuery, or even the new client-side web part within the SPFx. Best of luck!