Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

Is there a way to get a reference to the current folder through the JS COM? It doesn't seem as though it can be done with SP.ListOperation. And by current folder, I mean the folder the user is currently "looking in", so to speak, not if they have selected a list item and it happens to be a folder.

I'm thinking I will have to check document.URL to see if there is a RootFolder query parameter, but it would be nice to know if there is a way to get that information directly through the object model.

share|improve this question

3 Answers 3

up vote 3 down vote accepted

How to get current Folder client object via CSOM (JavaScript)

function get_currentFolder(OnSuccess,OnError){
    var context = SP.ClientContext.get_current();
    var web = context.get_web();
    var currentFolder = web.getFolderByServerRelativeUrl(decodeURIComponent(ctx.rootFolder));
    context.load(currentFolder); 
    context.executeQueryAsync(
      function() {
        OnSuccess(currentFolder);
      },
      OnError
    );
}


//Usage: print current folder name
get_currentFolder(function(folder){
   console.log('Current folder:' + folder.get_name()); 
},function(sender,args){
    console.log(args.get_message());
});

Note: rootFolder property from ContextInfo structure is used to identify Folder url. ContextInfo structure is available on every page where List View is rendered.

share|improve this answer
    
That's more correct than my answer, since you actually have the folder object. –  Anders Aune Feb 7 at 14:32
    
Anders, your answer is correct also (this why i've upvoted it ) and you right,the only difference that in my case the Folder client object is returned –  Vadim Gremyachev Feb 7 at 14:42
    
So according to Anders' answer, this ctx object is only available in SP 2013? Because you are using it in your answer also... and I am on SP 2010. Is there any way to do something similar in 2010? –  Dylan Cristy Feb 7 at 19:08
    
Dylan, ContextInfo object (including ctx.rootFolder property) is available in both SP 2010 and 2013, hence the specified code should work properly in SP 2010 –  Vadim Gremyachev Feb 7 at 19:22

You can try this way to get the root folder of the list or library:

var _oList;
function getRootFolder()
{
    _oList = web.get_lists().getById("xxx");
    context.load(_oList , "RootFolder");
    var rootFolder = _oList .get_rootFolder();

    context.executeQueryAsync(OnSucceeded, OnFailed);
}

function OnSucceeded()
{
    var rootFolder = _oList .get_rootFolder();
    var rootFolderUrl = _oList.get_serverRelativeUrl();
}
share|improve this answer
    
Thanks for your answer Mike, but I am not looking for the root folder of the library. If the library has a folder in it called "FolderA", and inside FolderA is a subfolder called "Subfolder1", and the user has clicked into the subfolder (essentially at http://server/sites/site/library/folderA/subfolder1), then I want to know that they are currently in Subfolder1. Basically I want to answer the question "what folder am I currently in?" using JSCOM. –  Dylan Cristy Feb 6 at 14:34

You could probably always sort this out from the URL, but in 2013 you could probably check the ctx.rootFolder property.

And get the last part, which should be the current folder:

var currentFolder = decodeURIComponent(ctx.rootFolder).split("/").pop();

Edit:

Just to make it clear, this is the ctx object created by SharePoint when you for example browse inside a view of a document library. So you don't need to create this ctx object, it should already be present.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.