Last active
January 4, 2016 18:28
-
-
Save stevenfollis/8660387 to your computer and use it in GitHub Desktop.
Snippets of jQuery/JavaScript for doing cool stuff with REST services in SharePoint 2013
This file contains 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
/*============================================================================*/ | |
/* Query an external endpoint via REST */ | |
function queryEndpoint(url, callback) { | |
// Issue a POST request to the SP.WebProxy.Invoke endpoint. | |
// The body has the information to issue a GET request | |
$.ajax({ | |
url: "../_api/SP.WebProxy.invoke", | |
type: "POST", | |
data: JSON.stringify({ | |
"requestInfo": { | |
"__metadata": { | |
"type": "SP.WebRequestInfo" | |
}, | |
"Url": url, | |
"Method": "GET", | |
"Headers": { | |
"results": [{ | |
"__metadata": { | |
"type": "SP.KeyValue" | |
}, | |
"Key": "Accept", | |
"Value": "application/json;odata=verbose", | |
"ValueType": "Edm.String" | |
}] | |
} | |
} | |
}), | |
headers: { | |
"Accept": "application/json;odata=verbose", | |
"Content-Type": "application/json;odata=verbose", | |
"X-RequestDigest": $("#__REQUESTDIGEST").val() | |
}, | |
success: function(rawData) { | |
// Check for status code == 200 | |
if (rawData.d.Invoke.StatusCode == 200) { | |
// convert the results string to a JSON object | |
var data = JSON.parse(rawData.d.Invoke.Body); | |
// execute the callback | |
callback(data); | |
} | |
}, | |
error: function(jqXHR, textStatus, errorThrown) { | |
// check if the browser can handle console logging | |
if (typeof console != "undefined") { | |
console.log(textStatus + ' ' + errorThrown + ' - when attempting to query ' + url); | |
} | |
} | |
}); | |
} | |
$(document).ready(function() { | |
// query the FAA's endpoint for the Charlotte, NC airport and do something after | |
queryEndpoint('http://services.faa.gov/airport/status/CLT?format=json', function(data) { | |
console.log(data); | |
}); | |
}); | |
/*============================================================================*/ | |
/* Create Site Column */ | |
function createSiteColumn() { | |
var fieldType = 2; //enum value from something | |
var fieldInternalName = "SFTest"; | |
var fieldDisplayName = "SF Test Column"; | |
var fieldGroup = "SF Custom Columns"; | |
var fieldRequired = false; | |
var fieldHidden = false; | |
$.ajax({ | |
url: getQueryStringParameter('SPAppWebUrl') + "/_api/SP.AppContextSite(@target)/web/fields?@target='" + getQueryStringParameter('SPHostUrl') + "'", | |
type: "POST", | |
data: JSON.stringify({ | |
'__metadata': { | |
'type': 'SP.Field' | |
}, | |
'FieldTypeKind': fieldType, | |
'InternalName': fieldInternalName, | |
'Title': fieldDisplayName, | |
'Group': fieldGroup, | |
'Required': fieldRequired, | |
'Hidden': fieldHidden | |
}), | |
headers: { | |
"accept": "application/json;odata=verbose", | |
"content-type": "application/json;odata=verbose", | |
"X-RequestDigest": $("#__REQUESTDIGEST").val() | |
}, | |
success: function(data) { | |
console.log('success!'); | |
console.log(data); | |
}, | |
error: function(err) { | |
alert(JSON.stringify(err)); | |
} | |
}); | |
} | |
function createContentType() { | |
var listSettings = { | |
'_metadata': { | |
'type': 'SP.ContentType' | |
}, | |
'AllowContentTypes': true, | |
'BaseTemplate': 104, | |
'ContentTypesEnabled': true, | |
'Description': 'My list description', | |
'Title': 'RestTest' | |
}; | |
$.ajax({ | |
url: getQueryStringParameter('SPAppWebUrl') + "/_api/SP.AppContextSite(@target)/web/lists?@target='" + getQueryStringParameter('SPHostUrl') + "'", | |
type: "POST", | |
data: JSON.stringify(listSettings), | |
headers: { | |
"accept": "application/json;odata=verbose", | |
"content-type": "application/json;odata=verbose", | |
"X-RequestDigest": $("#__REQUESTDIGEST").val() | |
}, | |
success: function(data) { | |
console.log('success!'); | |
console.log(data); | |
}, | |
error: function(err) { | |
alert(JSON.stringify(err)); | |
} | |
}); | |
} | |
//function to get a parameter value by a specific key | |
function getQueryStringParameter(urlParameterKey) { | |
var params = document.URL.split('?')[1].split('&'); | |
for (var i = 0; i < params.length; i = i + 1) { | |
var singleParam = params[i].split('='); | |
if (singleParam[0] == urlParameterKey) return decodeURIComponent(singleParam[1]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment