Skip to content

JavaScript convenience library to get items from SharePoint lists using CSOM

License

Notifications You must be signed in to change notification settings

imthenachoman/nSPGetListItems

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 

Repository files navigation

nSPGetListItems

a JavaScript convenience library to get items from SharePoint lists using CSOM/JSOM

https://github.com/imthenachoman/nSPGetListItems

Table of Contents

  1. Overview
  2. Reference
    1. function onListLoadComplete
    2. function onViewLoadComplete
    3. function onListItemLoadComplete
    4. function onError
  3. Examples

Overview

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.

Reference

The main function is called nSPGetListItems and it takes one key : value object. The available keys are:

Key Required Type Default Value Description Example
listName or listGUID yes string the name or GUID of the list
  • "Announcements"
  • "{1c7c0498-6f1c-4ec1-8ee6-dd9959f3c52d}"
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
  • "All Documents"
  • "{dce68293-70e5-4c47-acda-72e6236b8f65}"
webURL string the current web the web URL you want to pull from
  • "/someSite"
  • "/someSite/subSite"
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

onListLoadComplete

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 has add, edit, delete, and view permissions on the list
  • listForms - URLs to the new, edit, and disp forms for the list
  • contentTypes - an array of all the content types available in the list with the following properties:
    • name
    • description
    • forms - URLs to the new, edit, and disp forms for the content type (may be empty strings)

onViewLoadComplete

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:
  • viewXML - the XML of the view
  • camlQuery - an SP.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;
    }
});

onListItemLoadComplete

a

onError

a

Examples

a

About

JavaScript convenience library to get items from SharePoint lists using CSOM

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published