Category: SharePoint 2013

QR Code Generator Add-In for SharePoint

Using this you can generate QR Code for any text value and URL, this add-in can be add anywhere in you SharePoint Dashboard. Below you can see step by step instructions to develop this SharePoint Add-In. Also the complete project source code is available for download, find the download link at the bottom of this post.


Create new Project in Visual studio using “App for SharePoint” template, here I’m using visual studio 2015 version. In the new project wizard select SharePoint-Hosted and rest you can select based on your requirements. Provider-Hosted application can also be selected.
I’ve added two JS files qr.js and initstrings.js, these JS files are developed by Microsoft. Here I modified those files. Modified files can be found in the project.

Add new “Client Web Part (Host Web)” and select “Create a new app web page for the client web part content”, Edit newly created aspx page which is located in the Pages folder. Add below JS code and HTML. In the JS function I am passing Textbox value and Div name as parameter, JavaScript will generate QR code based on the content in Text box value.

 <script type="text/javascript">
 function generatefn() {
 if ($('#QR_URL').val() != '')
 onLoadQrCode($('#QR_URL').val(), 'DivImage');
 <input type="text" id="QR_URL" value="" style="width:250px" /><input onclick="generatefn()" type="button" id="btnGenerate" value="Generate" />
<div id="DivImage"></div>

Project source Download link


Provider Hosted App For SharePoint Online

Create a new project in visual studio using “App for SharePoint” template, here I’m using visual studio 2015 version. After selected project can see a wizard


Enter SharePoint online URL and select “Provider-Hosted” option next it will ask for user name and password, then wizard will automatically select “SharePoint Online” Option


Next select ASP.Net MVC Web Application in the wizard


Next leave by default selection of “Use Windows Azure Access Control Service (for SharePoint Cloud Apps)” then finish the wizard.

Create new razor view under home view folder


Add new ActionResult in HomeController.cs, below code will retrieve list items from the “Shared Documents” and generates a DataTable. pass the DataTable to View.

 public ActionResult DocumentList()
DataTable dt = new DataTable();
var spContext = SharePointContextProvider.Current.GetSharePointContext(HttpContext);
using (var clientcontext = spContext.CreateUserClientContextForSPHost())
List list = clientcontext.Web.GetList(spContext.SPHostUrl.ToString() + "Shared Documents");
CamlQuery cq = new CamlQuery();
ListItemCollection lic = list.GetItems(cq);
clientcontext.Load(lic, items => items.Take(15).Include(item => item["FileLeafRef"], item => item["Modified"],
item => item["Author"], item => item["FileRef"], item => item["File_x0020_Size"]));
dt.Columns.Add("File Name");
dt.Columns.Add("Modified", typeof(DateTime));

foreach (ListItem item in lic)
DataRow dr = dt.NewRow();
dr["File Name"] = "<a href=\"" + spContext.SPHostUrl.ToString().TrimEnd('/') + item["FileRef"]
+ "\">" + item["FileLeafRef"] + "</a>";
dr["Modified"] = item["Modified"];
dr["Author"] = ((FieldUserValue)item["Author"]).LookupValue;
dr["Size"] = item["File_x0020_Size"];

return View(dt);

Add below code to your new view, code will generate a HTML table using our ModelTable.

<div style="padding:5px 10px 3px 5px">
<table border="1" cellpadding="5" id="my-table">
@foreach (System.Data.DataColumn col in Model.Columns)
@foreach (System.Data.DataRow row in Model.Rows)
@foreach (var cell in row.ItemArray)

View name and action result name should be same else mention the view name in the return statement of action result.

In the AppManifest file select the permissions tab and Scope as list and permission as Read


Run the project in visual studio using play button (F5), now can see SharePoint App Permission approval page, select Documents list then click “Trust it”


After that SharePoint will redirect to our local hosted site, navigate to newly created view page. Page will display SharePoint list data.


You can now compare your retrieved data with the actual SharePoint list.



Using load method we can load other page’s full content or other page’s particular control inner HTML
Ex. Below code will load the MainDiv inner HTML. so inside the load method we need to pass page URL as a parameter if you want load full other source page or if you want to load particular element’s inner HTML then after the URL parameter give one space then enter the selector, here I’ve passed div id as selector (#MainDiv)

<!doctype html>
<html lang="en">
<meta charset="utf-8">
<title>main page</title>
<script src=""></script>
<b>HTML from another page:</b>

<div id="new-projects"></div>

 $("#new-projects").load("anotherpage.html #MainDiv");


This is the other source page


<!doctype html>
<html lang="en">
<title>another page</title>
full HTML contents From another page
<div id="MainDiv">
<h3 style="color:green">div inner html from another page</h3>




each() will take collection from selector and loop the items, this is same as Foreach in c#.
Ex. Below code will loop the all div elements and elements inner HTML append into one p tag.

<!DOCTYPE html>
<title>For Each</title>
<meta charset="utf-8" />
<script src=""></script>
 $(document).ready(function () { $('div').each(function () { $('.ptag').append($(this).html()); }); });

<div>Div 1</div>
<div>Div 2</div>
<div>Div 3</div>
<div>Div 4</div>
<p class="ptag"></p>





The jQuery library provides several techniques for adding animation to a web page, slideToggle will hide and show the targeted element,

Ex, below code will hide and show the all div elements

<!DOCTYPE html>
<title>Show Hide</title>
<meta charset="utf-8" />
<script src=""></script>

<div>Div 1</div>

<div>Div 2</div>

<div>Div 3</div>

<div>Div 4</div>

 <input type="button" id="button" value="click here" />
 $('#button').click(function () { $('div').slideToggle(1500); });


jQuery is a fast, small, and feature-rich JavaScript library. It makes things like HTML document traversal and manipulation, event handling, animation, and Ajax much simpler with an easy-to-use API that works across a multitude of browsers. With a combination of versatility and extensibility, jQuery has changed the way that millions of people write JavaScript.


In jQuery all the codes starts with “jquery” it’s kind of a door, but when we write some long functionality we can see lot of jquery terms in the code. We don’t really want to repeat it everywhere, instead of “jquery” we can use “$” symbol.

jquery == $

Ex, below are two methods that return the same o/p. In this example we write hello world in the browser console (F12 shortcut key to open browser console window) after the page load.

jQuery(function () { console.log('hello world'); });


$(function () { console.log('hello world'); });


We can use CSS3 Selectors for selecting particular tag or set of tags from the page.


<html> <head>
<style> p { background-color: #ffc022; } </style>

 </head> <body>
<div id="container">
<h1 class="Info">Introduction</h1>
<a href="/newpage">move to the new page</a></div>
</body> </html>

In the above HTML code we call div using id, in this method id should be unique,


Values for class be repeated and have multiple space-separated values


Adding and Removing CSS class

After selecting using selector we can add or remove CSS class using below methods

$( "p" ).addClass( "newClass onemoreClass" )

$( "p" ).removeClass( "newClass onemoreClass " )


In the jQuery we can call events in varies ways, in below method we add a function within click event. We have lot of events available in jQuery.

$( "#butSave" ).click(function() {

alert( "Handler for .click() called." );


Another method, here we are attaching the event into a button.

$( "#btnSave" ).on( "click", function() {
alert( "Handler for click called using event attach method " );


Below code will remove the event attachment from button.

$( "#btnSave" ).off( "click", function() {

alert( "Handler for click is removed" );


Below code will fire only once, there is some other event attaching methods also available in jQuery

$( "#btnSave" ).one( "click", function() {

alert( "Handler for click called using event attach method " );


Selecting right version of jQuery

There are two major release available in jQuery, one is 1.x and another is 2.x. Select 1.x version if you want to support your application in IE 8 and the below version browsers,else select 2.x.

Retrieve List data in SharePoint Online (office 365) using JSOM

Create new project using App for SharePoint template under Apps in Visual Studio.


In the new project wizard enter your SharePoint site URL and select SharePoint Hosted and click Finish.


After project created open solution explorer and select App.js file for edit


var context;
var hostweburl;
var appweburl;
var appContextSite;
var list;
var listItems;
var web;

$(document).ready(function () {
 //SP.SOD.executeFunc('sp.js', 'SP.ClientContext', getUrl);

function getUrl() {
 hostweburl = getQueryStringParameter("SPHostUrl");
 appweburl = getQueryStringParameter("SPAppWebUrl");
 hostweburl = decodeURIComponent(hostweburl);
 appweburl = decodeURIComponent(appweburl);
 var scriptbase = hostweburl + "/_layouts/15/";
 $.getScript(scriptbase + "SP.Runtime.js",
 function () {
 $.getScript(scriptbase + "SP.js",
 function () { $.getScript(scriptbase + "SP.RequestExecutor.js", execOperation); }

function execOperation() {
 context = new SP.ClientContext(appweburl);
 var factory =
 new SP.ProxyWebRequestExecutorFactory(
 appContextSite = new SP.AppContextSite(context, hostweburl);
 web = appContextSite.get_web();
 var camlQuery = new SP.CamlQuery();
 list = web.get_lists().getByTitle("Documents");
 listItems= list.getItems(camlQuery);
 context.executeQueryAsync(onSuccess, onFail);
function onSuccess() {
 var listInfo = '';
 var listEnumerator = listItems.getEnumerator();

 listInfo += "


 while (listEnumerator.moveNext()) {
 var listItem = listEnumerator.get_current();
 listInfo += '
<td>' + listItem.get_item('ID') + '</td>

 + '
<td>' + listItem.get_item('FileLeafRef') + '</td>

 + '</tr>


 listInfo += '</table>



// This function is executed if the above call fails
function onFail(sender, args) {
function getQueryStringParameter(paramToRetrieve) {
 var params =
 for (var i = 0; i < params.length; i = i + 1) {
 var singleParam = params[i].split("=");
 if (singleParam[0] == paramToRetrieve)
 return singleParam[1];

Code flow

Step 1: getUrl() function called when page get ready

Step 2: getUrl() function is collect host and app web URLs from query strings and adds some JavaScript files for reference and call execOperation() function

Step 3: execOperation() function is create app sire context and request Documents list data as asynchronous call

Step 4: onSuccess() event will receive list data, data written as html table and table added into div control

Document library in SharePoint online
Document library in SharePoint online
App view
App view

Continue reading “Retrieve List data in SharePoint Online (office 365) using JSOM”