SharepointPlus 4.0

Be notified on new release

Description

SharepointPlus ($SP) is a JavaScript API for Sharepoint. This library offers some extended features for SharePoint entirely on client side (requires no server install). $SP will simplify your interactions with the Sharepoint Web Services and will help you to deal with the List Forms.

Other JavaScript library like this one are often complex, with few or no example. With SharepointPlus it's easy (like the SQL syntax) and you'll find examples for each method.

I've developed this API for my needs during my job at Dell and we thought it could be useful for the community too, so here it is !

Sharepoint Support

Sharepoint 2007 : SharepointPlus v3.0.4 is the last release that I have tested with Sharepoint 2007 -- after this version I cannot assure the retro-compatibility
Sharepoint 2010 : Compatible since SharepointPlus v3.0.4
Sharepoint 2013 : Compatible since SharepointPlus v3.13
Sharepoint Online : I have no plan to test SharepointPlus on Sharepoint Online, but it should be compatible.

Browser Support

IE8+, and all modern browsers (Firefox, Chrome, ....)

Quick Start

Requirements

Since v3.13, there is no more need of jQuery to work! For older versions, make sure to include jQuery.

If you don't use jQuery in your project, then SharepointPlus will need to use nanoajax (811o minified and zipped)

The basics

Just add two lines: one to call either jQuery or nanoajax, and one for SharepointPlus:

// if you don't use jQuery, then you need to call 'nanoajax':
<script type="text/javascript" src="nanoajax.min.js"></script>
<script type="text/javascript" src="sharepointplus-4.0.min.js"></script>

You may also want to use a CDN: SharepointPlus on JSDelivr

Lists

Sharepoint provides different Web Services that permits to do several tasks.

Let's see some examples of what you can do with SharepointPlus regarding the Lists interaction:

// Update all items with an "Amount" value bigger than 1000
$SP().list('My List Name').update({
  Title:"Too expensive"
}, {
  where:"Amount > 1000",
  success:function(items) { alert(items.length+" items updated!");
});

// Get all items with "Requestor" as the current user and with "Default Color" is "pink"
$SP().list('ListName').get({
  fields:"Title,Size",
  where:"Requestor = '[Me]' AND Default_x0020_Color = 'pink'",
  orderby:"Size DESC"
}, function(data) {
  var html = "<ul>";
  for (var i=data.length; i--;)
    html += "<li>Model '"+data[i].getAttribute("Title")+"' (size: "+data[i].getAttribute("Default_x0020_Color")+")<li>";
  $('#list').append(html+'</ul>');
});

Forms

SharepointPlus provides few functions to interact with NewForm.aspx and EditForm.aspx of a list:

// hide some fields like "Title", "Next Action"
$SP().formfields("Title,Next Action").row().hide(); // row() returns a jQuery object (or a HTMLNodeElement) that represents the <TR> element

// check if all mandatory fields have a value, and also "Title"
$SP().formfields("Title",{mandatory:true}).each(function() {
  if (this.val() == "") {
    alert(this.name+" is empty!");
    return false;
  }
});

Utilities

SharepointPlus has also some useful functions:

// create an Excel file
var table="<table><tr><th>Column 1</th><th>Column 2</th></tr><tr><td>First cell</td><td>Other cell</td></tr></body>";
$SP().createFile({content:table,library:"Shared Documents",filename:"myfile.xls",after:function() { alert("File created!"); }});

// take a number and return it as a currency amount
$SP().toCurrency(1500000,2,'$'); // --> $1,500,000.00

// when you have to deal with a date from a .get() ...
$SP().list('My Calendar List').get({fields:"Meeting_x0020_Date"},function(data) {
  for (var i=data.length; i--;) console.log($SP().toDate(data[i].getAttribute("Meeting_x0020_Date")).getFullYear());
});
// ... or to a .add()/.update()
var nextMeeting = new Date("5/May/2015");
$SP().list('ListName').add({Title:"Next Meeting",Meeting_x0020_Date:$SP.toSPDate(nextMeeting)});

Promise

SharepointPlus is partially compatible with Promise. Check the function's documentation to verify before using it.

Example:

$SP().list("Audit").get({
  fields:"ID",
  view:"All Items",
  paging:true,
  rowlimit:1000,
  progress:function(n) { console.log(n+" items already loaded...") }
}).then(function(data) {
  data.forEach(function(d) { console.log(d.getAttribute("ID")) })
}, function(err) {
  console.log("ERR => ",err)
})

Attention, because Promise is not available on all browsers, you'll need to either use a polyfill for old browsers, or to use jQuery... or to use the normal callback system!

NodeJS

Since SharepointPlus 4.0 you can use it as a node module for your server side applications.

npm install sharepointplus

Find below some examples:

// the credentials depend of your authentication system
// see: https://github.com/s-KaiNet/node-sp-auth
const credentials = {
  username:'my_username',
  password:'mypassword',
  domain:'mydomain'
};
// you can also define a proxy
const proxyweb = "http://" + credentials.domain + "%5C" + credentials.username + ":" + credentials.password + "@proxy:80";

const $SP = require('sharepointplus');
const sp = $SP().proxy(proxyweb).auth(credentials);

// you can then use SharepointPlus normally
// e.g.: sp.list("Hello", "http://my.sharepoint.site/").get(...);