node-sharepoint-rest
Version:
On Premise SharePoint 2013 REST API wrapper. Requires basic auth to be enabled on SharePoint
256 lines (193 loc) • 6.76 kB
text/coffeescript
class CustomLists
constructor: ()->
getLists: (cb)->
processRequest = (err, res, body)->
if !body || !JSON.parse(body).d
cb({err:err})
else
cb(err, JSON.parse(body).d.results)
config =
headers :
Accept: "application/json;odata=verbose"
strictSSL: .strictSSL
url : "#{@url}/_api/lists"
.get(config, processRequest).auth(, , true)
return @
getListItemsByTitle: (title, cb)->
processRequest = (err, res, body)->
if !body || !JSON.parse(body).d
cb("no list of title : #{title}")
else
cb(err, JSON.parse(body).d.results)
config =
headers:
Accept: "application/json;odata=verbose"
strictSSL: .strictSSL
url: "#{@url}/_api/web/lists/getbytitle('#{title}')/items"
.get(config, processRequest).auth(, , true)
return @
getListTypeByTitle: (title, cb)->
processRequest = (err, res, body)->
if !body || !JSON.parse(body).d
cb("no list of title : #{title}")
else
cb(err, JSON.parse(body).d.ListItemEntityTypeFullName)
config =
headers :
Accept: "application/json;odata=verbose"
strictSSL: .strictSSL
url : "#{@url}/_api/web/lists/getbytitle('#{title}')?"
.get(config, processRequest).auth(, , true)
return @
addAttachmentToListItem: (req, cb)->
processRequest = (err, res, body)->
if err
err
jsonBody = JSON.parse(body)
if jsonBody.error && jsonBody.error.code.indexOf("Microsoft.SharePoint.Client.InvalidClientQueryException") >= 0
cb("Microsoft.SharePoint.Client.InvalidClientQueryException", null)
if jsonBody.error && jsonBody.error.code.indexOf("Microsoft.SharePoint.SPException")
cb("Microsoft.SharePoint.SPException", null)
if jsonBody.error && jsonBody.error.code
cb(jsonBody.error, null)
else
cb(err, JSON.parse(body).d)
config =
headers :
"Accept": "application/json;odata=verbose"
"X-RequestDigest": req.context
"content-type": "application/json;odata=verbose"
url : "#{@url}/_api/web/lists/getbytitle('#{req.title}')/items(#{req.itemId})/AttachmentFiles/add(Filename='#{req.binary.fileName}')"
strictSSL: .strictSSL
body: req.data
binaryStringRequestBody: true
state: "update"
.post(config, processRequest).auth(, , true)
return @
addListItemByTitle: (title, item, context, cb)->
processRequest = (err, res, body)->
jsonBody = JSON.parse(body)
if jsonBody.error && jsonBody.error.code.indexOf("Microsoft.SharePoint.Client.InvalidClientQueryException") >= 0
cb("Microsoft.SharePoint.Client.InvalidClientQueryException", null)
else if jsonBody.error && jsonBody.error.code
cb(JSON.parse(body).error, null)
else
cb(err, JSON.parse(body).d)
config =
headers :
"Accept": "application/json;odata=verbose"
"X-RequestDigest": context
"content-type": "application/json;odata=verbose"
url: "#{@url}/_api/web/lists/getbytitle('#{title}')/items"
strictSSL: .strictSSL
body: JSON.stringify(item)
.post(config, processRequest).auth(, , true)
return @
createList: (req, cb)->
if !req.context
cb({err: "please provide a context"})
return
if !req.title
cb({err: "please provide a title"})
return
if !req.description
cb({err: "please provide a description"})
return
context = req.context
title = req.title
description = req.description
processRequest = (err, res, body)->
jsonBody = JSON.parse(body)
if jsonBody.error && jsonBody.error.code.indexOf("Microsoft.SharePoint.Client.InvalidClientQueryException") >= 0
cb("Microsoft.SharePoint.Client.InvalidClientQueryException", null)
else if jsonBody.error && jsonBody.error.code
cb(JSON.parse(body).error, null)
else
cb(err, JSON.parse(body).d)
body =
__metadata:
type: 'SP.List'
AllowContentTypes: true
BaseTemplate: 100
ContentTypesEnabled: true
Description: description
Title: title
config =
headers :
"Accept": "application/json;odata=verbose"
"X-RequestDigest": context
"content-type": "application/json;odata=verbose"
url: "#{@url}/_api/web/lists"
strictSSL: .strictSSL
body: JSON.stringify(body)
.post(config, processRequest).auth(, , true)
return @
deleteListByGUID: (req, cb)->
if !req.context
cb({err: "please provide a context"})
return
if !req.guid
cb({err: "please provide a guid"})
return
if !cb
cb({err: "please provide a callback"})
return
context = req.context
guid = req.guid
processRequest = (err, res, body)->
cb(err)
config =
headers:
"Accept": "application/json;odata=verbose"
"X-RequestDigest": context
"IF-MATCH": "*"
"X-HTTP-Method": "DELETE"
url: "#{@url}/_api/web/lists(guid'#{guid}')"
strictSSL: .strictSSL
.post(config, processRequest).auth(, , true)
return @
createColumnForListByGUID: (req, cb)->
if !req.context
cb({err: "please provide a context"})
return
if !req.title
cb({err: "please provide a title"})
return
if !req.type
cb({err: "please provide a type"})
return
if !req.guid
cb({err: "please provide a guid"})
return
context = req.context
title = req.title
type = req.type
guid = req.guid
processRequest = (err, res, body)->
jsonBody = JSON.parse(body)
if jsonBody.error && jsonBody.error.code
cb(jsonBody.error, null)
else
cb(err, JSON.parse(body).d)
body =
__metadata:
type: 'SP.Field'
Title: title
FieldTypeKind: type
Required: 'false'
EnforceUniqueValues: 'false'
StaticName: title
if type is 3
body.__metadata.RichText = "TRUE"
body.__metadata.RichTextMode = "FullHtml"
config =
headers :
"Accept": "application/json;odata=verbose"
"X-RequestDigest": context
"content-type": "application/json;odata=verbose"
url: "#{@url}/_api/web/lists(guid'#{guid}')/Fields"
strictSSL: .strictSSL
body: JSON.stringify(body)
.post(config, processRequest).auth(, , true)
return @
module.exports = CustomLists