Knockout.js with Sharepoint 2013 App – Part III

In my previous post, we saw how to add items in a SP list using KO. This post will focus on removing items from a list. The code snippet at the end combines all the three posts. Using it; you can display, add and remove items from list. Besides that we send only updated (i.e. added or removed) data to server side. Going on the same lines, we can implement the “edit” functionality also. Right now, I am skipping it.

For reference, the other two posts are:

  1. Part I – Displaying Data from SP list. 
  2. Part II – Adding Data to SP list.
  3. Part III – Removing Data from SP list. (this one)

To add remove functionality, a “Remove this” link is added after every row in the table. So, the html looks like:


$root.RemoveCountries will update the view model. Here $root is the main view model in the topmost/parent context. It will update the whole table by removing the current row.

“Id” column of sharepoint list is also included in the “CountryList” class to delete items by id. For newly created rows the id is assigned to ‘0’. After every update to the server, the “changedCountries” array is emptied.

The final result will look like,


Now, the final collaborated code is:


Note: In sharepoint hosted app, we do not need SP.RequestExecutor.js to communicate with host web that means cross-domain calls to host web is allowed without this javascript file. The code is updated for this.

Summary: KO is an amazing library that makes life easier by segregating the display logic with the business logic. Using it in SP, makes the js code neat and simple to understand. The only flip side is, if there are changes at the server side, then UI is not automatically updated until a refresh is performed. Regardless of this, KO is a library we all should learn.

P.S.: This example is by no means perfect or bug free. There are few loopholes like:

  1. User can edit the existing items but this is not handled in the code.
  2. After adding a new item to SP list the view model is not updated unless user do a refresh.
  3. Only 20 items are fetched and no pagination is implemented.

But the code is for demo purpose only. You can extend it in any way you want. Hope it helps!!

Another good blogs on KO (also referenced here):

  1. Knockout.js Simplified
  2. Using Knockout.js in a sharepoint context

Knockout.js with Sharepoint 2013 App – Part II

In my previous post, we saw how to display items from a sharepoint list using KO. In this post, we will see how to add items in the list. This demo is in continuation with the previous one.

For your reference, the posts in this series are:

  1. Part I – Displaying Data from SP list. 
  2. Part II – Adding Data to SP list. (this one)
  3. Part III – Removing Data from SP list.

First, lets go through the changes made in the previous code for incorporating this functionality.

HTML Changes:

  1. A button “Add State” is added. This button updates the view model and add a new country-state in the existing table. It does not update the SP list.
  2. Another button “Submit to Sharepoint” is added. This button actually updates the data in the sharepoint list.
  3. Now the country and states are displayed in text box so that user can add new ones. Edit functionality is not included in this post.
  4. Two divs are added for displaying success and failure messages.

CSS Changes:

  1. styling is added for success and failure messages. Not much changes are done here.

Javascript Changes:

  1. Another property “IsUpdated” is added in  the “CountryList” class. This property is used to keep tab of newly created items in the view model. Using this, we will send only changed items to the server and not the complete list of data.
  2. In the view model, “UpdateSPList” function is added. This function will be called on click of “Submit to SP” button.
  3. “changedCountries” array holds all the newly added items. “emptyValues” is a boolean variable which maintains if any of the country name or state name is blank. If yes, then that entry is not sent to SP and a message is displayed to the user.
  4. “SubmitDataToSP” is a function which actually uses sharepoint CSOM and adds new items in the list.
  5. On click of “Add State” button a function “AddCountries” of view model is called. The parameters passed are $data (which is the current item inside a foreach loop), country name, state name and true as the value of “IsUpdated” property. For more details on ‘click’ event, check this link.
  6. Similarly on click of “Submit to SP” we call, “UpdateSPList” method of the view model.

So, now the complete code is as follows:

The result page will look like:



Please note that this might not be the completely optimized way to achieve this functionality. The goal here is to demo use of KO with sharepoint. In the next post, we will modify this code further to achieve delete functionality.

Hope this helps!!

Knockout.js with Sharepoint 2013 App – Part I

Knockout.js is an amazing javascript library for binding data and generating html at run time. It uses MVVM pattern that is Model-View-ViewModel pattern. This post assumes that you have a basic understanding of knockout and sharepoint client side object model. If not, then go through the amazing set of tutorials present in knockout (KO) site.

I was recently introduced to KO and found it amazing. But I didn’t find much on how to implement it in sharepoint context. So, I decided to look into it and  share with you all my learnings of KO with sharepoint. This post is dedicated to just displaying data from a list in a page using KO. Further posts will focus on adding and removing data too. At the end, we will see if KO is actually a good option for sharepoint or not. The post in the series will be:

  1. Part I – Displaying Data from SP list. (this one)
  2. Part II – Adding Data to SP list.
  3. Part III – Removing Data from SP list.

To start I am using sharepoint 2013 app model. A sharepoint hosted app is created for displaying list’s data in html page present in app context. The structure of the list is:

List for knockout Demo

This is a simple list which contains names of country and their states. Using KO we will display this in a table structure. KO makes the client side code simple and easier to use. HTML, CSS and JS all files are separate and manageable.

So the code for displaying this data is :

The output page will look like:

KO output 1

This is how we use KO in sharepoint. Notice the object oriented structure of “App.js”.  The code is relatively very clean and easy to understand. In next post we will add items in a list using KO. Hope this helps.