8

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.

6

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.

| improve this answer | |
  • 1
    That's more correct than my answer, since you actually have the folder object. – Anders Aune Feb 7 '14 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 '14 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 '14 at 19:08
  • 1
    @VadimGremyachev: Whether the ctx object available in the script depends on the context and location of the script. The ctx object and ist values are injected by the list view web part. That means, if your script block is located above this web part and executed before the ctx object gets initialized, it is simply not available yet and you should read the current folder value from the RootFolder query parameter. Or you should restructure the execution of your code that it would be executed after the ctx object gets initialized. – pholpar Feb 6 '19 at 11:05
  • 1
    @VadimGremyachev: Although it is not a common case, I found the situation is even worse, if you happen to have multiple list view web parts on the page. Since the wep parts simply ejects their JavaScript code into the page, they seem to pollute the global JS namespace. That means, the WP at the bottom of the page overwrites the ctx of the WP above. The RootFolder query parameter seems to have always the value that corresponds to the folder you actually clicked on, independently from the WP. Not an easy case, if one really needs to get the current folder of a specific web part on the page. – pholpar Feb 7 '19 at 8:21
3

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.

| improve this answer | |
  • Whether the ctx object available in the script depends on the context and location of the script. The ctx object and ist values are injected by the list view web part. That means, if your script block is located above this web part and executed before the ctx object gets initialized, it is simply not available yet and you should read the current folder value from the RootFolder query parameter. Or you should restructure the execution of your code that it would be executed after the ctx object gets initialized. – pholpar Feb 6 '19 at 11:06
1

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();
}
| improve this answer | |
  • 1
    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 '14 at 14:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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