Partilhar via


How to: Implement Resources in Custom List Views

Applies to: SharePoint Foundation 2010

This programming task shows how to create a custom view for a standard Microsoft SharePoint Foundation list. In the following example, you create two custom resources: One resource specifies text to introduce a custom link that is provided in the page footer section, and the other modifies the text that is displayed when the list contains no items. The XSLT example overrides the default FreeForm template to define a special footer section with a link to another location in the site collection. It also overrides the ViewEmpty template to modify the text that is rendered for empty lists.

This programming task demonstrates the following steps to create a custom view and implement resources in the view:

  1. Create a custom list definition in Microsoft Visual Studio 2010.

  2. Add an XSLT file to the project, and change the XslLink value in Schema.xml so that it references your XSLT file.

  3. Add a resource file to your project that defines resource strings.

  4. Add parameter bindings to the list’s Schema.xml file, and add declarations to your XSLT file to consume the parameters.

For a programming task that shows how to customize field rendering in list views, see How to: Customize the Rendering of a Field on a List View.

To create a custom list definition

  1. In Visual Studio 2010, on the File menu, point to New, and then click Project.

  2. In the New Project dialog box, under Installed Templates, expand either the Visual Basic node or the Visual C# node.

  3. Expand the SharePoint node, and select 2010.

  4. Under Project Type, select List Definition.

  5. Type a name for the list definition project, and then click OK.

  6. In the SharePoint Customization Wizard, specify a site to use for debugging. For the trust level of your solution, select Deploy as a farm solution, and then click Next.

  7. In the Choose List Definition Settings dialog box, specify a display name for your list definition, select the base list type that you want to use for your definition, and click Finish. The display name that you specify is the name that appears on the Create page.

To add and reference an XSLT file

  1. In Solution Explorer, right-click the project, point to Add, and then click SharePoint Mapped Folder.

  2. In the Add SharePoint Mapped Folder dialog box, select the {SharePointRoot}\TEMPLATE\LAYOUTS\XSL folder, and then click OK.

  3. In Solution Explorer, right-click the new XSL folder, point to Add, and then click New Item.

  4. In the Add New Item dialog box, expand the Visual C# node for C#, or the Common Items node for Visual Basic.

  5. Select Data, and then select XSLT File. Specify a name for the file, and then click Add.

  6. In Solution Explorer, double-click the Schema.xml file to open it in the Code Editor.

  7. Find the All Items or All Documents view definition in the Schema.xml file. You can identify this definition by the DefaultView attribute in the View element, which is set to TRUE.

  8. By default, the XslLink element in the view specifies main.xsl as the XSLT file. Change this value to the name that you gave to the XSLT file that you added in step 5.

To add resources for the custom view

  1. In Solution Explorer, right-click the project, point to Add, and then click SharePoint Mapped Folder.

  2. In the Add SharePoint Mapped Folder dialog box, select the {SharePointRoot}\CONFIG\Resources folder, and then click OK.

  3. In Solution Explorer, right-click the new Resources folder, point to Add, and then click New Item.

  4. In the Add New Item dialog box, expand the Visual C# node or the Common Items node, select General, and then select Resources File. Enter a name for the file, and then click Add.

  5. In the Resource Editor, enter a name and a value for the resource. The value is a string to display when the list is empty. This example specifies "EmptyListResource" as the resource name for the value "Please add information to this list ASAP!", and "WikiResource" for "For information about the contents of this list see the ".

  6. In the Schema.xml file, add parameter bindings to the <ParameterBindings> section of the view. This adds the resources that you specified in the previous step.

    These tags have the following formats: <ParameterBinding Name="EmptyListString" Location="Resource(Resource1,EmptyListResource)" /> and <ParameterBinding Name="TeamWiki" Location="Resource(Resource1,WikiResource)" />. In this example, "EmptyListString" and "TeamWiki" are the parameter names that can be implemented in the XSLT file, "Resource1" is the name of the resource file minus the extension, and "EmptyListResource" and "WikiResource" are the resource names that you provided in the .resx file.

