Tuesday, April 14, 2015

Hiding OneDrive from Your SharePoint 2013 Pages

Suppose you don't want your customers, for whatever reasons, to access OneDrive from their SharePoint sites, yet you do want them to use Newsfeed and Sites. What to do?

This problem actually has quite a simple solution. Basically you want to write a script (in JavaScript) that would prevent OneDrive from being displayed. The <a> element that contains a link to <span>OneDrive</span> has an ID that always ends with "_ShellDocuments" (e.g., "ctl00_ctl53_ShellDocuments"), and each link has the "ms-core-suiteLink-a" CSS class. Armed with this theory, you can easily design the solution:

1) Create a new ASP.NET user control.
2) Create a site-collection scoped feature whose elements file refers to this control, e.g.

<Control Id="HideOneDrive" Sequence="10" ControlSrc="~/_controltemplates/15/HideOneDrive.ascx" />

3) Add this to your ASP.NET user control:

<script language="javascript">

    _spBodyOnLoadFunctionNames.push("_doHideOneDrive");

    String.prototype.endsWith = function(suffix) {
        return this.indexOf(suffix, this.length - suffix.length) !== -1;
    };

function _doHideOneDrive() {
        var x = document.getElementsByClassName('ms-core-suiteLink-a');
        var i;
        for (i = 0; i < x.length; i++) {
            if (x[i].id.endsWith("_ShellDocuments")) {
                x[i].style.display = 'none';
            }
        }
    }
<script>

That's it. Newsfeed and Sites stay, but OneDrive is gone.