DataFormWebPart – create your pagination

DataFormWebPart is simple to be customized if you edit it with SharePoint Designer from design view. But on 2013 version, design view was removed. So it is time to learn how to customize the webpart from the code. And one of the things a developer might be required to do is to create a pagination.

Now, let’s start creating the skeleton of the dataformwebpart.

<WebPartPages:DataFormWebPart runat="server" IsIncluded="True" FrameType="None" NoDefaultStyle="TRUE" ViewFlag="0" Title="" __markuptype="vsattributemarkup" __WebPartId="{1B724E2D-207A-4F03-A851-7DA57139C01B}" id="PMView" showwithsampledata="true" __AllowXSLTEditing="true" WebPart="true" Height="" Width="">

So we have all the sections for the webpart. Now we will add information to each section.

Add the datasource

<SharePoint:SPDataSource runat="server" DataSourceMode="List" SelectCommand="&lt;View&gt;&lt;/View&gt;" UseInternalName="True" ID="MyDataSource"><SelectParameters>
<WebPartPages:DataFormParameter ParameterKey="dvt_sortfield" PropertyName="ParameterValues" Name="dvt_sortfield" DefaultValue="ID" /><WebPartPages:DataFormParameter ParameterKey="dvt_sortdir" PropertyName="ParameterValues" Name="dvt_sortdir" DefaultValue="FALSE" />
<asp:parameter DefaultValue="MyList" Name="ListName" />
<asp:parameter DefaultValue="0" Name="StartRowIndex" />
<asp:parameter DefaultValue="" Name="nextpagedata" />
<asp:parameter DefaultValue="30" Name="MaximumRows" />

You can notice some select parameters:

– ListName – representing the name of the list you want to display;
– StartRowIndex – starting row from where select statement begins;
– nextpagedata – data for the next page, if exists (if not is empty);
– MaximumRows – numbers of rows per page.

Add ParameterBindings section

<ParameterBinding Name="dvt_firstrow" Location="QueryString(FirstRow)" DefaultValue="1"/>
<ParameterBinding Name="dvt_nextpagedata" Location="Postback;Connection"/>
<ParameterBinding Name="dvt_startposition" Location="QueryString(Start)" DefaultValue=""/>
<ParameterBinding Name="Qs" Location="ServerVariable(QUERY_STRING)" DefaultValue=""/>
<ParameterBinding Name="CurrentPageUrl" Location="ServerVariable(URL)" DefaultValue=""/>

Add XSLT code

<xsl:stylesheet xmlns:x="" xmlns:d="" version="1.0" exclude-result-prefixes="xsl msxsl ddwrt" xmlns:ddwrt="" xmlns:asp="" xmlns:__designer="" xmlns:xsl="" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:SharePoint="Microsoft.SharePoint.WebControls" xmlns:ddwrt2="urn:frontpage:internal">
<xsl:output method="html" indent="no"/>
<xsl:decimal-format NaN=""/>
<xsl:param name="dvt_sortfield" />
<xsl:param name="dvt_sortdir" />
<xsl:param name="PageSize">30</xsl:param>
<xsl:param name="dvt_firstrow" />
<xsl:param name="dvt_nextpagedata" />	
<xsl:param name="dvt_startposition" />
<xsl:param name="Qs" />
<xsl:param name="CurrentPageUrl" />
<xsl:param name="FullPath">
	<xsl:when test="$Qs = ''"><xsl:value-of select="$CurrentPageUrl" disable-output-escaping="yes" /></xsl:when>
	<xsl:otherwise><xsl:value-of select="concat($CurrentPageUrl,'?',$Qs)" disable-output-escaping="yes" /></xsl:otherwise>

<xsl:template match="/" xmlns:x="" xmlns:d="" xmlns:asp="" xmlns:__designer="" xmlns:SharePoint="Microsoft.SharePoint.WebControls">

      <xsl:for-each select="/dsQueryResponse/Rows/Row">
	<td><xsl:value-of select="@ID" /></td>
	<td><xsl:value-of select="@Title" /></td>
<td colspan="2">

<!-- Start of Pagination -->
	<xsl:if test="$dvt_nextpagedata != '' or $dvt_firstrow &gt; 1">
<table cellpadding="0" cellspacing="0" border="0">
	<td style="padding:2px 2px 2px 2px;">
	<!-- Previous Page Link -->
	<xsl:if test="$dvt_firstrow &gt; 1">
	<!-- Calculate data for previous page -->
	<xsl:variable name="PreviousID" select="/dsQueryResponse/Rows/Row/@ID[position() = 1]" />
	<xsl:variable name="PreviousPos" select="$dvt_firstrow - $PageSize" />
	<xsl:variable name="PreviousPageData" select="concat('Paged=TRUE&amp;PagedPrev=TRUE&amp;p_ID=',$PreviousID)" />
	<img src="/_layouts/1033/images/prev.gif" alt="Previous page" style="border:0;cursor:pointer;" onclick="javascript:Paginate('{$PreviousPos}','{$PreviousPageData}');" />
	<td style="padding:2px 2px 2px 2px; font-family:Arial, Helvetica, sans-serif; font-size:12px; vertical-align:middle;">
	<xsl:value-of select="$dvt_firstrow" disable-output-escaping="yes" /> - <xsl:value-of select="$dvt_firstrow + $PageSize - 1" />
	<td style="padding:2px 2px 2px 2px;">
	<!-- Link to next page -->
	<xsl:if test="$dvt_nextpagedata != ''">
	<xsl:variable name="NextPos" select="$dvt_firstrow + $PageSize" />
	<img src="/_layouts/1033/images/next.gif" alt="Next" style="border:0;cursor:pointer;" onclick="javascript:Paginate('{$NextPos}','{$dvt_nextpagedata}');"  />
<!-- End of Pagination -->



Add JavaScript for going to next or previous page

function Paginate(firstrow,start) {

        JSRequest.EnsureSetup() ;
	var FilePath = JSRequest.PathName; = FilePath + '?&FirstRow=' + escapeProperly(firstrow) + '&Start=' + escapeProperly(start);

Please not that JSRequest and escapeProperly are included in SharePoint client code.

If you complete each section as I described, you will have an working pagination which is set from querystrings parameters.


3 thoughts on “DataFormWebPart – create your pagination

  1. Matteo

    I just wanted to say a big thank you because you really saved my life. I’m working on SP2010 and I was going crazy in trying to properly build the PreviousPageData object. I was missing the PagedPrev=TRUE attribute. Where did you find it? Is it documented somewhere?

    1. I do not remember where I find PagedPrev attribute. Was also difficult for me create my own pagination. But I am glad my post helped you. If you have other questions, don’t hesitate to contact me. I might be able to help you.

  2. Cris


    I just want to say a big thank you for this nice. I’ve been been searching for days on how to populate dvt_firstrow, and your post really save my sanity..


Leave a Reply

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

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

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s