Thursday, December 17, 2015

How to Create a Custom Greeting in SharePoint

Here a simple greeting you can create with the out-of-the-box Content Editor web part and some JavaScript. The script obtains the user's name from the SharePoint display name, and determines what time of the day it is to display the correct greeting as the following example.

Good morning, Leo!


Here's how to create the greeting. Add a Content Editor web part to your SharePoint page and add the following code to it:

<script type="text/javascript">
ExecuteOrDelayUntilScriptLoaded(init,'sp.js');
var currentUser;
var title;
var trimmed;
var titleAry;
var currentDate;
var currentHours;


function init(){
    this.clientContext = new SP.ClientContext.get_current();
    this.oWeb = clientContext.get_web();
    currentUser = this.oWeb.get_currentUser();
    this.clientContext.load(currentUser);
    this.clientContext.executeQueryAsync(Function.createDelegate(this,this.onQuerySucceeded), Function.createDelegate(this,this.onQueryFailed));
}


function onQuerySucceeded() {
    currentDate = new Date();
    currentHours = currentDate.getHours();
    if (currentHours >= 5 && currentHours < 12) { greeting = "Good morning"; }
    else if (currentHours >= 12 && currentHours <= 17) { greeting = "Good afternoon"; }
    else if (currentHours < 5 || currentHours > 17) { greeting = "Good evening"; }


    title = currentUser.get_title();
    trimmed = title.replace(/^\s+|\s+$/g, '');
    titleAry = trimmed.split(" ");
    document.getElementById('userTitle').innerHTML = greeting + ", " + titleAry[2] + "!";
}


function onQueryFailed(sender, args) {
    alert('Request failed. \nError: ' + args.get_message() + '\nStackTrace: ' + args.get_stackTrace());
}


</script><div style="font-size: 14pt; color: green"><b><span id="userTitle"></span></b></div>


NOTE: The user's name is obtained by parsing the display name. In this code example, the display name format is "LastName Title/Rank FirstName MI". So, the third token ([2]) is used to obtain the user's first name. You may need to modify the code depending on your organization's display name format.

Enjoy!
Leo

6 comments:

  1. Personally, I have found that adding code directly to a web part can be problematic (especially with infinite loop accidents). The solution I use is to store the code in an HTML or JS file which is uploaded to a document library. Then I link to that file from the content editor web part using the edit menu. If anything goes wrong I can still get to the code without needing the page it runs on to be working. In addition, I can turn on versioning in the document library where my code is stored to keep track of changes.

    ReplyDelete
  2. Good point and I agree with you Mr. Ben. I like to put my code in text files as well. You just need to update the text file when making code changes without having to re-publish your page.

    ReplyDelete
  3. Hi Leo,

    Thanks for this great script. I partially get the results I want. I would like to have the FirstName of the user as you're showing. The only thing I get is the LastName with titleAry[1] whenever I choose 2, 3, 4, etc. I get ‘Good morning Undefined!’
    Any suggestions how I can solve this even if it has to be in my AD? I tried it on 2 different Office 365 tenants one with a federated AD and one that only uses O365 itself.

    Thanks in advanced,
    Kind regards,
    Maarten

    ReplyDelete
  4. Yeah same problem with me as well, am getting goodafternoon Undefined !

    ReplyDelete