An alternative way of getting Client Object properties using SharePoint REST and CSOM APIs

When working with client APIs such as JSOM or REST you have probably noticed that certain properties of objects are not available compared to SSOM counterparts. For example, SPList class exposes SPList.Author property for getting an SPUser object that represents information about the user who created the list which in turn is not available for SP.List object. Hence the question arises, how those properties could be retrieved using client APIs?

The solution that I would like to demonstrate is based on retrieving client object properties from XML schema.

Getting SP.List object properties using JSOM

The function getListProperties is indented for loading list schema of the SP.List using SP.List.schemaXml property and extracting properties from it’s value:

function getListProperties(listTitle, OnSuccess,OnError) {
var context = SP.ClientContext.get_current();
var web = context.get_web();
var list = web.get_lists().getByTitle(listTitle);
context.load(web);
context.load(list,'SchemaXml');
context.executeQueryAsync(function(){
var schemaXml = list.get_schemaXml();
var listJson = schemaXml2Json(schemaXml);
OnSuccess(listJson);
},OnError);
}
function schemaXml2Json(schemaXml)
{
var jsonObject = {};
var schemaXmlDoc = $.parseXML(schemaXml);
$(schemaXmlDoc).find('List').each(function() {
$.each(this.attributes, function(i, attr){
jsonObject[attr.name] = attr.value;
});
});
return jsonObject;
}

view raw
getListProperties.js
hosted with ❤ by GitHub

The following example demonstrates how to retrieve Author property  using the specified method:

Example:

function getListAuthor(listTitle,OnSuccess,OnError) {
getListProperties(listTitle,
function(listJson){
var context = SP.ClientContext.get_current();
var listAuthor = context.get_web().getUserById(listJson.Author);
context.load(listAuthor);
context.executeQueryAsync(
function(){
OnSuccess(listAuthor);
},OnError);
},
OnError);
}
//Usage
getListAuthor('Discussion Board',
function(author){
console.log('List created by: ' + author.get_loginName());
},
function(sender,args){
console.log('Error occured:' + args.get_message());
}
);

view raw
getListAuthor.js
hosted with ❤ by GitHub

Getting SP.List object properties using REST

The following REST endpoint is used for retrieving SP.List.schemaXml property:

http://<sitecollection>/<site>/_api/web/lists/getbytitle(listtitle)/schemaXml

function getJson(url)
{
return $.ajax({
url: url,
type: "GET",
contentType: "application/json;odata=verbose",
headers: {
"Accept": "application/json;odata=verbose"
}
});
}
function getListProperties(listTitle) {
var listEndpointUrl = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('" + listTitle + "')/SchemaXml";
return getJson(listEndpointUrl).then(
function(data){
var schemaXml = data.d.SchemaXml;
return schemaXml2Json(schemaXml);
});
}

view raw
getListProperties.js
hosted with ❤ by GitHub

Example:

The same example that demonstrates how to retrieve Author property of List resource using REST:

getListProperties('Discussion Board')
.done(function(properties)
{
console.log('List created by: ' + properties.Author);
})
.fail(
function(error){
console.log(JSON.stringify(error));
});

view raw
getListAuthor.js
hosted with ❤ by GitHub