Displaying the Most Recent Documents from All Document Libraries in a SharePoint Site

Data View Web Parts (DVWPs) are still my favorite tool in my SharePoint toolbox. Whenever I can use them to get the server to do the heavy lifting before the page is rendered, I do. jQuery and all of the fancy template libraries out there are grand, but in most cases it introduces a lag for the user between the time the page shows up in the browser and the time they can see all of the content.

In this case, I wanted to show the most recently modified documents in each of the Document Libraries in one site on that site’s home page. A DVWP using DataSourceMode=”CrossList” is a great way to do this. By using this method, any new Document Libraries will simply show up in the DVWP as soon as they are added. There’s no need to change any code at all.

I don’t usually like to just give the full code to something because I think it’s more beneficial to show the pieces so that you, gentle reader, must adapt things to your own environment and particular business needs. Given the neutered state of SharePoint Designer 2013, though, I think I’ll probably post full DVWPs where I can going forward. It’s simply too darn hard to edit DVWPs in SPD2013. Here I was working in SharePoint 2010, but the DVWP will work the same, whether you are in SharePoint 2007, 2010, or 2013.

This DVWP will display the Document Library name in a header row and then the three most recently modified documents, whether or not they are in folders. I’m display the name of the document as a link, the Modified By user as a link to their info or User Profile (depending on license version), and the Modified date/time columns. If you’d like to show other columns you can add them into the ViewFields in the DataSource section and then into the XSL. (In a CrossList DVWP, you must explicitly request each column you’d like to use.) To adjust the number of documents displayed per Document Library, just change the value of the TopN param near the top of the XSL section.

<WebPartPages:DataFormWebPart runat="server" Description="" ListDisplayName="" PartOrder="1" HelpLink="" AllowRemove="True" IsVisible="True" AllowHide="True" UseSQLDataSourcePaging="True" ExportControlledProperties="True" IsIncludedFilter="" DataSourceID="" Title="Most Recent Documents" ViewFlag="8" NoDefaultStyle="TRUE" AllowConnect="True" FrameState="Normal" PageSize="-1" PartImageLarge="" AsyncRefresh="False" ExportMode="All" Dir="Default" DetailLink="" ShowWithSampleData="False" FrameType="Default" PartImageSmall="" IsIncluded="True" SuppressWebPartChrome="False" AllowEdit="True" EnableOriginalValue="False" AutoRefresh="False" AutoRefreshInterval="60" AllowMinimize="True" ViewContentTypeId="" InitialAsyncDataFetch="False" MissingAssembly="Cannot import this Web Part." HelpMode="Modeless" ListUrl="" ID="g_2b53a84e_bf26_4583_af64_ac20e428b458" ConnectionID="00000000-0000-0000-0000-000000000000" AllowZoneChange="True" ManualRefresh="False" __MarkupType="vsattributemarkup" __WebPartId="{D2B80E07-81CE-48C5-83A1-D124096DC364}" __AllowXSLTEditing="true" WebPart="true" Height="" Width="">
<ParameterBindings>
  <ParameterBinding Name="URL" Location="ServerVariable(URL)" DefaultValue=""/>
</ParameterBindings>
<Xsl>
<xsl:stylesheet xmlns:x="http://www.w3.org/2001/XMLSchema" xmlns:d="http://schemas.microsoft.com/sharepoint/dsp" version="1.0" exclude-result-prefixes="xsl msxsl ddwrt" xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime" xmlns:asp="http://schemas.microsoft.com/ASPNET/20" xmlns:__designer="http://schemas.microsoft.com/WebParts/v2/DataView/designer" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:SharePoint="Microsoft.SharePoint.WebControls" xmlns:ddwrt2="urn:frontpage:internal">
  <xsl:output method="html" indent="no"/>
  <xsl:param name="URL"/>
  <xsl:param name="TopN">3</xsl:param>

  <xsl:template match="/" xmlns:x="http://www.w3.org/2001/XMLSchema" xmlns:d="http://schemas.microsoft.com/sharepoint/dsp" xmlns:asp="http://schemas.microsoft.com/ASPNET/20" xmlns:__designer="http://schemas.microsoft.com/WebParts/v2/DataView/designer" xmlns:SharePoint="Microsoft.SharePoint.WebControls">
    <xsl:call-template name="dvt_1"/>
  </xsl:template>

  <xsl:template name="dvt_1">
    <xsl:variable name="Rows" select="/dsQueryResponse/Rows/Row"/>
    <table border="0" width="100%" cellpadding="2" cellspacing="0">
      <tr valign="top">
        <th class="ms-vh" nowrap="nowrap">Name</th>
        <th class="ms-vh" nowrap="nowrap">Modified By</th>
        <th class="ms-vh" nowrap="nowrap">Modified</th>
      </tr>
      <xsl:call-template name="dvt_1.body">
        <xsl:with-param name="Rows" select="$Rows"/>
      </xsl:call-template>
    </table>
  </xsl:template>

  <xsl:template name="dvt_1.body">
    <xsl:param name="Rows"/>
    <xsl:for-each select="$Rows">
      <xsl:variable name="NewList" select="ddwrt:NameChanged(string(@ListProperty.Title), 0)"/>
      <xsl:if test="string-length($NewList) &gt; 0">
        <tr>
          <td class="ms-vb" style="background-color:#cfcfcf;" colspan="99">
            <a style="color:#990202;" href="/{substring-after(@FileDirRef, ';#')}"><xsl:value-of select="@ListProperty.Title" /></a>
          </td>
        </tr>
        <xsl:call-template name="dvt_1.body2">
          <xsl:with-param name="Rows" select="$Rows[@ListProperty.Title = current()/@ListProperty.Title]"/>
        </xsl:call-template>
      </xsl:if>
    </xsl:for-each>
  </xsl:template>

  <xsl:template name="dvt_1.body2">
    <xsl:param name="Rows"/>
    <xsl:for-each select="$Rows">
      <xsl:sort select="ddwrt:FormatDateTime(string(@Modified), 1033, 'yyyyMMdd HHmmss')" order="descending"/>
      <xsl:if test="position() &lt;= $TopN">
        <xsl:call-template name="dvt_1.rowview" />
      </xsl:if>
    </xsl:for-each>
  </xsl:template>

  <xsl:template name="dvt_1.rowview">
    <tr>
      <xsl:if test="position() mod 2 = 1">
        <xsl:attribute name="class">ms-alternating</xsl:attribute>
      </xsl:if>
      <td class="ms-vb">
        <a href="/{substring-after(@FileDirRef, ';#')}/Forms/DispForm.aspx?ID={@ID}&amp;Source={$URL}"><xsl:value-of select="substring-after(@FileLeafRef, ';#')" /></a>
      </td>
      <td class="ms-vb">
        <a href="/_layouts/userdisp.aspx?ID={substring-before(@Editor, ';#')}"><xsl:value-of select="substring-after(@Editor, ';#')"/></a>
      </td>
      <td class="ms-vb">
        <xsl:value-of select="ddwrt:FormatDate(string(@Modified), 1033, 5)"/>
      </td>
    </tr>
  </xsl:template>

</xsl:stylesheet></Xsl>
<DataSources>
  <SharePoint:SPDataSource runat="server" DataSourceMode="CrossList" SelectCommand="&lt;View&gt;&lt;Lists ServerTemplate='101'&gt;&lt;/Lists&gt;<Query><Where><Eq><FieldRef Name='FSObjType' /><Value Type='int'>0</Value></Eq></Where><OrderBy><ListProperty Name='Title'/></OrderBy></Query>&lt;ViewFields&gt;&lt;ListProperty Name=&quot;Title&quot; /&gt;<FieldRef Name='FSObjType' />&lt;FieldRef Name=&quot;ID&quot;/&gt;&lt;FieldRef Name=&quot;Title&quot;/&gt;&lt;FieldRef Name=&quot;FileRef&quot;/&gt;&lt;FieldRef Name=&quot;FileDirRef&quot;/&gt;&lt;FieldRef Name=&quot;FileLeafRef&quot;/&gt;&lt;FieldRef Name=&quot;Editor&quot;/&gt;&lt;FieldRef Name=&quot;Modified&quot;/&gt;&lt;/ViewFields&gt;&lt;/ViewFields&gt;&lt;/View&gt;" UpdateCommand="" InsertCommand="" DeleteCommand="" UseInternalName="True" UseServerDataFormat="True" ID="dataformwebpart1">
  </SharePoint:SPDataSource>
</DataSources>
</WebPartPages:DataFormWebPart>

Similar Posts

33 Comments

  1. Thank you for your reply Marc.
    I am new to xslt coding, can you please tell me more details
    do i have to look in “<SharePoint:SPDataSource ” section or is that error from ” “?
    I was just copied all your code and pasted in data view web part. Doesn’t changed any value of it.

    1. Just copying and pasting is unlikely to work unless your lists are identical to mine in the post. You should always look at things you find on my blog – if not the entire Web – as examples which you need to adapt to your own purposes.

      M.

  2. Marc you always my favorite in data view web parts. I want to show top 5 most frequently used documents in a document library and document library is having n number of folders. could you please share what could be filter condition for this. I am working on SP 2013.

    1. Ram:

      AFAIK, there’s no “number of times used” count in Document Libraries, so you’d need to define what that means (not as simple as it sounds – viewed? opened? read?), record it, and then build your display around that data.

      M.

    2. i need to Display the Most Recent Documents from only one Document Libraries not all..do you have any solution for this.please let me know

  3. Marc,

    Thanks for the code. We are looking for a way to show the latest documents from a group of libraries and this will make for a great start. Would this be the route to go if we wanted a web part showing just the latest X documents from all libraries? Regardless of which library they are in. I’m also going to play around to see if I can filter out results older than X days (removing the document library header from the list if it’s empty).

    1. Keith:

      Yes, this ought to work for you with some tweaks. You can change the query in the DataSource however you need to. You could also do something similar with script. It depends on your version of SharePoint, your comfort level with script, etc.

      M.

  4. Hi Marc,

    I have a SharePoint 2013 page in that we are displaying more than 20 document library’s data.My requirement is “need to display top 5″ documents which are most viewed from this page.” Can you please help on this ??

  5. Hi Marc,

    I’m having trouble with this one. So copied your code and pasted it into my dvwp. I also added the fields that I wanted to view, but I am also getting the error “datasourceID missing or set to an empty string”. How do I find the Data Source ID of the linked data source that I want to use for this.

    Thanks,

    Dave

  6. @Marc:

    First I want to say that I’m a huge fan of your work with SPServices. I’m reading your post today because I’ve just come to the disappointing realization (very late admittedly) that most of the tools I relied upon for my work have been taken from me in SharePoint Designer 2013. I’ve primarily used the DVWP with my own CSS, SpServices, Jquery, conditional formatting, sorting, grouping, custom action buttons, etc. for all of my web apps for years now. Even with all of the quirks SPD had it was still very fast to implement solutions. My organization is upgrading 2010 to 2016 using Sharegate and I’ve only just now had the opportunity to use SharePoint Designer 2013 for the very first time. What a CRUSHING blow.

    I’m curious, in you opinion, whether or not my previous methods are now discouraged and there’s a new, better way to do things. If the method is still valid I’m in great need of step-by-step instructions for how to manually create and manipulate DVWPs and XSL. Can you recommend any such training websites, videos, or books that target this? I really need to catch up fast.

    Thanks in advance,
    JPL

    1. @JPL:

      Yes, things have changed. Since you’re coming from SharePoint 2010, you’ll find that a number of your favorite techniques are eight not available or harder than they used to be.

      It do happens I have a big, long series about Unlocking the Mysteries of Data View Web Part XSL Tags

      Your best bet is to make your code more future proof is to learn JavaScript. There is lots of good content out there, but it can be daunting. You’ll want to learn how to make REST calls – which frankly are better to work with than the SOAP calls you have been making with SPServices.

      I also have a series about Moving from SPServices to REST

      M.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.