Reputation: 566
Using the Sharepoint REST API, I am able to get "Author" (which I take as owner/creator info) information for both Files and Folders. For Files, I can simply expand the "Author" element and get Title and UserId information. For Folders, I have to use the "AuthorId" field from expanding "ListItemAllFields" to make a subsequent REST call to resolve the Title and UserId information by Id (I don't like this extra step either but I haven't found a way to get that info for Folders with a single GET). However, for the base Lists and their associated "RootFolder"s, I do not get Author information, even if I expand the RootFolder's ListItemAllFields. Is there a way to get the owner\creator information for at least the Document Library Lists where my Folders and Files reside? I expected that expanding the RootFolder's ListItemAllFields would give me the "AuthorId" like it does for the sub-folders that have been created in the Document Library but all I get for this: .../_api/Web/GetFolderByServerRelativeUrl('docLibraryRootFolderServerRelativeUrl')/ListItemAllFields, is:
{
"d": {
"ListItemAllFields": null
}
}
Upvotes: 2
Views: 3682
Reputation: 59328
ListItemAllFields
property returns associated List Item
with File
or Folder
objects.
For List object
REST query:
http://<sitecollection>/<site>/_api/web/lists/getbytitle(listtitle)/ListItemAllFields
returns nothing since List
object is not associated with List Item object.
In order to retrieve Author
property for List
object you could utilize the following approach. The solution is to extract Author property from List XML schema.
The following method is used to return all available properties for List
object:
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);
});
}
where
function getJson(url)
{
return $.ajax({
url: url,
type: "GET",
contentType: "application/json;odata=verbose",
headers: {
"Accept": "application/json;odata=verbose"
}
});
}
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;
}
Usage
How to get Author
property for List
object:
getListProperties('Discussion Board')
.done(function(properties)
{
console.log('List created by: ' + properties.Author);
})
.fail(
function(error){
console.log(JSON.stringify(error));
});
Please follow this post for a more details.
Upvotes: 1