Skip to content

Instantly share code, notes, and snippets.

@purtuga
Last active August 29, 2015 13:56
Show Gist options
  • Save purtuga/9036747 to your computer and use it in GitHub Desktop.
Save purtuga/9036747 to your computer and use it in GitHub Desktop.
Sharepoint WebServices utlity to retrieve information about a List (or Document Library) item based on its URL.
/**
* Given a URL to an item, this method will retrieve information about
* that URL, including the List UID and the ID of the item within the
* list.
*
* @param {Object|String} options
* An object with the options below or a string with the URL.
*
* @param {String} options.url
* @param {String} [options.webURL=(current site)]
* If left unset, the webURL of the current site will be used.
* @param {Boolean} [options.async=true]
*
* @return {jQuery.Promise}
* Promise is resolved with the following 3 params
* 1. Object containing the information about the url. The object
* will always contain at least one property - resultsFound -
* which is a Boolean indicating if information about the
* URL was found.. Other attribute in the object include
* 'strItemID' and 'strListID'. Object example:
* {
* resultsFound: false,
* strListID: '',
* strItemID: ''
* }
* 2. jQuery XHR object
* 3. jQuery ajax call Status
*
* @example
*
* $.SPGetListItemInfoByUrl({ url: "https...." })
* .done(function(urlInfo){
* alert(urlInfo.strListID);
* })
*
* // String input
* $.SPGetListItemInfoByUrl("https.....")
* .done(function(urlInfo){
* alert(urlInfo.strListID);
* })
*
* @requires jQuery
* @requires jQuery.SPServices
*
* @see http://wp.me/p2kCXW-62
*
* (c) 2014 | Paul Tavares (@paul_tavares) | MIT License
*/
$.SPGetListItemInfoByUrl = function(options){
var opt = $.extend({}, {
url: '',
webURL: $().SPServices.SPGetCurrentSite(),
async: true
}, options),
urlInfo = {
resultsFound: false,
strListID: '',
strItemID: ''
};
if (typeof options === "string") {
opt.url = options;
}
if (opt.url.indexOf("?") > -1) {
opt.url = opt.url.substr(0, opt.url.indexOf("?"));
} else if (opt.url.indexOf("#") > -1) {
opt.url = opt.url.substr(0, opt.url.indexOf("#"));
}
// Return a Promise
return $.Deferred(function(dfd){
// Make ajac call to SP webservice
$.ajax({
type: "POST",
cache: false,
async: opt.async,
url: opt.webURL + "/_vti_bin/SiteData.asmx",
contentType: "text/xml;charset=utf-8",
dataType: "xml",
data: "<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' " +
"xmlns:xsd='http://www.w3.org/2001/XMLSchema' " +
"xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>" +
"<soap:Body>" +
"<GetURLSegments xmlns='http://schemas.microsoft.com/sharepoint/soap/'>" +
"<strURL>" + opt.url + "</strURL>" +
"</GetURLSegments>" +
"</soap:Body>" +
"</soap:Envelope>",
})
.done(function(data, status, jqXHR){
var $xmlDoc = $(jqXHR.responseXML);
$xmlDoc.find("GetURLSegmentsResponse").children().each(function(){
var $this = $(this);
if ($this[0].nodeName === "GetURLSegmentsResult") {
urlInfo.resultsFound = ( $this.text() === "true" ? true : false );
} else {
urlInfo[ $this[0].nodeName ] = $this.text() || "";
}
});
dfd.resolveWith($, [urlInfo, jqXHR, status]);
})
.fail(function(jqXHR, status, error){
dfd.rejectWith($, [urlInfo, jqXHR, status]);
}); //end: .ajax()
}).promise();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment