Saturday 10 August 2013


Create Treeview of Current site All Lists and their Folders.


function retrieveAllFolders(listname, ulElement) {

    $().SPServices({
        operation: "GetListItems",
        listName: listname,
        CAMLQuery: '<Query><OrderBy><FieldRef Name="Modified"
Ascending="True" /></OrderBy></Query>',
        CAMLQueryOptions: '<QueryOptions><ViewAttributes
Scope="RecursiveAll"/></QueryOptions>',
        completefunc: function (xData, Status) {

            $(xData.responseXML).find("[nodeName='z:row']").each(function () {
                var thisFSObjType =
$(this).attr("ows_FSObjType").split(";#")[1];

                if (thisFSObjType == 1) {

                    var leafRef = $(this).attr("ows_FileRef").toLowerCase();
                    var nodename =
$(this).attr("ows_LinkFilename").toLowerCase();


                    if (leafRef.endsWith(("/" + listname + "/" +
nodename).toLowerCase())) {

                        var childULElement = document.createElement("ul");
                        childULElement.setAttribute("id",
updateID("pare_" + nodename));

                        var liElement = document.createElement("li");
                        liElement.setAttribute("id",
updateID("listname_" + nodename));


                        var textNode = document.createTextNode(nodename);
                        var anchorElement = document.createElement("a");
                        anchorElement.setAttribute("class", "anchorStyle");
                        anchorElement.appendChild(textNode);
                        $(anchorElement).click(toggleExpandCollapse);
                        anchorElement.setAttribute("href", "#");

                        liElement.setAttribute("class", "collapse");
                        liElement.appendChild(anchorElement);
                        liElement.appendChild(childULElement);
                        ulElement.appendChild(liElement);
                    }
                    else {

                        var leafRef = $(this).attr("ows_FileRef").toLowerCase();

                        var actualPath =
leafRef.substring(leafRef.indexOf(listname) + listname.length + 1);
                        var parent = actualPath.substring(0,
actualPath.lastIndexOf("/"));
                        parent = parent.replace("/", "_");
                        var nodeID = actualPath.replace("/", "_");

                        var parentElement =
document.getElementById(updateID("pare_" + parent));

                        var childULElement = document.createElement("ul");
                        childULElement.setAttribute("id",
updateID("pare_" + nodeID));
                        var liElement = document.createElement("li");
                        liElement.setAttribute("class", "collapse");
                        liElement.setAttribute("id", updateID(listname
+ "_parent_" + nodename));
                        var anchorElement = document.createElement("a");
                        anchorElement.setAttribute("class", "anchorStyle");

                        var textNode = document.createTextNode(nodename);
                        anchorElement.appendChild(textNode);
                        anchorElement.setAttribute("href", "#");
                        $(anchorElement).click(toggleExpandCollapse);
                        liElement.appendChild(anchorElement);
                        liElement.appendChild(childULElement);
                        liElement.click(toggleExpandCollapse);
                        parentElement.appendChild(liElement);

                    }
                }

                ImplementExpandCollapse(ulElement);
            });
        }
    });
}
function setupTree() {
    RetreiveAllSubsites();

}
function RetreiveAllSubsites() {


    $('#tree1').html().SPServices({
        operation: "GetAllSubWebCollection",
        completefunc: function (xData, status) {
            var ulElement1 = document.createElement("ul");
            ulElement1.setAttribute("id", "folderStruct");
            $('#tree1').html("").append(ulElement1);


            $(xData.responseXML).find("Web").each(function () {
                var webtitle = $(this).attr("Title").toLowerCase();
                var weburlvar = $(this).attr("Url").toLowerCase();



                var liElement = document.createElement("li");
                liElement.setAttribute("id", updateID(webtitle));

                liElement.setAttribute("class", "collapse");

                var childULElement = document.createElement("ul");
                childULElement.setAttribute("id", updateID("pare_" + webtitle));

                var textNode = document.createTextNode(webtitle);
                var anchorelement = document.createElement("a");

                anchorelement.appendChild(textNode);
                anchorelement.setAttribute("href", "#");
                anchorelement.setAttribute("class", "anchorStyle");

                $(anchorelement).click(toggleExpandCollapse);

                liElement.appendChild(anchorelement);
                liElement.appendChild(childULElement);
                ulElement1.appendChild(liElement);

                RetreiveAllLists(weburlvar, childULElement);

            });
        }

    });

}

