SharePoint 2010/2013 Tip of the Day

I know there are a few blog posts and web parts floating around out there to do Tip of the Day functionality in SharePoint and it’s nothing revolutionary, but I was quickly able to throw a script together that accomplished this in SharePoint 2013 / Office 365 so I thought I’d share it in case you were having issues with one of those other solutions.

The functionality is very simple (the way I like to do things). To use the script you will need to make sure you have the following in place:

  1. A list named “TOTD”
  2. A “Title” field in your list that is the Tip’s title
  3. A “Tip” field which can be a single line of text or multiline text field which contains the “tip”
  4. A date only field called “TipDate” for the day you wish the tip to be displayed.  *Note* using the script below, you can have multiple tips for the same date and the script will randomly pick one of those tips. It will also randomly pick a tip if it doesn’t find a tip for the current date.

After you have the Tip of the Day list  in place you’ll need to do the following:

  1. Upload the script below to your “Site Assets” Document Library (or whichever library you store your scripts to)
  2. Add a Content Editor Web Part the page you would like to display the “Tip of the Day”
  3. Edit the Content Editor Web Part and link it to the script you previously uploaded.
  4. Ta and Da… that should be all there is to it.

The Script

Here’s the script. Because the script is so simple, I just put all the styling and everything in the one script. If you want to change the way the Tip of the Day is displayed I’d suggest modifying the styles.  There are two separate scripts below. One for SharePoint 2010 and one for SharePoint 2013. The main difference in the scripts is the REST query.

SharePoint 2013 Script

<script src="//code.jquery.com/jquery-1.10.1.min.js"></script>

<style type="text/css">

.TOTDHeading
{
    font-size:x-large;
    color:#3982F8;
}
.TOTDBody
{
    font-size:medium
}


</style>

<div id="TOTD">
<span class='TOTDHeading' id="TOTDTitle"></span><br>
<span class='TOTDBody' id="TOTDTip"></span>
</div>

<script type="text/javascript">

    //if "true" is passed in, the script will look for a tip with today's date
    //if "false" is passed in, a random tip will be displayed
    DisplayTip(true);
    
    function DisplayTip(useDate)
    {
        var listName = "TOTD";
        var titleField = "Title";
        var tipField = "Tip";
        var dateField = "TipDate";

        //query to retrieve tips, NOTE if no date is used, it will return ALL tips and select a random
        //one. If you plan to use the script in this manner, be sure that you don't put than 50 or so tips
        //in your list
        var query = "/_api/Web/Lists/GetByTitle('"+listName+"')/items?$select="+titleField+","+tipField;
        if (useDate)
        {
            var today = new Date();
            todayString = today.getFullYear() + "-" + (today.getMonth() + 1) + "-" + today.getDate();
            query += "&$filter="+dateField+" eq '" + todayString + "'";
        }
        var call = $.ajax({
            url: _spPageContextInfo.webAbsoluteUrl + query,
            type: "GET",
            dataType: "json",
            headers: {
                Accept: "application/json;odata=verbose"
            }       
        });
        call.done(function (data,textStatus, jqXHR){
            if (data.d.results.length < 1 && useDate)
            {
                //if not tip is found for today, just get a random tip
                //if you KNOW you will not have a tip for a specific day
                //set the initial call to pass in "false" otherwise you 
                //will notice a lag in load time.
                DisplayTip(false);
            } else {
            
                tipIndex = getRandomInt(0,data.d.results.length - 1);
    
                var title = data.d.results[tipIndex][titleField];
                var tip = data.d.results[tipIndex][tipField];
                
                $("#TOTDTitle").text(title);
                $("#TOTDTip").html(tip);
            }

        });
        call.fail(function (jqXHR,textStatus,errorThrown){
            alert("Error retrieving Tips: " + jqXHR.responseText);
        });
        
    }

    
    function getRandomInt (min, max) {
        return Math.floor(Math.random() * (max - min + 1)) + min;
    }
    
</script>

SharePoint 2010 Script

<script src="//code.jquery.com/jquery-1.10.1.min.js"></script>

<style type="text/css">

.TOTDHeading
{
    font-size:x-large;
    color:#3982F8;
}
.TOTDBody
{
    font-size:medium
}


</style>

<div id="TOTD">
<span class='TOTDHeading' id="TOTDTitle"></span><br>
<span class='TOTDBody' id="TOTDTip"></span>
</div>

