Created
July 26, 2013 16:11
-
-
Save mikekunze/6090130 to your computer and use it in GitHub Desktop.
This is an example of how to post a file attachment to a SharePoint 2013 list item. You must have the list contextinfo token available prior to adding attachments to a list item.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
user = "domain\\username" | |
pass = "password" | |
url = "http://sharepoint/siteCollection" | |
list = "listName" | |
spItemId = 1 | |
fileName = "filename.file" | |
data = "" # The file data should be pulled from the fs using { encoding: null } | |
# This is pulled from REST using ./_api/contextinfo | |
context = "some really long hex string value" | |
# Define the request fn | |
request = require 'request' | |
addAttachment: (cb)-> | |
proccessRequest = (err, res, body)-> | |
if err | |
console.log err | |
jsonBody = JSON.parse(body) | |
if jsonBody.error && jsonBody.error.code.indexOf("Microsoft.SharePoint.Client.InvalidClientQueryException") >= 0 | |
cb(jsonBody.error, null) | |
if jsonBody.error && jsonBody.error.code.indexOf("Microsoft.SharePoint.SPException") | |
cb(jsonBody.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('#{listName}')/items(#{spItemId})/AttachmentFiles/add(Filename='#{fileName}')" | |
body: data | |
binaryStringRequestBody: true | |
state: "Update" | |
request.post(config, processRequest).auth(user, pass, true) | |
addAttachment (err, res)-> | |
console.log err || res |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment