1

I have the following code, which gives me the title and FileRef of a page:

SP.SOD.executeOrDelayUntilScriptLoaded( 'SP.UserProfiles.js', "~sitecollection/Style%20Library/Scripts/jquery.SPServices-2013.01.min.js");
SP.SOD.executeFunc('SP.js', 'SP.ClientContext');

$(function() {
    var arrayItems = [];
    $().SPServices({
        operation: "GetListItems",
        webURL: "/Algemeen/",
        listName: "Pagina's",
        async: false,
        CAMLViewFields: "<ViewFields><FieldRef Name='Title' /></ViewFields>",               
        CAMLQuery: "<Query></Query>",
        completefunc: function(xData, Status) {
            $(xData.responseXML).SPFilterNode("z:row").each(function () {   
                content = new Object();
                content.title = $(this).attr("ows_Title");
                content.url =  $(this).attr("ows_FileRef").split(";#")[1];
                arrayItems.push(content);                                                         
                if(content.url.indexOf("aspx") <= -1) {
                    console.log('This element is not a page. ');
                }
            });
        }
    });
    console.log(arrayItems);
});

"Pagina's" is the name of the pages library, in which all of my pages are. In there I've also made some folders that contain pages. All of these work just fine.

Now, with the code I currently have, I can fetch the names of pages that are directly inside the library, but it will also return the paths of folders (the titles are returned as "undefined" because folders don't have a title, I suppose).

For example, if this would be my structure in the pages library:

- General.aspx
- Contact.aspx
- Company
   > Company-history.aspx
   > Employee-statistics.aspx
- FAQ.aspx

Then my code would only return "General.aspx", "Contact.aspx", "Company" and "FAQ.aspx" for the FileRef values. It doesn't show the pages that are inside the "Company" folder.

Now I've figured out how to check if an item in the library is a page or not, by checking if it contains "aspx". If it does not, then I know it's a folder. What I do not know though is how to see if this folder has child elements, meaning if it contains pages.

My end goal is to create some sort of sitemap from a subsite (in my case, it's called "Algemeen").

Any idea, suggestion or even a code example would be very appreciated!

2

Add this to your SPServices call:

 CAMLQueryOptions: '<QueryOptions><ViewAttributes Scope="Recursive"/></QueryOptions>',

So:

SP.SOD.executeOrDelayUntilScriptLoaded( 'SP.UserProfiles.js', "~sitecollection/Style%20Library/Scripts/jquery.SPServices-2013.01.min.js");
SP.SOD.executeFunc('SP.js', 'SP.ClientContext');

$(function() {
    var arrayItems = [];
    $().SPServices({
        operation: "GetListItems",
        webURL: "/Algemeen/",
        listName: "Pagina's",
        async: false,
        CAMLViewFields: "<ViewFields><FieldRef Name='Title' /></ViewFields>",               
        CAMLQuery: "<Query></Query>",
        CAMLQueryOptions: '<QueryOptions><ViewAttributes Scope="Recursive"/></QueryOptions>',
        completefunc: function(xData, Status) {
            $(xData.responseXML).SPFilterNode("z:row").each(function () {   
                content = new Object();
                content.title = $(this).attr("ows_Title");
                content.url =  $(this).attr("ows_FileRef").split(";#")[1];
                arrayItems.push(content);                                                         
                if(content.url.indexOf("aspx") <= -1) {
                    console.log('This element is not a page. ');
                }
            });
        }
    });
    console.log(arrayItems);
});

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.