How do you get id of subfolder in a list

Sep 15, 2011 at 4:05 PM

I am needing the ID of a subfolder in a list in order to delete it.  While the following has worked for a folder:

// the following works for getting the id of a folder  

_getProjectFolderIdByName = function (projectFolder) {
         var thisId;
         camlQuery = "<Query><Where><Contains><FieldRef Name='FileRef'/><Value Type='Text'>ContractActions/" + projectFolder + "</Value></Contains></Where></Query>";

             $().SPServices({
                 operation: "GetListItems",
                 async: false,
                 listName: "ContractActions",
                 CAMLQuery: camlQuery,
                 completefunc: function (xData, Status) {
                     $(xData.responseXML).find("[nodeName='z:row']").each(function () {
                         thisId = $(this).attr("ows_ID");
                 }
             });
   
         return thisId;
     }
Trying to do something similar for a subfolder does not.
// the following does not work for getting the id of a subfolder

    _getContractActionFolderIdByName = function (projectFolder, contractActionFolder) {
        var thisId;
  	camlQuery = "<Query><Where><Contains><FieldRef Name='FileRef'/><Value Type='Text'>CAChecklists/" + projectFolder + "/" + contractActionFolder + "/</Value></Contains></Where></Query>";

            $().SPServices({
                operation: "GetListItems",
                async: false,
                listName: "CAChecklists",
                CAMLQuery: camlQuery,
                completefunc: function (xData, Status) {
                     $(xData.responseXML).find("[nodeName='z:row']").each(function () {
                         thisId = $(this).attr("ows_ID");
                }
            });
  
        return thisId;
    }
Any suggestions are appreciated.


Sep 15, 2011 at 4:15 PM

You can use the FSObjType field to get the folders in a list. You could get all list items with FSObjType="1" which indicates that the object is a folder. I have done this in the past and it works fine.

Sorry I do not have access to my code example for this here.

Coordinator
Sep 21, 2011 at 1:25 PM

Hopefully Dan's suggestion got you to the answer. Post back if not.

M.

Oct 3, 2011 at 3:42 PM

Not sure how I would just FSObjType in the example I have above.  More explanation would be helpful.  Also, I am dealing with a lot of subfolders.  It looks like what you are describing would require me to get all the subfolders back and then post process for the one I wanted.  How can I get just the one I am looking for?  (I need the id so I can delete the folder.)

Oct 3, 2011 at 11:16 PM

You'll want to look at using <QueryOptions> within you CAML.  Something like this should work for you:

<QueryOptions>
<ViewAttributes Scope="Recursive" />
</QueryOptions>

 

So in your SPService call, you'll want to add this to your call:

	$().SPServices({
                //<snip>...

		CAMLQueryOptions: "<QueryOptions><ViewAttributes Scope='RecursiveAll'/></QueryOptions>",
		completefunc: function(xData, Status) {
	             //<snip>...

		}
	});

Oct 4, 2011 at 2:26 PM

Thanks for your response.  I finally got it to work.  The following is a way to get the id of a subfolder called contractActionFolder that is located under the folder called projectFolder. 

 _getContractActionFolderIdByName = function (projectFolder, contractActionFolder) {

       var thisId;
       var camlQuery = "<Query><Where><And><Eq><FieldRef Name='FSObjType' /><Value Type='Lookup'>1</Value></Eq><Contains><FieldRef Name='FileRef'/><Value Type='Text'>" + contractActionFolder + "</Value></Contains></And></Where></Query>";
       var camlQueryOptions = "<QueryOptions><Folder>Lists/CAChecklists/" + projectFolder + "</Folder></QueryOptions>";

       $().SPServices({
           operation: "GetListItems",
           async: false,
           listName: "CAChecklists",
           CAMLQuery: camlQuery,
           CAMLQueryOptions: camlQueryOptions,
           completefunc: function (xData, Status) {
 
               $(xData.responseXML).find("[nodeName='z:row']").each(function () {
                   thisId = $(this).attr("ows_ID");
               });
           }
       });
 
       return thisId;
   }

Coordinator
Oct 4, 2011 at 2:29 PM

Great!

M.