Showing posts with label JSOM. Show all posts
Showing posts with label JSOM. Show all posts

Tuesday, October 4, 2016

JSOM basic operations with SharePoint Hosted Apps

In the following post I will be showing how I had created basic JSOM operations such as create, insert, update, delete operations on SharePoint list by using SharePoint hosted addins. Using Visual Studio, I had created new project then select the App for SharePoint 2013 as template.


In the next window, In the hosting type I had selected SharePoint Hosted App.


After the project is created, Project structure looks like as show on right.

There are several parts in the projects such as Pages, Scripts, List Instance, Images.

I had created list called 'CountryInfo' that would contain sample country data,

In the Pages section I had added CRUD.aspx. It is the main page for the application.

In the Scripts folder I had various javascript files such as app.js, crud.js, ListManager.js, ListItemManager.js.

CRUD.aspx - The file contains DIV that would display the results of various operations and also there are 4 buttons for CRUD operations for performming respective task. The references for below js libraries are added in PlaceHolderAdditionalPageHead  section.

CRUD.js - The file contains logic for JSOM CRUD operations.

ListManager.js - The file contains logic to fetch or create list.

ListItemManager.js -  The file contains logic for updating, creating list item.

App.js - The files contains logic to log and display the error to div. CRUD.aspx
















Creating SharePoint List 'CountryInfo'
Right on project and then add new item to create a new list.



I had created new columns to the 'CountryInfo' list and added sample data. Below is element.xml file for SharePoint list.





Next, CRUD.aspx has DIV and 4 radio buttons. I had added all the js references before we start working on each of the js files in PlaceHolderAdditionalPageHead section. Below is the complete designer code for CRUD.aspx


In CRUD.js Instead of polluting the global namespace I am creating/using namespace called JSOM(window.JSOM || {} specifically says that if there is namespace with JSOM we are going to use it or else it creates a new one for us ) and also we create class under it called JSOM.Crud. Each of the buttons are assigned with click handlers.



Reading SharePoint list data using JSOM in SharePoint Hosted Apps:

When the first button 'Read' is clicked below code queries SharePoint list and displays results on SharePoint Hosted App.




Result



Inserting SharePoint list data using JSOM in SharePoint Hosted Apps

In CRUD.js on button click insercall is executed and below is source code. Before inserting any item it initially checks for whether valid list is present or else it will create a list. This process is performed in separate JS file called ListManager.Js

Since 'executeQueryAsync' is async process which executes the statements some time in future. In our case based on result of fetch operation in ListManager.js we have to execute further statement in CRUD.js so I had created deffered object and returned the promise. Based on the promise success/failure calls are executed in in CRUD.js

CRUD.js:

ListManager.js



ListItemManager.js



Result:


Updating SharePoint list data using JSOM in SharePoint Hosted Apps:
On update button click, I had called update function that takes list id to update the data into SharePoint list. I had used ListItemManager.js file to update the data provided from crud.js file. Upon return promise I  am executing the further success and failure functions.

crud.js



ListItemManager.js



Results:

Before and after update updating ID with 2


Deleting SharePoint list data using JSOM in SharePoint Hosted Apps:

When delete button is clicked app prompts for list item id to be deleted. Below is code.


Result
App.js has below code for logging the output to DIV element.


Complete Source Code: Link

Monday, September 19, 2016

Validating SharePoint list data by using PreSaveAction involving JSOM Asynchronous call

In the previous post I had shared validating SharePoint list data that invlolved JSOM asynchronous call by overriding the PreSaveAction . The same requirement can also be accomplished without over riding the presaveaction.

Approach would be when the call is succeded or failed I am creating delegate on executeQueryAsync call with call back. On success we will posting the data manually to SharePoint list or on failure I willnt post any data to SharePoint list by returning false.



Output


Friday, September 16, 2016

Validating SharePoint list data by overriding PreSaveAction involving JSOM Asynchronous call

SharePoint supports ExecuteQueryAsync and when we call ExecuteQueryAsync it executes the object model code declared and carries on and returns the ouput at some point in future without waiting for subsequent code. Most of the time we would expect our code to act on result of call.

This is challenging particular when we use it with presaveaction that would expect true or false and ExecuteQueryAsync returns output infuture. PreSaveAction in SharePoint does validation before submitting an item to SharePoint list.

Solution: By using jquery I had added custom OnClick fumction to the existing Save button. The custom function does the validation and once it is completed I had called orginial Save function to complete the saving process.

In below code I am using JSOM ExecuteQueryAsync to fetch status of SharePoint list item field. Based on status of field and I am comparing it other field as part of business validation. During the validation I decide whether to save the item or not. If the item is valid I am calling orginal save function or else just an alert will be displayed to user that there is problem with request. I had used this code in editform.aspx.




Output