<script type="text/javascript">

    //if "true" is passed in, the script will look for a tip with today's date
    //if "false" is passed in, a random tip will be displayed
    DisplayTip(true);
    
    function DisplayTip(useDate)
    {
        var listName = "TOTD";
        var titleField = "Title";
        var tipField = "Tip";
        var dateField = "TipDate";

        //query to retrieve tips, NOTE if no date is used, it will return ALL tips and select a random
        //one. If you plan to use the script in this manner, be sure that you don't put than 50 or so tips
        //in your list
        var query = "http://sp2010dev:1234/_vti_bin/listdata.svc/"+listName+"?$select="+titleField+","+tipField;
        if (useDate)
        {
            var today = new Date();
            var month = (today.getMonth() + 1);
            var monthString = month < 9?"0" + month: month;
            var day = today.getDate();
            var dayString = day< 9?"0" + day: day;
            
            todayString = today.getFullYear() + "-" + monthString + "-" + dayString;
            query += "&$filter="+dateField+" eq dateTime'"+todayString+"'";
        }
        var call = $.ajax({
            url: query,
            type: "GET",
            dataType: "json",
            headers: {
                Accept: "application/json;odata=verbose"
            }       
        });
        call.done(function (data,textStatus, jqXHR){
            if (data.d.results.length < 1 && useDate)
            {
                //if not tip is found for today, just get a random tip
                //if you KNOW you will not have a tip for a specific day
                //set the initial call to pass in "false" otherwise you 
                //will notice a lag in load time.
                DisplayTip(false);
            } else {
            
                tipIndex = getRandomInt(0,data.d.results.length - 1);
    
                var title = data.d.results[tipIndex][titleField];
                var tip = data.d.results[tipIndex][tipField];
                
                $("#TOTDTitle").text(title);
                $("#TOTDTip").html(tip);
            }

        });
        call.fail(function (jqXHR,textStatus,errorThrown){
            alert("Error retrieving Tips: " + jqXHR.responseText);
        });
        
    }

    
    function getRandomInt (min, max) {
        return Math.floor(Math.random() * (max - min + 1)) + min;
    }
    
</script>

And of course the video showing it in action

SharePoint Tip of the Day functionality

 

Some notes about these scripts

In order to keep the script simple and to reduce REST queries, the script returns ALL tips from the list if it does not query for a specific day. So, if you plan to use this script without using the date field (which I don’t recommend), you must keep number of tips in the list as low as possible so that you aren’t sending too much data over the wire. There are of course MANY different ways to implement this functionality, but again, for simplicity this is what I chose to do. 

Even if you have zero use for “Tip of the Day” functionality, there is some useful skills presented in these scripts if you are interested in learning how to use REST to query a SharePoint list for a specific date and then doing something with those returned results. Once you get hooked on the simplicity of using SharePoint’s REST functionality you’ll wonder how you lived without it.

Cheers! Hopefully you find these little posts useful! 

Families in Germany who are facing divers health problem, such persons can buy drugs from the Web without prescription. With the market flooded with divers web-sites selling sundry medicaments, purchasing medicines from th WEB is no longer a trouble for common man. Certain medications are used to treat infections caused by dental abscesses. Of course it isn’t all. If you’re concerned about erectile health problem, you probably know about Xenical and Hoodia. Probably each adult knows about Garcinia. (Read more PhentermineXenical). The symptoms of sexual health problems in men include improbability to have an erection sufficient for sexual functioning. Certain medications may add to sex drive difficulties, so its vital to cooperate with your soundness care professional so that the prescription can be tailored to your needs. Preparatory to capture Levitra or other preparation, speak to your dispenser if you have any allergies. Talk to your health care purveyor for more details. Preparatory to ordering this remedy, tell your physician if you are allergic to anything.

8 Comments

  1. Thanks Mark,

    Love your tips, and look forward to installing the Forms 7 system also soon.

    I am a complete newbie come power user not a programmer, and manage a Property and Facilities team site for a large public company I work for. I dabble in solution for our sute.

    I have tried to implement this on our 2010 system and get a “Error retriveing tips, undefined message. I am guessing the content edit has found the scrip ok, as it display no message if I change the address.

    Before I go annyoing our admin man 🙂
    do i need to change your script in any way or need to load and install any JQuery or Java script libray, to try and reslove the problem?

    Thanks

  2. This is great and is exactly what I need! I’ve got this working on a page by itself, but when added on to a page that already has the slider js from your old site, the slider disappears. I’m guessing this has something to do with having two javascripts on one page. Can you help?

    • Yes, it’s very likely that the scripts are not playing well together. I would have to see your scripts though in order to detangle what might be going on. First thing to check would be for unique id’s for all your elements and to make sure your supporting scripts (jQuery library and the like) are only referenced once.

  3. I have tried the 2013 script on SP2013 and getting an error message: “Error retrieving Tips: {“error”:{“code”:”-1, Microsoft.Sharepoint.SPException”,”message”:{“lang”:”en-US”,”value”:” The field or property ‘Tip’ does not exist.”}}}.
    Hopefully I typed this correctly! Thanks!

Comments are closed.