a JavaScript convenience library to get items from SharePoint lists using CSOM/JSOM
https://github.com/imthenachoman/nSPGetListItems
- Overview
- Reference
- function onListLoadComplete
- function onViewLoadComplete
- function onListItemLoadComplete
- function onError
- Examples
When developing something for SharePoint the majority of the time I am getting items from a list or document library using JavaScript. I got tired of having to write the same code over and over again so I wrote the helper library/utility nSPGetListItems
.
The idea is simple: call nSPGetListItems
with your options (a list or document library name at minimum), and get back details about the list, the view, and, of course, the list items.
Additionally, nSPGetListItems
lets you refresh the data asynchronously so you don't have to code for that.
The main function is called nSPGetListItems
and it takes one key : value
object. The available key
s are:
Key | Required | Type | Default Value | Description | Example |
---|---|---|---|---|---|
listName or listGUID |
yes | string | the name or GUID of the list |
|
|
viewName or viewGUID |
string | the list's default view | the name or GUID of the view you want to use to determine the fields and items to pull |
|
|
webURL |
string | the current web | the web URL you want to pull from |
|
|
onListLoadComplete |
function | n/a | the function to call when the list details are loaded | see onListLoadComplete below | |
onViewLoadComplete |
function | n/a | the function to call when the view details are loaded | see onViewLoadComplete below | |
onListItemLoadComplete |
function | n/a | the function to call when the list items are loaded | see onListItemLoadComplete below | |
onError |
function | n/a | the function to call if there is an error at any stage | see onError below | |
interval |
number | n/a |
The first thing nSPGetListItems
does is pull relevant data about the list based on the options you provided. If you provide a onListLoadComplete
function it will be called with an object with the following properties:
nSPGetListItems({
"onListLoadComplete" : function(listData)
});
listData:
listName
listGUID
viewName
viewGUID
listPermissions
- boolean values to indicate if the current user hasadd
,edit
,delete
, andview
permissions on the listlistForms
- URLs to thenew
,edit
, anddisp
forms for the listcontentTypes
- an array of all the content types available in the list with the following properties:name
description
forms
- URLs to thenew
,edit
, anddisp
forms for the content type (may be empty strings)
Next nSPGetListItems
will get details about the view you requested or the default view for the list. If you provide a onViewLoadComplete
function it will be called with three parameters and expects an SP.CamlQuery
return object.
nSPGetListItems({
"onViewLoadComplete" : function(viewFields, viewXML, camlQueru)
{
return camlQuery;
}
});
viewFields
- an array of objects providing details about each column/field, in order, from the view:typeID
- SharePoint field type number (see https://msdn.microsoft.com/en-us/library/office/jj245640.aspx)typeName
- SharePoint field type name (see https://msdn.microsoft.com/en-us/library/office/jj245640.aspx)internalName
- the internal name of the fielddisplayName
- the display name of the fieldstaticName
- the static name of the fieldsourceList
- iftypeName
isLookup
then this will have details about the lookup list and fieldlistPermissions
- same as onListLoadCompletelistForms
- same as onListLoadCompletesourceField
- the details of the lookup fieldtypeID
- same as abovetypeName
- same as aboveinternalName
- same as abovedisplayName
- same as abovestaticName
- same as above
viewXML
- the XML of the viewcamlQuery
- anSP.CamlQuery
object if you want to do something custom
If you want to do something custom with the query then you can use the camlQuery
paramater as the base and return an SP.CamlQuery
object.
For example:
nSPGetListItems({
"onViewLoadComplete" : function(viewFields, viewXML, camlQuery)
{
camlQuery.RecordCount = 100;
return camlQuery;
}
});
a
a
a