function RetreiveAllLists(url, ulElement1) {

    $().SPServices({
        operation: "GetListCollection",
        webURL: url,
        completefunc: function (xData, Status) {



            $(xData.responseXML).find("[nodeName='List']").each(function () {
                var baseType = $(this).attr("BaseType");
                var listurl = $(this).attr("DefaultViewUrl").toLowerCase();
                var title = $(this).attr("Title").toLowerCase();
                if ((baseType == "1") &&
(listurl.indexOf("/_catalogs/") == -1) && (title != "converted forms")
&& (title != "customized reports") && (title != "form templates") &&
(title != "reporting templates") && (title != "style library") &&
(title != "site pages") && (title != "site assets") && (title !=
"wfpub")) {
                    var liElement = document.createElement("li");
                    liElement.setAttribute("id", updateID(title));
                    liElement.setAttribute("class", "collapse");

                    var childULElement = document.createElement("ul");
                    childULElement.setAttribute("id", updateID("pare_"
+ title));
                    var textNode = document.createTextNode(title);
                    var anchorelement = document.createElement("a");
                    anchorelement.appendChild(textNode);
                    anchorelement.setAttribute("href", "#");
                    anchorelement.setAttribute("class", "anchorStyle");
                    $(anchorelement).click(toggleExpandCollapse);

                    liElement.appendChild(anchorelement);
                    liElement.appendChild(childULElement);
                    ulElement1.appendChild(liElement);

                    retrieveAllFolders(title, childULElement);


                }
            });
        }
    });
}


function ImplementExpandCollapse(parentulElement) {
    PopulateCollapse($(parentulElement));
    addClickEvents();
}
function addClickEvents() {

    $("#folderStruct > li").each(function () {
        $(this).click = toggleExpandCollapse;
    });


}


function PopulateCollapse(parentulElement) {
    var count = $(parentulElement).children().length;

    if (count < 1)
        return;
    else {

        $(parentulElement).parent().attr("class", "collapse");
        $(parentulElement).hide();

        $("#" + parentulElement.attr("id") + "> li").each(function () {
            var lielement = $(this);
            $("#" + lielement.attr("id") + " > ul").each(function () {
                PopulateCollapse($(this));
            });
        });
    }
}


function toggleExpandCollapse(event) {
    var thisSiteUrl = $().SPServices.SPGetCurrentSite();
    var anchorElement = $(this);
    var liElement = anchorElement.parent();
    var classValue = liElement.attr("class");
    var AppendingText = "";

    if (classValue == "collapse") {
        liElement.attr("class", "expand");
        $("#" + liElement.attr("id") + " > ul").each(function () {
            var childElement = $(this);
            childElement.show();

        });
    }
    else {

        liElement.attr("class", "collapse");
        $("#" + liElement.attr("id") + " > ul").each(function () {
            var childElement = $(this);
            childElement.hide();

            PopulateCollapse(childElement);
        });
    }


    var textBox = $("input[id$= tbxUrl]").each(function () {
        $(anchorElement).parents().each(function () {
            var lielement = $(this);
            if (lielement.get(0).tagName == "LI") {
                $("#" + lielement.attr("id") + " > a").each(function () {

                    var liText = $(this).text();
                    AppendingText = liText + "/" + AppendingText;

                });
            }
        });
        $(this).val(thisSiteUrl + "/" + AppendingText);
    });
}

function updateID(tokenID) {

    tokenID = tokenID.replace("#", "_");
    tokenID = tokenID.replace(" ", "_");
    tokenID = tokenID.replace("/", "_");
    tokenID = tokenID.replace("/", "_");

    return tokenID;
}

No comments:

Post a Comment