Adding a simple Tip of the day web part

In a good enterprise environment , day after day such junior users or even power users need an everyday tip and trick or even daily quotes or news.

Here you are a good example of implementing such scenario:

  • Create a custom list and name it Tips.
  • Create a column name it {Tip} and specify its values like below in the picture:

Image

  • Add some tips to use it in previewing our list content while adding the web part like below:
Image
  • Add a content Editor Web Part in the Home page of your site, just click edit page and click insert ribbon and choose web part then from category section choose Media and Content then choose from left Content Editor:
Image
  • after saving your page you will notice a link inside the CEWP named ” Click here to add a content” just click it, and click the HTML drop-down control and choose the Edit HTML Source option, and enter the following code:

<style type=”text/css”>
.message { text-align: center; }
.message img { vertical-align: middle; }
blockTip p { font: italic bold 1.6em/1.2 “Times New Roman” , serif; }
blockTip p.title {
font: normal 1em/1.2 sans-serif;
color: #666;
text-align: right;
}
</style>
<div id=”tipOfTheDay”></div>
<script type=”text/javascript”>
Date.prototype.getFullDate = function () {
var dateAsString = ”;
dateAsString += this.getFullYear();
var month = this.getMonth() + 1;
if (month < 10) {
dateAsString += 0;
}

dateAsString += month;

var day = this.getDate();
if (day < 10) {
dateAsString += 0;
}

dateAsString += day;

return parseInt(dateAsString);
}

SP.SOD.executeOrDelayUntilScriptLoaded(function() {
Type.registerNamespace(‘Mavention.TipOfTheDay’);
Mavention.TipOfTheDay.displayTip =
function (webUrl, listName, container) {
container.innerHTML = ‘<div class=”message”>\
<img src=”/_layouts/images/loading.gif” alt=””/> \
Loading Tip of the Day…</div>’;

context = SP.ClientContext.get_current();
web = context.get_site().openWeb(webUrl);
list = web.get_lists().getByTitle(listName);
query = new SP.CamlQuery();
query.set_viewXml(‘<View><ViewFields><FieldRef Name=”Title”/>\
<FieldRef Name=”Tip”/></ViewFields></View>’);
items = list.getItems(query);
context.load(items);
context.executeQueryAsync(Function.createDelegate(this, function () {
numberOfTips = items.get_count();
if (numberOfTips > 0) {
now = new Date();
TipNumber = now.getFullDate() % numberOfTips;
TipItem = items.get_item(TipNumber);
title= TipItem.get_item(‘Title’);
Tip = TipItem.get_item(‘Tip’);

container.innerHTML = ‘<blockTip><p>’ + Tip +
‘</p><p class=”title”>’ + title + ‘</p></blockTip>’;
}
else {
container.innerHTML = ‘<div class=”message”>\
There are no Tips in the list yet</div>’;
}
}), Function.createDelegate(this, function () {
container.innerHTML = ‘<div class=”message”>\
<img src=”/_layouts/images/error16by16.gif” alt=””/> \
An error has occured while loading Tip of the Day</div>’;
}));
}

Mavention.TipOfTheDay.displayTip(‘/’, ‘Tips’,
document.getElementById(‘tipOfTheDay’));
}, ‘sp.js’);
</script>

note : you may not couldn’t add this code inside an HTML tag editor within the CEWP so, you will make alternate solution by copying the above code inside a text file and save it, then upload this text file to a document library such as {Shared Documents (for test only) }, and in the CEWP editing mode add the URL of the saved text file containing the code, and click ok to finish editing the web part like below:

Image

finally we have the Tip of the Day in the home page,

You can replace this scenario with any daily notifications or even daily quotes or announcements.

Advertisements

About Hossam Kamal

Hossam Kamal Senior SharePoint Developer with high development skills in C#, ASP.NET, SharePoint Server Technologies, jQuery, CSS, XML, O.O.D, SSRS and MS.SQL Server.
This entry was posted in SharePoint Designer, SharePoint Development, SharePoint General. Bookmark the permalink.

4 Responses to Adding a simple Tip of the day web part

  1. Anne says:

    This code looks suspiciously similar to a “Quote of the day” webpart by Waldek Mastykarz, consultant with Mavention. The code is published in the book “SharePoint 2010 at work”. Your only difference is “tip” instead of “quote”. Hint: the “Mavention” references in your script… Credit where credit is due, please !

    • Hossam Kamal says:

      Yes sir, but it wasn’t only in the book you mentioned, you can search for it, dozens of blogs talking about that and if I think i should mention every peace of code that i learned where ever you should reference the inventor of any programming language you use in daily work !!

  2. crjangel says:

    Could not get this to work for some reason

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s