Friday 1 March 2013

How to customize List Field in A Sharepoint List


How to Customize Sharepoint List Field Xslt


There are some xsl fiels which are responsible for rendering list views. Main.xsl in layouts declare some global variables such as XmlDefinition  etc.
viewStyle.xsl contains view Styles such as default , boxed etc . other is Fieldtypes.xsl whitch are responsible for rendering particular fields.
Suppose we want to customize a field named TestColumn. In some list.
Lets begin by declaring   xsl declaration
<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" xmlns:o="urn:schemas-microsoft-com:office:office">

Now import sharepoint main and internal.xsl
<xsl:include href="/_layouts/xsl/main.xsl"/>
<xsl:include href="/_layouts/xsl/internal.xsl"/>

Several templates are responsible which are in renderview mode ,body mode . as we are going to customize the field text so I can override the template which renders text suppose we have created a column called TestColumn yes/no type We override it as bellow.
<xsl:template name="FieldRef_body.TestColumn" ddwrt:dvt_mode="body" match="FieldRef[@Name='TestColumn']" mode="body" ddwrt:ghost="hide">
    <xsl:param name="thisNode" select="."/>
    <xsl:choose>
    <xsl:when test ="$thisNode/@*[name()=current()/@Name]='Yes'">
      Yes is Writen here
    </xsl:when>
    <xsl:otherwise>
      No Is Writen Here
    </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

full code is bellow.

<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" xmlns:o="urn:schemas-microsoft-com:office:office">
      

<xsl:include href="/_layouts/xsl/main.xsl"/>
       <xsl:include href="/_layouts/xsl/internal.xsl"/>
       <xsl:template name="FieldRef_body.Status" match="FieldRef[@Name='Status']" mode="body">
       <xsl:param name="thisNode" select="."/>
              <xsl:choose>
                     <xsl:when test="$thisNode/@*[name()=current()/@Name] = 'Completed'">
                           <img src="/Style Library/en-us/Custom/images/image_cancel_33.png" alt="Status: {$thisNode/@Status}"/>
                     </xsl:when>
                     <xsl:when test="$thisNode/@*[name()=current()/@Name] = 'In Progress'">
                           <img src="/Style Library/en-us/Custom/images/image_cancel_32.png" alt="Status: {$thisNode/@Status}"/>
                     </xsl:when>
                     <xsl:otherwise>
                           <img src="/Style Library/en-us/Custom/images/image_cancel_33.png" alt="Status: {$thisNode/@Status}"/>
                     </xsl:otherwise>
              </xsl:choose>
       </xsl:template>
  <xsl:template name="FieldRef_body.TestColumn" ddwrt:dvt_mode="body" match="FieldRef[@Name='TestColumn']" mode="body" ddwrt:ghost="hide">
    <xsl:param name="thisNode" select="."/>
    <xsl:choose>
    <xsl:when test ="$thisNode/@*[name()=current()/@Name]='Yes'">
      Yes is Writen here
    </xsl:when>
    <xsl:otherwise>
      No Is Writen Here
    </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

</xsl:stylesheet>

Insert webpart >>from webpart properities>>miscellaneous >>xsl link insert the link.

apply the  changes and enjoy.

follow my next Post to see how to to Enable FBA with Sharepoint....

4 comments:

Setup dev environment for spfx

So lets setup dev environment for SharePoint Framework abbreviated as SPFX. for an Introduction of What is SPFX and What are the capebiliti...