I am trying to figure out how to add a new item or update an existing item using SAS PROC HTTP in a SharePoint 2010 list.
Here is a javascript/jquery button action that is tied to a temporary list to test posting data form SAS to SharePoint 2010. I've verified that the below code updates the first item's title to "I am a robot" and inserts a new item with the title "I am a robot too" This is javascript that is updating the list item or creating the list item using the SharePoint API (JSON data). The data update in the list is attached as a screenshot.
$('#testerbtn').click(function(event) { var beforeSendFunction = function (xhr) { xhr.setRequestHeader("If-Match", "*"); // Using MERGE so that the entire entity doesn't need to be sent over the wire. xhr.setRequestHeader("X-HTTP-Method", 'MERGE'); } //Update an existing item, Item 1, with I am a robot as the title var d = {}; d.Title = 'I am a robot'; var URL = thisbase.toLowerCase() + "/_vti_bin/ListData.svc/Tester2(1)"; console.log('data: '+JSON.stringify(d)); console.log(URL); var call = $.ajax({ type: "POST", contentType: "application/json; charset=utf-8", processData: false, beforeSend: beforeSendFunction, url: URL, data: JSON.stringify(d), dataType: "json" }); call.done(function (data,textStatus, jqXHR){ alert('success'); }); call.fail(function (jqXHR,textStatus,errorThrown){ alert('error. see logs'); console.log("Error in calendar data transmission: " + jqXHR.responseText); }); //Create a new item with I too am a robot as a title var d = {}; d.Title = 'I too am a robot'; var URL = thisbase.toLowerCase() + "/_vti_bin/ListData.svc/Tester2"; console.log('data: '+JSON.stringify(d)); console.log(URL); var call = $.ajax({ type: "POST", contentType: "application/json; charset=utf-8", processData: false, headers: { "Accept": "application/json;odata=verbose", "X-RequestDigest": $("#__REQUESTDIGEST").val() }, url: URL, data: JSON.stringify(d), dataType: "json" }); call.done(function (data,textStatus, jqXHR){ alert('success'); }); call.fail(function (jqXHR,textStatus,errorThrown){ alert('error. see logs'); console.log("Error in calendar data transmission: " + jqXHR.responseText); }); event.preventDefault(); });
Here is what the data looks like that is submitted to sharepoint:
data: {"Title":"I am a robot"} data: {"Title":"I too am a robot"}
So I tried to repeat this with SAS PROC HTTP. I think PROC JSON will only output an array, so I tried a string with no luck.
filename resp TEMP;
proc json out=resp pretty;
export work.tester (where=(one='A')) / nosastags;
run;
filename topics "C:\temp\tester_resp_json.txt" encoding="UTF-8";
/* type: "POST",
contentType: "application/json; charset=utf-8",
processData: false,
beforeSend: beforeSendFunction,
url: URL,
data: JSON.stringify(d),
dataType: "json"
*/
proc http url="http://sharepoint.site/subsite/_vti_bin/ListData.svc/Tester2(1)"
method="POST" in=resp /*"{'Title':'I am not a robot'}" */
out=topics AUTH_NEGOTIATE;
debug level = 0;
headers
"contentType "="application/json;odata=verbose;charset=utf-8"
"processData"="false"
"beforeSend"="function (xhr) {xhr.setRequestHeader('If-Match', '*'); xhr.setRequestHeader('X-HTTP-Method', 'MERGE');"
"Accept"="application/json; charset=utf-8"
"dataType"="json";
run;
When I look at the output, because I get this error:
NOTE: PROCEDURE HTTP used (Total process time):
real time 0.31 seconds
cpu time 0.00 seconds
NOTE: 405 Method Not Allowed
This is what the output says
{ "error": { "code": "", "message": { "lang": "en-US", "value": "The URI 'http://sharepoint.site/subsite/_vti_bin/ListData.svc/Tester2(1)/' is not valid for POST operation. For POST operations, the URI must refer to a service operation or an entity set." } }
Results after using the javascript (no success yet using SAS)
Wouldn't
NOTE: 405 Method Not Allowed
mean that
method="POST"
is disallowed?
Hi,
Made some modification, but now am getting an HTTP error 415.
filename topics "C:\temp\tester_resp_json.txt" encoding="UTF-8";
filename submit "C:\temp\submit.txt" encoding="UTF-8";
proc http url="http://sharepoint.site/subsite/_vti_bin/ListData.svc/Tester2(1)"
method="POST" in=submit
out=topics AUTH_NEGOTIATE;
headers
"dataType"="json"
"contentType"="application/json; charset=utf-8"
"processData"="false"
"X-HTTP-Method"="MERGE"
"If-Match"="*" ;
run;
The dataType matches what works using JavaScript. I'm not sure what could be going on as the CT= option has depreciated since 9.4 M3 from the documents. Any thoughts?
Using the SharePoint api's is on my "bucket list of SAS programs" to try and work out out some day (to be more portable across platforms).
On Windows, what I've been doing for many years is creating a Microsoft Access Database which contains a link to the SharePoint list. Then in SAS, I access the database (team site list) via SAS/Access:
2 libname teamsite access "D:\Public\SharePoint\Public.accdb"; NOTE: Libref TEAMSITE was successfully assigned as follows: Engine: ACCESS Physical Name: D:\Public\SharePoint\Public.accdb 3 4 proc sql noprint; 5 create table work.subset as 6 select * from teamsite.admins; NOTE: Table WORK.SUBSET created, with 88 rows and 26 columns. 7 quit; NOTE: PROCEDURE SQL used (Total process time): real time 0.36 seconds user cpu time 0.04 seconds system cpu time 0.04 seconds memory 5330.31k OS Memory 13796.00k Timestamp 02/08/2018 09:22:39 AM
Using proc sql, we can also update/delete/insert rows.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.