Last active
January 22, 2019 21:32
-
-
Save PatD/1b69d553b7e5b85f0431749021663bff to your computer and use it in GitHub Desktop.
SharePoint SPServices Example - Create Sitewide alert banner
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function () { | |
// Sitewide alert banner | |
// Set varible for staging, or not | |
if(window.location.pathname.indexOf('/stage/connect/') === 0){ | |
var webURL = "/stage/connect" | |
}else{ | |
var webURL = "/" | |
}; | |
function displayAlert(alertMsg) { // expects object | |
// Build the alert bar | |
var _header = document.getElementById('site-header'); | |
var alert = document.createElement('div'); | |
alert.className = 'site_down section alert ms-dialogHidden'; | |
// Handle alerts that have or don't have links | |
if(alertMsg._linkText === undefined){ | |
alert.innerHTML = '<b>⚠ ' + alertMsg._title + ':</b> ' + alertMsg._text + ''; | |
} else{ // Support for Read More link | |
alert.innerHTML = '<b>⚠ ' + alertMsg._title + ':</b> ' + alertMsg._text + ' <a href="'+ alertMsg._link+'" target="_blank" style="display:inline;text-decoration:underline;">' + alertMsg._linkText + '</a>'; | |
} | |
// Handle showing alerts only for authenticated users | |
if(alertMsg._showOn === "Authenticated Only" && typeof _spPageContextInfo.userId === "number"){ | |
_header.parentNode.insertBefore(alert, _header); | |
console.info("Banner is Active for Authenticated sites"); | |
} else if(alertMsg._showOn === "Authenticated Only" && typeof _spPageContextInfo.userId !== "number"){ | |
console.info("Alert Banner is active, but only shown to authenticated users"); | |
} else{ | |
_header.parentNode.insertBefore(alert, _header); | |
console.info("Alert Banner is Active for al users."); | |
} | |
}; | |
// Loads alert from list. | |
$().SPServices({ | |
operation: "GetListItems", | |
async: true, | |
webURL: webURL, | |
listName: "ConnectAlerts", | |
CAMLViewFields: "<ViewFields><FieldRef Name='Title' /><FieldRef Name='ShowOn' /><FieldRef Name='Text' /><FieldRef Name='URL' /><FieldRef Name='Expire' /></ViewFields>", | |
completefunc: function (xData) { | |
$(xData.responseXML).SPFilterNode("z:row").each(function () { | |
var alertMsg = new Object(); | |
if(this.attributes.ows_URL){ // Add link to object if there is one | |
alertMsg._link = this.attributes.ows_URL.nodeValue.split(", ")[0]; | |
alertMsg._linkText = this.attributes.ows_URL.nodeValue.split(", ")[1]; | |
} | |
alertMsg._title = this.attributes.ows_Title.nodeValue; | |
alertMsg._showOn = this.attributes.ows_ShowOn.nodeValue; | |
alertMsg._text = this.attributes.ows_Text.nodeValue; | |
return displayAlert(alertMsg); | |
}); | |
} | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment