RSM
RSM

Reputation: 15108

Custom Field definition

On SharePoint 2013 I am having troubles with a field on a list which i define through a schema.xml file. We upgraded from 2007 where it works fine, but after upgrading to 2013, it no longer works when creating list from new.

Historically:

The field is a custom field that outputs an image wrapped with a link which is dynamic using javascript. this is defined using display pattern and cdata.

We defined the field as follows in our schema.xml file:

<Field ID="{A54A4AE0-CA79-47b0-819E-32DC1B3F5AFB}" ReadOnly="TRUE" Type="Computed" Name="Book" Sortable="FALSE" Filterable="FALSE" DisplayName="Book" ClassInfo="Icon" AuthoringInfo="(link to book item)" SourceID="http://schemas.microsoft.com/sharepoint/v3" StaticName="Book" FromBaseType="TRUE">
        <DisplayPattern>
          <HTML><![CDATA[<a href="javascript:" OnClick="javascript:this.href=L_Menu_BaseUrl + '/Lists/Bookings/NewForm.aspx?Session_x0020_Name=]]></HTML>
          <Column Name="ID" />
          <HTML><![CDATA[';GoToLink(this);return false;" target="_self">]]></HTML>
          <HTML><![CDATA[<img border="0" alt="]]></HTML>
          <HTML>Book</HTML>
          <HTML><![CDATA[" src="/_layouts/images/Book.GIF">]]></HTML>
          <HTML><![CDATA[</a>]]></HTML>
        </DisplayPattern>
      </Field>

When initiating the list from new with this schema it no longer works, it creates the book field, but its just blank.

What I have tried:

After research, i learnt that custom fields are now meant to be defined with an xsl file in:

C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\TEMPLATE\LAYOUTS\XSL

So i created an xls file named: fldtypes_Book.xsl

of which the contents look like below (at the moment im just trying to output simple text to get it working):

<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:template match="FieldRef[@ID='A54A4AE0-CA79-47b0-819E-32DC1B3F5AFB']" mode="Computed_body"  priority="1">
    <xsl:param name="thisNode" select="."/>
    <span>Hello.</span>      
  </xsl:template>
</xsl:stylesheet>

Again this isnt working, it just outputs the field but the field is empty.

I know for a fact the the field is hooking up with the xsl file because if i input some wrong tags or random mess into the file the list breaks.

24/08/2015 - update

After research I have found this:

https://msdn.microsoft.com/en-us/library/office/jj220061.aspx

And tried to upload the js file to the master page and link to it on the list web part settings, but nothing happens, the contents of my js file:

(function () {

//   Initialize the variables for overrides objects
    var overrideCtx = {};
    overrideCtx.Templates = {};

//  alert("Override call worked");

//  Use BaseViewID and ListTemplateType to narrow focus/scope on 
//  which web parts on the page are affected
//  overrideCtx.BaseViewID = 1;
//  overrideCtx.ListTemplateType = 100;

    /*
     * Using the Fields override leaves the rest of the rendering intact, but 
     * allows control over one or more specific fields in the existing view
     */
    overrideCtx.Templates.Fields = {
        'Book': { 'View' : 'Hello' }
    };

    /*
     * Register the template overrides.
     */
    SPClientTemplates.TemplateManager.RegisterTemplateOverrides(overrideCtx);
})();

Any help greatly appreciated.

Upvotes: 3

Views: 583

Answers (2)

Vadim Gremyachev
Vadim Gremyachev

Reputation: 59328

In SharePoint 2013 was introduced a Client Side Rendering (aka CSR) which is used for rendering list views,list forms and search results. Unlike previous SharePoint rendering systems (XSLT in 2010 and CAML in 2007), CSR is client-side rendering mode and the default one in SharePoint 2013. To get acquainted with CSR i would recommend the following article: SharePoint 2013 Client Side Rendering: List Views.

How to customize field rendering in List View in SharePoint 2013 via CSR

The following example demonstrates how to customize LinkTitle column rendering in Phones list.

JavaScript template:

SP.SOD.executeFunc("clienttemplates.js", "SPClientTemplates", function() {

  SPClientTemplates.TemplateManager.RegisterTemplateOverrides({
     Templates: {
        Fields: {
           'LinkTitle': {'View': phoneIconRenderer}
        }
     }
  });

});


function phoneIconRenderer(ctx) {
    var item = ctx.CurrentItem;
    return '<img src="/SiteAssets/' +  item.Title + '.png" />';  
}

How to apply the changes

There are at least two options available:

  1. Use JSLink property
  2. Place JavaScript template on the page where list view resides

Option 1:

  1. Save the specified template as a file
  2. Upload the file into, for example, Site Assets library
  3. Specify JS Link property for List View web part Example: ~site collection/SiteAssets/Phones.js

Option 2:

Here is how to apply the changes using the second option:

  1. Switch the page into edit mode
  2. Add Script Editor webpart right below the list view web part.
  3. Put the specified code by enclosing it using script tag into the Script Editor, for example: <script type="text/javascript">{Template JS code goes here}</script>
  4. Save the page

Result

enter image description here

Upvotes: 2

Olga Golubeva
Olga Golubeva

Reputation: 307

Try to put FieldRefs of used fields before DisplayPattern definition (also, ensure that these fields were defined too), like this:

<!-- This is your computed field -->
<Field ID="{GUID}" Name="Book" DisplayName="Book" Type="Computed" Required="FALSE" Group="Custom Site Columns">
  <!--Insert these refs -->
  <FieldRefs>
    <FieldRef Name="BookID"/>
  </FieldRefs>
    <DisplayPattern>
      <HTML><![CDATA[<a href="javascript:" OnClick="javascript:this.href=L_Menu_BaseUrl + '/Lists/Bookings/NewForm.aspx?Session_x0020_Name=]]></HTML>
      <Column Name="BookID" />
      <HTML><![CDATA[';GoToLink(this);return false;" target="_self">]]></HTML>
      <HTML><![CDATA[<img border="0" alt="]]></HTML>
      <HTML>Book</HTML>
      <HTML><![CDATA[" src="/_layouts/images/Book.GIF">]]></HTML>
      <HTML><![CDATA[</a>]]></HTML>
    </DisplayPattern> 
</Field>

<!--This is refered field -->
<Field ID="{GUID}" Name ="BookID" DisplayName="BookID" Type="Text" Group="Custom Site Columns"></Field>

And don't forget to re-create your List, this will help sharepoint to "see" your updates. Good luck!

Upvotes: 0

Related Questions