To implement the resources in XSLT code

  1. To override the default FreeForm and EmptyTemplate templates that are defined in vwstyles.xsl, import the main.xsl file into your XSLT file.

    <?xml version="1.0" encoding="utf-8"?>
      <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
    >
    
    <xsl:import href="/_layouts/xsl/main.xsl"/>
    

    Importing main.xsl also imports vwstyles.xsl and fldtypes.xsl into your file, because by default, they are both imported into main.xsl.

    Because you previously modified the XslLink element value and added parameter bindings in the Schema.xml file, the resource strings that you defined are available for use in the XSLT file.

  2. Declare the parameters as follows.

    <xsl:param name="MyEmptyListString" />
    <xsl:param name="TeamWiki" />
    
  3. Copy the FreeForm template into your XSLT file, and add a section that defines special rendering of the footer section in the All Items view, as follows.

    <xsl:template name="Freeform">
      <xsl:param name="AddNewText"/>
      <xsl:param name="ID"/>
      <xsl:variable name="Url">
        <xsl:choose>
          <xsl:when test="List/@TemplateType='119'">
            <xsl:value-of select="$HttpVDir"/>/_layouts/CreateWebPage.aspx?List=<xsl:value-of select="$List"/>&amp;RootFolder=<xsl:value-of select="$XmlDefinition/List/@RootFolder"/>
          </xsl:when>
          <xsl:when test="$IsDocLib">
            <xsl:value-of select="$HttpVDir"/>/_layouts/Upload.aspx?List=<xsl:value-of select="$List"/>&amp;RootFolder=<xsl:value-of select="$XmlDefinition/List/@RootFolder"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="$ENCODED_FORM_NEW"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:variable>
      <xsl:variable name="HeroStyle">
        <xsl:choose>
          <xsl:when test="Toolbar[@Type='Standard']">display:none</xsl:when>
          <xsl:otherwise></xsl:otherwise>
        </xsl:choose>
      </xsl:variable>
      <xsl:if test="$ListRight_AddListItems = '1' and (not($InlineEdit) or $IsDocLib)">
        <table id="Hero-{$WPQ}" width="100%" cellpadding="0" cellspacing="0" border="0" style="{$HeroStyle}">
          <tr><td colspan="2" class="ms-partline">
            <img src="/_layouts/images/blank.gif" width="1" height="1" alt="" />
          </td></tr>
          <tr><td class="ms-addnew" style="padding-bottom: 3px">
            <span style="height:10px;width:10px;position:relative;display:inline-block;overflow:hidden;" class="s4-clust">
             <img src="/_layouts/images/fgimg.png" alt="" style="left:-0px !important;top:-128px !important;position:absolute;"  /></span>
        <xsl:text disable-output-escaping="yes" ddwrt:nbsp-preserve="yes">&amp;nbsp;</xsl:text>
          <xsl:choose>
            <xsl:when test="List/@TemplateType = '115'">
              <a class="ms-addnew" id="{$ID}-{$WPQ}" href="{$Url}"
                onclick="javascript:NewItem2(event, &quot;{$Url}&quot;);javascript:return false;"
                target="_self">
                <xsl:value-of select="$AddNewText" />
              </a>
            </xsl:when>
    
    <!—Define a special footer section for the custom list definition, which has 10000 as template type.-->
            <xsl:when test="$XmlDefinition/List/@TemplateType ='10000'">
              <a class="ms-addnew" id="{$ID}-{$WPQ}" href="{$Url}"
                onclick="javascript:NewItem2(event, &quot;{$Url}&quot;);javascript:return false;"
                target="_self">
              <xsl:value-of select="$AddNewText" />
            </a>
            <p>
              <xsl:value-of select="$TeamWiki"/>
              <a href="{$RootSiteUrl}/TeamWiki">Team Wiki</a>.
            </p>
          </xsl:when>
    
          <xsl:otherwise>
            <a class="ms-addnew" id="{$ID}"
              href="{$Url}" onclick="javascript:NewItem2(event, &quot;{$Url}&quot;);javascript:return false;"
              target="_self">
              <xsl:value-of select="$AddNewText" /></a>
          </xsl:otherwise>
        </xsl:choose>
        </td></tr>
        <tr><td><img src="/_layouts/images/blank.gif" width="1" height="5" alt="" /></td></tr></table>
        <xsl:choose>
          <xsl:when test="Toolbar[@Type='Standard']">
            <script type='text/javascript'>
              if (typeof(heroButtonWebPart<xsl:value-of select="$WPQ"/>) != "undefined")
              {
                <xsl:value-of select="concat('  var eleHero = document.getElementById(&quot;Hero-', $WPQ, '&quot;);')"/>
                  if (eleHero != null)
                    eleHero.style.display = "";
                  }
            </script>
          </xsl:when>
          <xsl:otherwise></xsl:otherwise>
        </xsl:choose>
        <xsl:if test="List/@TemplateType = '115'">
          <script type='text/javascript'>
                if (typeof(DefaultNewButtonWebPart<xsl:value-of select="$WPQ"/>) != "undefined")
                {
                  <xsl:value-of select="concat('  var eleLink = document.getElementById(&quot;', $ID, '-', $WPQ, '&quot;);')"/>
                  if (eleLink != null)
                  {
                    DefaultNewButtonWebPart<xsl:value-of select="$WPQ"/>(eleLink);
                  }
                }
          </script>
        </xsl:if>
      </xsl:if>
    </xsl:template>
    

    The example applies special rendering to the footer section of the view, using the XmlDefinition parameter to retrieve the TemplateType of the list definition, which is 10000. In addition, the example uses the "TeamWiki" parameter to return the "WikiResource" string, and the global "RootSiteUrl" parameter to return the root website of the site collection that contains the parent website of the list. The example assumes the existence of a Wiki library in the root website that is called "TeamWiki".

  4. Add the following code to the XSLT file, which overrides the EmptyTemplate definition to modify the text that is displayed.

      <xsl:template name="EmptyTemplate">
        <tr>
          <td class="ms-vb" colspan="99"  style="color:red;font-weight:bold">
            <xsl:value-of select="$MyEmptyListString"/>
          </td>
        </tr>
      </xsl:template>
    </xsl:stylesheet>
    
  5. On the Build menu, click Build Solution, and then click Deploy Solution to deploy your custom list to the SharePoint Foundation site.

    Note

    When you deploy your project, Visual Studio 2010 places the resource file that you create into the mapped folder that you specified (%ProgramFiles%\Common Files\Microsoft Shared\web server extensions\14\CONFIG\Resources). When you create a new web application, SharePoint Foundation copies the .resx file into the \inetpub\...\App_GlobalResources folder that is created for the web application, which is where the file must be located for the resources to take effect. However, for the file to take effect in web applications that already exist when you deploy your project, you must manually copy the file into the web application’s App_GlobalResources folder.

See Also

Reference

ParameterBinding

Concepts

XsltListViewWebPart and Custom List Views

XSLT Parameter Bindings

Overview of XSLT List View Rendering System