Basically, what I want to achieve is to hide particular elements in all SharePoint sites within a single farm.
What I did is I created a SiteCode.js
file inside the Site Assets folder and filled it with the following code:
function CallClientOM() {
var context = new SP.ClientContext.get_current();
this.website = context.get_web();
this.currentUser = website.get_currentUser();
context.load(currentUser);
context.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}
function onQuerySucceeded(sender, args) {
alert(currentUser.get_loginName());
}
function onQueryFailed(sender, args) {
var siteActionsMenu = document.querySelectorAll("#siteactionid")[0];
siteActionsMenu.parentNode.removeChild(siteActionsMenu);
var topNavLinks = document.querySelectorAll(".brc-nav-toplinks-right")[0];
topNavLinks.parentNode.removeChild(topNavLinks);
}
//window.onload = CallClientOM();
/*
$(document).ready(function() {
CallClientOM();
});
*/
When I load this function inside Google Chrome Developer Tool (CallClientOM())
I am actually able to hide those elements and see changes right away. But the problem is I cannot achieve this once the site loads.
I have tried two methods which are at the bottom (as comments).
I've also tried to wrap all code within $(document).ready(function() {}
function, but with no success. It seems that I'm missing something or doing incorrectly.
How do I load this function once the site loads (without using Developer Tools or anything) before site content?