How to get folder information in SharePoint 2010

How do you get get a folder’s detailed information, such as time it was modified and who modified it, from the folder’s “serverRelativeUrl” using JavaScript object model in SharePoint 2010?

At first glance I thought this was a simple question. It is easy to get a SPFolder object from “serverRelativeUrl”, thus it should be very simple to get the folder related item. In SharePoint Server Object Model, we can get this from a folder’s “Item” property, but it’s not like that, instead I get a folders information using a JavaScript Object Model like this:

var context = SP.ClientContext.get_current();
var web = context.get_web();
//doc is a document library server relative url
var folder = web.getFolderByServerRelativeUrl("doc/f1");
context.load(folder);
Context.executeQueryAsnyc(func1, func2);

After loading the folder, I can only get a limited amount of information about the folder such as name, serverRelativeUrl and so on. I cannot get the folders modified time and which user modified it, and I even cannot get folder related items when I try to get detailed information from the related item. I can get the folder’s subfolders, the folder’s files and so on, but I cannot get the folders detailed information.

How can I get folder related item?
I found there is no direct way to get it. But I found I can get items using a caml query. A folder is a special listitem; so we can get the folders’ parent folder’s items, which is a filesystemobjecttype “folder”; and the server relative URL, which is equal to the specified server relative URL. The demo code is like this:

var context = SP.ClientContext.get_current();
var web = context.get_web();
//doc is a document library server relative url
var folder = web.getFolderByServerRelativeUrl("doc/f1");
var parentFolder = folder.get_parentFolder();
context.load(parentFolder);
context. executeQueryAsync(function(){
var query = new SP.CamlQuery();
query.set_folderServerRelativeUrl(parentFolder.get_serverRelativeUrl());
var allItems = list.getItems(query);
context.load(allItems, "Include(Title, FileSystemObjectType, File)");
context.executeQueryAsync(function(){
var itemsEnumerator = allItems.getEnumerator();
while(itemsEnumerator.moveNext()){
var item = itemsEnumerator.get_current();
var fileType = item.get_fileSystemObjectType();
if(fileType=="1"&&item.get_file().get_serverRelativeUrl()== "doc/f1"){
//get the detailed information
//item.get_item("Modified")
}
}
}, function(){});
}, function(){});

There is no direct way to get the folder’s detail information, so I tried to get the file related item. But there is still no direct way to get the folder related listitem, so we try to query the items and which content type is a folder, and then fetch the item which is related to the file’s serverRelativeUrl, which is equal to the folder’s serverRelativeUrl. After getting the item, we can get detailed information from the item.


Leave a Reply

Your email address will not be published.