Find an user based on a part of his name
$SP().addressbook("john", {limit:25}, function(people) {
for (var i=0; i < people.length; i++) {
for (var j=0; j < people[i].length; j++) console.log(people[i][j]+" = "+people[i][people[i][j]]);
}
});
Checkin a file
$SP().checkin({
destination:"http://mysite/Shared Documents/myfile.txt",
comments:"Automatic check in with SharepointPlus",
after:function() { alert("Done"); }
});
clean a string returned by a GET (remove ";#" and "string;#" and null becomes "")
$SP().cleanResult("15;#Paul"); // -> "Paul"
$SP().cleanResult("string;#Paul"); // -> "Paul"
$SP().cleanResult("string;#"); // -> ""
$SP().cleanResult(";#Paul;#Jacques;#Aymeric;#"); // -> "Paul;Jacques;Aymeric"
$SP().cleanResult(";#Paul;#Jacques;#Aymeric;#", ", "); // -> "Paul, Jacques, Aymeric"
Close the last modal dialog
// if the user use the cross to close the modal, then `dialogResult` equals to 0 in the callback
// but you can trigger the close of the modal and pass anything you want
$SP().showModalDialog({
title:"Hello World",
html:'This is an example. Click one of the buttons.
',
callback:function(res) {
alert(res)
}
})
Create a file and save it to a Document library
// create a text document
$SP().createFile({
content:"Hello World!",
filename:"SubFolder/myfile.txt",
library:"Shared Document",
fields:{
"Title":"My Document",
"File_x0020_Description":"This is my file!"
},
after:function(fileURL, error) {
if (error) alert("Error: "+error)
else alert("File created at " + fileURL); // fileURL -> http://mysite/Shared Documents/SubFolder/myfile.txt
}
});
// you can remove "library" if you use $SP().list()
$SP().list("Shared Document").createFile({
content:"Hello World!",
filename:"SubFolder/myfile.txt",
fields:{
"Title":"My Document",
"File_x0020_Description":"This is my file!"
},
after:function(fileURL, error) {
if (error) alert("Error: "+error)
else alert("File created at " + fileURL); // fileURL -> http://mysite/Shared Documents/SubFolder/myfile.txt
}
})
// we can also create an Excel file
// a good way to export some data to Excel
$SP().createFile({
content:"<table><tr><th>Column A</th><th>Column B</th></tr><tr><td>Hello</td><td>World!</td></tr></table>",
filename:"myfile.xls",
library:"Excel Exports",
after:function(fileURL) {
window.location.href=fileURL;
}
});
// You can use https://github.com/Aymkdn/FileToDataURI if you want to be able to read a local file
// and then upload it to a document library, via Javascript/Flash
// We'll use "encoded:true" to say our content is already a base64 string
$SP().createFile({
content:"U2hhcmVwb2ludFBsdXMgUm9ja3Mh",
encoded:true,
filename:"Demo/HelloWorld.txt",
library:"Documents",
url:"http://my.other.site/website/"
});
// NOTE: in some cases the files are automatically checked out, so you have to use $SP().checkin()
Create a folter in a Document library
// create a folder called "first" at the root of the Shared Documents library
// the result should be "http://mysite/Shared Documents/first/"
$SP().createFolder({
path:"first",
library:"Shared Documents",
url:"http://mysite/",
after:function() { alert("Folder created!"); }
});
// create a folder called "second" under "first"
// the result should be "http://mysite/Shared Documents/first/second/"
// if "first" doesn't exist then it will be created
$SP().createFolder({
path:"first/second",
library:"Shared Documents",
after:function() { alert("Folder created!"); }
});
// Note: To delete a folder you can use $SP().list().remove()
Permits to decode a Base 64 string
Find the distribution lists where the specified user is member of
$SP().distributionLists("mydomain\\john_doe",{url:"http://my.si.te/subdir/"}, function(mailing) {
for (var i=0; i < mailing.length; i++) console.log(mailing[i]); // -> {SourceReference: "cn=listname,ou=distribution lists,ou=rainbow,dc=com", DisplayName:"listname", MailNickname:"List Name", Url:"mailto:listname@rainbow.com"}
});
Permits to encode in Base 64
Split the ID and Value
$SP().getLookup("328;#Foo"); // --> {id:328, value:"Foo"}
Retrieve the modal object for a special modalDialog
var modal = $SP().getModalDialog("MyModal");
$SP().closeModalDialog(modal);
Find the User ID, work email, and preferred name for the specified username (this is useful because of the User ID that can then be used for filtering a list)
$SP().getUserInfo("domain\\john_doe",{url:"http://my.si.te/subdir/"}, function(info) {
if (typeof info === "string") {
alert("Error:"+info); // there was a problem so we show it
} else
alert("User ID = "+info.ID)
});
Find the members of a Sharepoint group
$SP().groupMembers("my group", function(members) {
for (var i=0; i < members.length; i++) console.log(members[i]); // -> {ID:"1234", Name:"Doe, John", LoginName:"mydomain\john_doe", Email:"john_doe@rainbow.com"}
});
Find if the user is member of the Sharepoint group
$SP().isMember({user:"mydomain\\john_doe",group:"my group",url:"http://my.site.com/"}, function(isMember) {
if (isMember) alert("OK !")
});
Get the lists from the site (for each list we'll have "ID", "Name", "Description", "Url")
$SP().lists(function(list) {
for (var i=0; i<list.length; i++) console.log("List #"+i+": "+list[i]['Name']);
});
Permits to notify the user using the SP.UI.Notify.addNotification system
$SP().notify('Processing the data...', {sticky:true}); // the notification will stay on the screen until we remove it
$SP().notify('All done!', {overrideAll:true}); // the "Processing the data..." is removed from the screen and a 5 seconds message says "All done!"
$SP().notify('Please wait 10 seconds...', {
name:"My 10 seconds notification",
timeout:10,
after:function(name,afterDelay) {
if (afterDelay) alert("OK, you waited during 10 seconds!")
else alert("Something just removed this notification called '"+name+"'' before the timeout :-(")
}
})
Use a WHERE sentence to transform it into a CAML Syntax sentence
$SP().parse('ContentType = "My Content Type" OR Description <> null AND Fiscal_x0020_Week >= 43 AND Result_x0020_Date < "2012-02-03"');
// -> return <And><And><Or><Eq><FieldRef Name="ContentType" /><Value Type="Text">My Content Type</Value></Eq><IsNotNull><FieldRef Name="Description" /></IsNotNull></Or><Geq><FieldRef Name="Fiscal_x0020_Week" /><Value Type="Number">43</Value></Geq></And><Lt><FieldRef Name="Result_x0020_Date" /><Value Type="DateTime">2012-02-03</Value></Lt></And>
// available operators :
// "<" : less than
// "<=" : less than or equal to
// ">" : greater than
// ">=" : greater than or equal to
// "<>" : different
// "~=" : this must be only used when comparing to a number that represents the User ID (e.g. 'User ~= 328') - that permits to query a list with too many items but with the User column that is indexed
// " AND "
// " OR "
// " LIKE " : for example 'Title LIKE "foo"' will return "foobar" "foo" "barfoobar" "barfoo" and so on
// " IN " : for example 'Location IN ["Los Angeles","San Francisco","New York"]', equivalent to 'Location = "Los Angeles" OR Location = "San Francisco" OR Location = "New York"' — SP2013 limits each IN to 60 items. If you want to check Lookup IDs instead of text you can use '~' followed by the ID, for example 'Location IN ["~23", "~26", "~30"]'
// special words:
// '[Me]' : for the current user
// '[Today]' : to use the today date
// '[Today+X]' : to use today + X days
// Null : for the Null value
// TRUE : for the Yes/No columns
// FALSE : for the Yes/No columns
// in the below example, on the "&" will be escaped
var bar="Bob & Marley";
var foo="O'Conney";
$SP().parse('Bar = "'+bar+'" AND Foo = "'+foo+'"'); // -> <And><Eq><FieldRef Name="Bar" /><Value Type="Text">Bob & Marley</Value></Eq><Eq><FieldRef Name="Foo" /><Value Type="Text">O'Conney</Value></Eq></And>
$SP().parse("Bar = '"+bar+"' AND Foo = '"+foo+"'"); // don't put the simple and double quotes this way because it'll cause an issue with O'Conney
Find the user details like manager, email, colleagues, ...
$SP().people("john_doe",{url:"http://my.si.te/subdir/"}, function(people) {
if (typeof people === "string") {
alert(people); // there was a problem so we prompt it
} else
for (var i=0; i < people.length; i++) console.log(people[i]+" = "+people[people[i]]);
});
Permits to use a plugin
$SP().plugin('test',{message:"This is a test !"})
Provide the Date Format based on the user regional settings (YYYY for 4-digits Year, YY for 2-digits day, MM for 2-digits Month, M for 1-digit Month, DD for 2-digits day, D for 1-digit day) -- it's using the DatePicker iFrame (so an AJAX request)
// you'll typically need that info when parsing a date from a Date Picker field from a form
// we suppose here you're using momentjs
// eg. we want to verify start date is before end date
var startDate = $SP().formfields("Start Date").val();
var endDate = $SP().formfields("End Date").val();
$SP().regionalDateFormat(function(dateFormat) {
// if the user settings are on French, then dateFormat = "DD/MM/YYYY"
if (moment(startDate, dateFormat).isAfter(moment(endDate, dateFormat))) {
alert("StartDate must be before EndDate!")
}
})
// Here is also an example of how you can parse a string date
// -> https://gist.github.com/Aymkdn/b17903cf7786578300f04f50460ebe96
Find the region settings (of the current user) defined with _layouts/regionalsetng.aspx?Type=User (lcid, cultureInfo, timeZone, calendar, alternateCalendar, workWeek, timeFormat..)
$SP().regionalSettings(function(region) {
if (typeof region === "string") {
// something went wrong
console.log(region); // returns the error
} else {
// show the selected timezone, and the working days
console.log("timeZone: "+region.timeZone);
console.log("working days: "+region.workWeek.days.join(", "))
}
})
Permits to register a plugin
$SP().registerPlugin('test', function(options) {
console.log(options.message);
})
Permits to remove a notification that is shown on the screen
$SP().notify('Processing the data...', {sticky:true,name:"Processing data"}); // the notification will stay on the screen until we remove it
$SP().removeNotify("Processing data"); // the notification is removed
$SP().notify('Doing some stuff...');
$SP().notify('Doing some other stuff...');
$SP().removeNotify({all:true}); // all the notifications are removed
$SP().notify('Doing some stuff...');
$SP().notify('Doing some other stuff...');
$SP().notify('This is a sticky message', {sticky:true});
$SP().removeNotify({all:true, includeSticky:false}); // all the notifications are removed except the sticky one
Show a modal dialog (based on SP.UI.ModalDialog.showModalDialog) but provides some advanced functions and better management of the modals (for example when you launch several modals)
$SP().showModalDialog({
title:"Dialog",
html:'<h1>Hello World</h1><p><button type="button" onclick="$SP().closeModialDialog(\'here\')">Close</button></p>',
callback:function(dialogResult, returnValue) {
alert("Result="+dialogResult); // -> "here"
}
})
// show a waiting message
$SP().waitModalDialog("Working...");
// --- do some stuff ---
// close the waiting message and open a new modal dialog
$SP().showModalDialog({
closePrevious:true,
title:"Success",
html:'<h1>Done!</h1>'
})
// and use $SP().closeModalDialog() to close it
It will return a number with commas, currency sign and a specific number of decimals
$SP().toCurrency(1500000); // --> $1,500,000 $SP().toCurrency(1500000,2,''); // --> 1,500,000.00
Change a Sharepoint date (as a string) to a Date Object
$SP().toDate("2012-10-31T00:00:00").getFullYear(); // 2012
Change a Date object into a Sharepoint date string
$SP().toSPDate(new Date(2012,9,31), true); // --> "2012-10-31 00:00:00" $SP().toSPDate(new Date(2012,9,31)); // --> "2012-10-31"
Change a string into a XSL format string
$SP().toXSLString("Big Title"); // --> "Big_x0020_Title"
Find the Sharepoint groups where the specified user is member of
$SP().usergroups("mydomain\\john_doe",{url:"http://my.si.te/subdir/"}, function(groups) {
for (var i=0; i < groups.length; i++) console.log(groups[i]); // -> "Roadmap Admin", "Global Viewers", ...
});
Shortcut for SP.UI.ModalDialog.showWaitScreenWithNoClose()
Find the current user details like manager, email, colleagues, ...
$SP().whoami({url:"http://my.si.te/subdir/"}, function(people) {
for (var i=0; i < people.length; i++) console.log(people[i]+" = "+people[people[i]]);
});
Return the text related to a workflow status code
$SP().workflowStatusToText(2); // -> "In Progress"