In last two post we discussed basics on JSLink. Now lets look bit deeper. In this post I will illustrate; what are the fields we can override and use in template. I will give you examples for each and every property. Below are the properties that we can use to override in our custom template.

Header

Footer

View

Body

Group

Item

Fields

OnPreRender

OnPostRender

I have created an announcement list instance in order to illustrate the examples. My list instance displays as follows. Please note that I have grouped the list items by Created.

I have linked my JavaScript file for the above list instance. Now lets get into the examples.

Header

Overrides the Header of the list instance. The header area is described by the below image.

Results

Fields

Visit my first blog post and see the example. You can understand how to override the fields. If you want to retrieve the actual value of a field you can retrieve by “<#= ctx.CurrentItem.FieldName#>” or by ctx.CurrentItem.FieldName.

Results

`

Conclusion

So the basics are covered and if you are familiar with JavaScript and CSOM you can do more on Client side rendering with JSLink. In future posts I will show some examples how you can use client side rendering.

Thursday, September 11, 2014

Introduction

In my last post I described how to startup with Client Side Rendering using JSLink with basic example. In this post I will describe “What are the places you can deploy JavaScript files in order to use in SharePoint development” and “How can we use rendering; in order to customize list forms”.

What are the places you can deploy JavaScript files in order to use in SharePoint development?

In my last post I had used layouts folder which is inside the 15 hive. But there are more options where we can deploy JavaScript files in order to use them in JSLink property. So these option can be useful for the developers who want to develop SharePoint hosted apps and who don’t have the access to local file system. JSLink property accepts below SharePoint tokens.

~site – The URL of the current website. (Recommended to use in SharePoint hosted apps development)

~sitecollection – The URL of the parent site collection of the current website.

~layouts – The URL of the Layouts virtual folder for the current website. You don’t have to worry about 14 hive or 15 hive.

~sitecollectionlayouts – The URL of Layouts folder in the current site collection (/sites/spsite/_layouts/15)

~sitelayouts – The URL of Layouts folder in the current site (/spsite/spweb/_layouts/15)

If you want to use multiple JavaScript file on a single JSLink property you can use “|” symbol.

Eg. ~layouts/folder001/test.js | ~layouts/folder002/test.js

How can we use rendering in order to customize list forms?

In the first post I described how to customize the default view form. In this post I will describe how to customize New form, Edit form and the Display form by changing the scope in JavaScript. I will show how to customize the New form and you can use the same for other forms as well.

Solution

(function () {

// Initialize the variables for overrides objects

var overrideCtx = {};

overrideCtx.Templates = {};

overrideCtx.BaseViewID = "NewForm";

overrideCtx.ListTemplateType = "10000";

/*

* Using the Fields override leaves the rest of the rendering intact, but

* allows control over one or more specific fields in the existing view

Wednesday, September 3, 2014

Introduction

There was a requirement to create a discussion board list definition through visual studio. So that our client can be able to create list instance with some customization.

Problem Background

We tried to create content type by inheriting Discussion and then created a custom list definition using created content type. But it was not same as the out of the box discussion board – We couldn’t get the reply page and the views as in the ootb.

Solution

Create a new SharePoint solution

Add a content type inheriting from Discussion

Right Click on the project –> Add –> New Item –> Content Type –> Name it (I have named it as DisBoardCT) –> Click Add –> Select Discussion from Content type setting page

Monday, September 1, 2014

Introduction

In SharePoint 2010, we use XSLT in order to customize the style of search results and view of list and list items. In SharePoint 2013 and for SharePoint Online it is recommended to use Client side rendering. Obliviously CSR is through JavaScript (of course; including CSS and HTML as well). Again, if you are familiar with XSLT you can still use it in SharePoint 2013 but note that XSLT falls under server side rendering.

In this post I will just describe how to override a column field with our custom value and in future I will go in details with styling and all.

Advantages of Client Side rendering over using XSLT

Avoid unnecessary loads on server: JavaScript, HTML and CSS render on client browser and they avoid unnecessary loads on the server.

Easy to Debug: We use JavaScript for client side render. Modern browser supports debugging facility which XSLT is not providing.

More Libraries: We can use other JavaScript libraries as we needed (such as JQuery)

Easy to learn: SharePoint developers are becoming comfortable with JavaScript because of the trend of SharePoint hosted apps. So most developers are comfortable with JavaScript and feel easier to learn JavaScript than XSLT.

Flexibility: We can override particular fields, header, footer or the entire view of the list as we needed.

Disadvantages

Depends on client browser: If a user use old version of browser it may take long time to process the view; and if the user has disabled JavaScript or blocked JavaScript the rendering will not work. In case we use XSLT will work.

SEO Problem: Crawlers may not be able to understand the content generated by JavaScript.

Now lets cover some basic topics which we need

How to add JSLink to a List

Solution 01 – Through the browser user interface

Go to the List.

Click on edit page

Click on Edit Web Part

At the bottom of the web part properties control there is a category called “Miscellaneous”. Expand it. At the bottom you can find a text box to add JS Link and there you can define the url for JS Link.

Solution 02 – Through Visual Studio

If you are trying to add a SharePoint list through visual studio declaratively this will be useful.

Click on the schema.xml of your list

Search for JSLink and you will see something like this.

Now you can give your own path of the JavaScript file.

Basic Example

Now let’s create a basic JavaScript which overrides the value of our Body column of a announcement list.

Steps:

Create an announcement list.

Change the default view which displays the Body.

Modify view –> Select Body under the column name and click OK.

Now create a JavaScript file in side layouts folder inside 15 hive.

Usually the path will be C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\TEMPLATE\LAYOUTS\

In my case I created the JavaScript file as cutombody.js

Now copy and paste the below code

(function () {

// Initialize the variables for overrides objects

var overrideCtx = {};

overrideCtx.Templates = {};

/*

* Using the Fields override leaves the rest of the rendering intact, but

* allows control over one or more specific fields in the existing view

In the above code overrideCtx variables holds current context of the list item. overrideCtx.Templates.Fields enables to access the individual list field. SPClientTemplates.TemplateManager.RegisterTemplateOverrides function registers our customized template; so that our list can use it.

Here we are overriding the Body (the internal name of the field) with the value “Our Custom Value”. Here the view is the scope. The scope can be DisplayForm, View, EditForm, NewForm.

Now save the JavaScript and give the JSLink as I have mentioned above under Solution 01. Your url will can be something like this. /_layouts/15/cutombody.js

Now our Announcement list will only display “Our Custom Value” in body field.

Conclusion

Stay Calm I will update more post with different examples with the code in upcoming days.

I have attached a Visual Studio solution file with a custom announcement list and have added the the list to XsltListViewWebPart and define the JSLink for the web part as well as for the list through the schema.xml. Please download it and make use of it.