Search This Blog

Tuesday, August 27, 2013

Custom breadcrumb - Sharepoint 2013


<%-- breadcrumb --%>
<div class="loop-breadcrumb">
<div id='javascript-breadcrumb' class='selected'></div>
</div>
 <%-- /breadcrumb --%>

<script type="text/javascript">

        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 (_spPageContextInfo.pageListId !== null && _spPageContextInfo.pageListId !== undefined) {
                var path = window.location.pathname;

                var breadcrumbpth = "";
                var bredname = "";
                var separated = path.split("/");

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

                    if (i > 2) {
                        bredname += "/" + separated[i];
                        var webAbsoluteUrl = _spPageContextInfo.webAbsoluteUrl;
                        breadcrumbpth += " > " + "<a href=" + webAbsoluteUrl + "" + bredname + ">" + separated[i] + "</a>";
                    }
                    if (length == 3) {
                        breadcrumbpth = " > " + document.title;
                    }
                }
                var titl = document.title.split('-');

                if (titl[1] != undefined) {
                    titl = " > " + document.title.split('-')[1];
                }
                else {
                    titl = "";
                }
                // Add the current site link to the breadcrumb
                var breadcrumburl = breadcrumbpth;

                this.breadcrumb = "<a href='" + _spPageContextInfo.webAbsoluteUrl + "' >" + _spPageContextInfo.webTitle + "</a></span><span style='font-style:italic;'>" + breadcrumburl + "</span><span style='font-style:italic;'>" + titl + "</span>";
                this.elm.innerHTML = this.breadcrumb;
            }
            // Retrieve the parent web of the current site
            var clientContext = new SP.ClientContext.get_current();
            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;
                    alert(this.elm.innerHTML);
                    // 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. Hi! I understand this is kind of off-topic however
    I had to ask. Does managing a well-established blog like yours take a large amount of
    work? I am brand new to running a blog however I do write in my
    diary every day. I'd like to start a blog so I can easily share my experience
    and feelings online. Please let me know if you have any ideas or tips
    for new aspiring blog owners. Appreciate it!

    http://www.est.hi-ho.ne.jp/cgi-bin/user/pino/yybbs.cgi

    Here is my webpage: Minneapolis SEO

    ReplyDelete