Search This Blog

Thursday, September 19, 2013

Update Custom Breadcrumb- Sharepoint 2013

<%-- breadcrumb --%>
<div class="loop-breadcrumb">
<div id='javascript-breadcrumb' class='selected'></div>
</div>
 <%-- /breadcrumb --%>
<script>
    if (document.addEventListener) {
        document.addEventListener('DOMContentLoaded', function () {
            EnsureScriptFunc('sp.js', 'SP.ClientContext', JavascriptBreadcrumb);
        }, false);
    } else {
        window.setTimeout(function () {
            EnsureScriptFunc('sp.js', 'SP.ClientContext', JavascriptBreadcrumb);
        }, 0);
    }

    function JavascriptBreadcrumb() {

        if (_spPageContextInfo.pageListId !== null && _spPageContextInfo.pageListId !== undefined)

        // Retrieve the breadcrumb element
            this.elm = document.getElementById('javascript-breadcrumb');

        if (window.location.pathname.indexOf("Lists") != -1) {
            var path = window.location.pathname;
            //document.getElementById('javascript-breadcrumbx').innerHTML = window.location.href;
            var breadcrumbpth = "";
            var bredname = "";
            var separated = path.split("/");
            var titl = document.title.split('-');

            if (titl[1] != undefined) {
                titl = " > " + document.title.split('-')[1];
            }
            else {
                titl = " > " + document.title;
            }

            for (var i = 0, length = separated.length - 1; i < length; i++) {

                if (i > 2) {
                    bredname += "/" + separated[i];

                    var webAbsoluteUrl = _spPageContextInfo.webAbsoluteUrl;
                    var sppath = separated[i].replace(/-/g, " ");
                    breadcrumbpth += " > " + "<a href=" + webAbsoluteUrl + "" + bredname + ">" + sppath.replace(/%20/g, " ") + "</a>";
                }
                if (length == 3) {
                    breadcrumbpth = " > " + "<span style='font-weight:bold;'>" + document.title.replace("%20", " ") + "</span>";
                    titl = "";

                }
            }

            // Add the current site link to the breadcrumb
            var breadcrumburl = breadcrumbpth;
            breadcrumburl = breadcrumburl.toLowerCase().replace(/\b[a-z]/g, function (letter) {
                return letter.toUpperCase();
            });

            this.breadcrumb = "<a href='" + _spPageContextInfo.webAbsoluteUrl + "' >" + _spPageContextInfo.webTitle + "</a><span style='font-style:normal;'>" + breadcrumburl + "</span><span style='font-weight:bold;'>" + titl + "</span>";
            this.elm.innerHTML = this.breadcrumb;

        }

        if (_spPageContextInfo.pageListId !== null && _spPageContextInfo.pageListId !== undefined) {
            if (window.location.pathname.indexOf(".aspx") == -1) {

                var path = window.location.pathname;

                var breadcrumbpth = "";
                var bredname = "";
                var separated = path.split("/");
                var titl = document.title.split('-');
                var FormTitle = "";
                if (titl[1] != undefined) {
                    if (titl.length == 2) {
                        titl = " > " + "<span style='font-weight:bold;'>" + document.title.split('-')[1] + "</span>";

                    }
                    else {
                        for (var i = 0, length = titl.length - 1; i < length; i++) {

                            FormTitle += " " + titl[i];
                        }

                        titl = " > " + "<span style='font-weight:bold;'>" + FormTitle + "</span>";
                    }
                }

                else {
                    titl = " > " + "<span style='font-weight:bold;'>" + document.title + "</span>";
                }

                for (var i = 0, length = separated.length - 1; i < length; i++) {

                    if (i > 2) {
                        if (separated[i] == "gbt" || separated[i] == "Gbt") {
                            separated[i] = separated[i].toUpperCase();
                        }

                        bredname += "/" + separated[i];
                        var webAbsoluteUrl = _spPageContextInfo.webAbsoluteUrl;
                        breadcrumbpth += " > " + "<a href=" + webAbsoluteUrl + "" + bredname + ">" + separated[i].replace(/-/g, " ") + "</a>";
                    }
                    if (length == 3) {
                        breadcrumbpth = " > " + "<span style='font-weight:bold;'>" + document.title + "</span>";
                        titl = "";

                    }
                }

                // Add the current site link to the breadcrumb
                var breadcrumburl = breadcrumbpth;

                breadcrumburl = breadcrumburl.replace(/\b[a-z]/g, function (letter) {
                    return letter.toUpperCase();
                });

                this.breadcrumb = "<a href='" + _spPageContextInfo.webAbsoluteUrl + "' >" + _spPageContextInfo.webTitle + "</a><span style='font-style:normal;'>" + breadcrumburl + "</span><span style='font-weight:normal;'>" + titl + "</span>";
                this.elm.innerHTML = this.breadcrumb;
            }
        }
        // Retrieve the parent web of the current site
        var clientContext = new SP.ClientContext(_spPageContextInfo.webAbsoluteUrl);
        this.web = clientContext.get_web();
        this.parentweb = this.web.get_parentWeb();

        clientContext.load(this.parentweb);
        clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySuccess), Function.createDelegate(this, this.onCreationFail));
    }

    function onQuerySuccess() {
        try {
            // Check if the current web has a title, if undefined, the top site has already been reached
            if (this.parentweb.get_title() !== undefined) {
                // Add the parent site to the breadcrumb
                this.breadcrumb = "<a href='" + this.parentweb.get_serverRelativeUrl() + "' style='color:#fff'>" + this.parentweb.get_title() + "</a>" + " > " + this.breadcrumb;
                this.elm.innerHTML = this.breadcrumb;
                // Retrieve the parent its parent site
                var clientContext = new SP.ClientContext(this.parentweb.get_serverRelativeUrl());
                this.web = clientContext.get_web();
                this.parentweb = this.web.get_parentWeb();

                clientContext.load(this.parentweb);
                clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySuccess), Function.createDelegate(this, this.onCreationFail));
            }
        }
        catch (e) { }
    }

    function onCreationFail() { }

</script>

1 comment:

  1. We're a group of volunteers and starting a new scheme in our community.
    Your website provided us with valuable info to work on.
    You have done an impressive job and our entire community will be thankful to you.




    http://www.syfytopsites.com/index.php?a=stats&u=nolavwtdpdpvw

    Also visit my website; easy money online

    ReplyDelete