{"id":967,"date":"2011-01-09T16:53:28","date_gmt":"2011-01-09T15:53:28","guid":{"rendered":"http:\/\/www.itidea.nl\/?p=967"},"modified":"2015-09-08T20:33:55","modified_gmt":"2015-09-08T18:33:55","slug":"spservices-and-jquery-templates-upcoming-birthday-overview","status":"publish","type":"post","link":"https:\/\/www.itidea.nl\/index.php\/spservices-and-jquery-templates-upcoming-birthday-overview\/","title":{"rendered":"SPServices and jQuery templates: Upcoming birthday\u2019s overview"},"content":{"rendered":"<p>The use of jQuery templates is very neat. It easily displays content in a structured way according to a defined template. Microsoft created the jQuery template plugin (<a href=\"https:\/\/github.com\/nje\/jquery-tmpl\">https:\/\/github.com\/nje\/jquery-tmpl<\/a>) which can be used by referencing the template script file.\u00a0<\/p>\n<p>In this article both SPServices and the jQuery template will be used to show the strength of combining these libraries.\u00a0<\/p>\n<h2>Purpose<\/h2>\n<p>All employees are highly appreciated at the company there are working (of course). The company decides to send all employees flowers on their birthday.\u00a0The employee data is stored in a standard SharePoint Contacts list extended with a column to register the birth days. Employees of the HR department are responsible for sending the flowers on time, but they it\u2019s difficult for them to get the upcoming birthdays from the Contact list.<br \/>\nIt is desirable to provide the HR department with an overview of upcoming birthdays.\u00a0\u00a0\u00a0\u00a0<\/p>\n<h2>Set up the Contacts list<\/h2>\n<p>To create the overview of upcoming birthdays a standard SharePoint Contacts list is used extended with a column with the name \u2018Date of birth\u2019 of type Date and Time. To display some additional information in the overview the column \u2018Department\u2019, type single line of text, is added to the list.<br \/>\nThe list is filled with some data:<br \/>\n<a href=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2011\/01\/BirthdayContactsList.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-975\" title=\"BirthdayContactsList\" src=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2011\/01\/BirthdayContactsList.png\" alt=\"\" width=\"593\" height=\"115\" srcset=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2011\/01\/BirthdayContactsList.png 741w, https:\/\/www.itidea.nl\/wp-content\/uploads\/2011\/01\/BirthdayContactsList-300x58.png 300w\" sizes=\"auto, (max-width: 593px) 100vw, 593px\" \/><\/a><\/p>\n<h2>Create the overview\u00a0\u00a0\u00a0\u00a0<\/h2>\n<p>The SPServices operation GetListItems is used to get the data from the Contacts list:\u00a0\u00a0\u00a0\u00a0<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\r\n$().SPServices({\r\n\u00a0\u00a0\u00a0 operation: &quot;GetListItems&quot;,\r\n\u00a0\u00a0\u00a0 async: false,\r\n\u00a0\u00a0\u00a0 debug: true,\r\n\u00a0\u00a0\u00a0 listName: &quot;Contacts&quot;,\r\n\u00a0\u00a0\u00a0 CAMLViewFields: &quot;&lt;ViewFields&gt;&lt;\/ViewFields&gt;&quot;,\r\n\u00a0\u00a0\u00a0 completefunc: function (xData, Status) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $(xData.responseXML).find(&quot;&#x5B;nodeName=z:row]&quot;).each(function () {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 daysleft = CalculateDaysLeft($(this).attr(&quot;ows_Date_x0020_of_x0020_birth&quot;));\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 contacts.push({ FirstName: $(this).attr(&quot;ows_FirstName&quot;), LastName: $(this).attr(&quot;ows_Title&quot;), Department: $(this).attr(&quot;ows_Department&quot;), DaysLeft: daysleft });\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 });\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 SortByDaysLeft(contacts);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $(&quot;#showContacts&quot;).html(&quot;&quot;);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $(&quot;#contactTemplate&quot;).tmpl(contacts)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 .appendTo(&quot;#showContacts&quot;);\r\n\u00a0\u00a0\u00a0 }\r\n});\r\n<\/pre>\n<p>The callback function gets the response of the web service. The function CalculateDaysLeft calculates the days left until the date stored in \u2018Date of birth\u2019 will occur in the current year. Since this is a helper method it is not showed here.<br \/>\nThe firstname, lastname, department and the daysleft are stored in an array of objects named contacts. To display the birthdays in upcoming order the function SortByDaysLeft is called. This is also a helper function and for readability not displayed here.<br \/>\nA jQuery template, contactTemplate, is used to show the results on the page. The template is rendered with the data and appended to a div with the id showContacts. \u00a0The div functions as the template container.<br \/>\nThe template uses the objects in the array:\u00a0<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n&lt;script id=&quot;contactTemplate&quot; type=&quot;text\/x-jquery-tmpl&quot;&gt;\r\n\u00a0&lt;div&gt;\r\n\u00a0\u00a0\u00a0 ${FirstName} ${LastName}\r\n\u00a0\u00a0\u00a0 &lt;ul style=&quot;list-style-type:none;&quot;&gt;\r\n\u00a0\u00a0\u00a0 &lt;li&gt;${Department}&lt;\/li&gt;\r\n\u00a0\u00a0\u00a0 &lt;li&gt;${DaysLeft} days left until birthday!!&lt;\/li&gt;\r\n\u00a0\u00a0\u00a0 &lt;\/ul&gt;\r\n &lt;\/div&gt;\r\n&lt;\/script&gt;\r\n<\/pre>\n<p>The first thing you\u2019ll probably notice is the script tags. Because of these tags the template is embedded in the body of the page. In the template html can be used to format the data to be displayed. The ${FirstName}, ${LastName}, ${Departement} and ${DaysLeft} expressions are used in the template as placeholders for the data present in the array of objects \u2018contacts\u2019. ${..} tells the parser the fields have to be replaced with the values passed in the template by the array of objects.\u00a0\u00a0\u00a0\u00a0<\/p>\n<p>The template is rendered once for each item in the array named contacts.<br \/>\nThe upcoming birthdays are displayed with the contactTemplate as shown in the figure below:<br \/>\n<a href=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2011\/01\/BirthdaySimple.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-978\" title=\"BirthdaySimple\" src=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2011\/01\/BirthdaySimple.png\" alt=\"\" width=\"210\" height=\"296\" \/><\/a><\/p>\n<p>This template is quite simple and fulfills the purpose of displaying upcoming birthdays from the Contacts list.<\/p>\n<p>The template displays \u2018.. days left until birthday!\u2019 for all items. It\u2019s much nicer to display a message based on the number of days left to provide the HR department with some additional information.<br \/>\nThis can be accomplished with code in the template itself with an \u2018if ..else..\u2019 statement as shown in the code below.\u00a0<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n&lt;script id=&quot;contactTemplateInlineCode&quot; type=&quot;text\/x-jquery-tmpl&quot;&gt;\r\n\u00a0&lt;div&gt;\r\n\u00a0\u00a0\u00a0 ${FirstName} ${LastName}\r\n\u00a0\u00a0\u00a0 &lt;ul style=&quot;list-style-type:none;&quot;&gt;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &lt;li&gt;&lt;a href=&quot;#&quot; id=${Department} title=&quot;Show all employees of ${Department}&quot;&gt;${Department}&lt;\/a&gt;&lt;\/li&gt;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &lt;li&gt;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {{if (DaysLeft &lt; 10)}} Did you order flowers already? It's almost his birthday, only ${DaysLeft} days!\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {{else DaysLeft &lt; 150}} It takes a little while until his birthday: ${DaysLeft} days!\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {{else (DaysLeft &lt; 360 )}} A long time until his birthday: ${DaysLeft} days!\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {{else (DaysLeft &gt; 359 )}} If you didn't sent any flowers, you're too late now...{{\/if}}\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &lt;\/li&gt;\r\n\u00a0\u00a0\u00a0 &lt;\/ul&gt;\r\n &lt;\/div&gt;\r\n&lt;\/script&gt;\r\n<\/pre>\n<p>Besides adjusting the text based of the number of days left, the Department is adjusted to be a link. When selecting the link the function GetAllContactsFromDepartment is called with the ID of the link. The ID of the link is set with the Department value. The values of the array used by the template can be used everywhere in the template. The function GetAllContactsFromDepartment will display the firstname, lastname and day and month of the birthdate of the specified Department with the help of the SPServices library operation GetListItems.<br \/>\nThe results of this operation are stored in an array of objects and another template is rendered with this array.<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\u00a0$().SPServices({\r\n\u00a0\u00a0\u00a0 operation: &quot;GetListItems&quot;,\r\n\u00a0\u00a0\u00a0 async: false,\r\n\u00a0\u00a0\u00a0 debug: true,\r\n\u00a0\u00a0\u00a0 listName: &quot;Contacts&quot;,\r\n\u00a0\u00a0\u00a0 CAMLViewFields: &quot;&lt;ViewFields&gt;&lt;\/ViewFields&gt;&quot;,\r\n\u00a0\u00a0\u00a0 CAMLQuery: &quot;&lt;Query&gt;&lt;Where&gt;&lt;Eq&gt;&lt;FieldRef Name='Department'\/&gt;&lt;Value Type='Text'&gt;&quot; + Department + &quot;&lt;\/Value&gt;&lt;\/Eq&gt;&lt;\/Where&gt;&lt;\/Query&gt;&quot;,\r\n\u00a0\u00a0\u00a0 completefunc: function (xData, Status) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $(xData.responseXML).find(&quot;&#x5B;nodeName=z:row]&quot;).each(function () {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 var temp = $(this).attr(&quot;ows_Date_x0020_of_x0020_birth&quot;);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 var formattedDate = &quot;&quot;;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 if (temp.length &gt; 0) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 var month = temp.substring(5, 7);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 var day = temp.substring(8, 10);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 formattedDate = day + &quot; &quot; + month;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 daysLeft = CalculateDaysLeft(temp);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 contactsOfDepartment.push({ FirstName: $(this).attr(&quot;ows_FirstName&quot;), LastName: $(this).attr(&quot;ows_Title&quot;), BirthDate: formattedDate, DaysLeft: daysLeft });\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 });\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 SortByDaysLeft(contactsOfDepartment);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $(&quot;#showContactsFromDepartment&quot;).html(&quot;&lt;h4&gt;&quot; + Department + &quot;&lt;\/h4&gt;&quot;);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $(&quot;#DepartmentTemplate&quot;).tmpl(contactsOfDepartment)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 .appendTo(&quot;#showContactsFromDepartment&quot;);\r\n\u00a0\u00a0\u00a0 }\r\n});\r\n<\/pre>\n<p>The DepartmentTemplate used in the code above:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n&lt;script id=&quot;DepartmentTemplate&quot; type=&quot;text\/x-jquery-tmpl&quot;&gt;\r\n&lt;div style=&quot;float:left;width:100%&quot;&gt;\r\n\u00a0\u00a0\u00a0 &lt;div style=&quot;float:left;width:15%&quot;&gt;\r\n\u00a0\u00a0\u00a0\u00a0 ${FirstName} ${LastName}\r\n\u00a0\u00a0\u00a0 &lt;\/div&gt;\r\n\u00a0\u00a0\u00a0 &lt;div style=&quot;float:right;width:85%&quot;&gt;\r\n\u00a0\u00a0\u00a0\u00a0 ${BirthDate}\r\n\u00a0\u00a0\u00a0 &lt;\/div&gt;\r\n&lt;\/div&gt;\r\n&lt;\/script&gt;\r\n<\/pre>\n<p>So within a template a call to fill another template with data can be made.\u00a0\u00a0\u00a0\u00a0<\/p>\n<p>The upcoming birthdays are now displayed as in the figure below:\ufffd<br \/>\n<a href=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2011\/01\/BirthdayExtended.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-977\" title=\"BirthdayExtended\" src=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2011\/01\/BirthdayExtended.png\" alt=\"\" width=\"442\" height=\"302\" srcset=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2011\/01\/BirthdayExtended.png 442w, https:\/\/www.itidea.nl\/wp-content\/uploads\/2011\/01\/BirthdayExtended-300x204.png 300w\" sizes=\"auto, (max-width: 442px) 100vw, 442px\" \/><\/a><\/p>\n<p>When selecting the Marketing department another template will be filled with data:<br \/>\n<a href=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2011\/01\/BirthdayDepartment.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-976\" title=\"BirthdayDepartment\" src=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2011\/01\/BirthdayDepartment.png\" alt=\"\" width=\"285\" height=\"61\" \/><\/a><\/p>\n<p>All the code displayed in this post is based on jQuery. With references to jQuery, the SPServices library and the jQuery template plugin all the code can be pasted in a Content Editor web part on a SharePoint page.\u00a0\u00a0\u00a0\u00a0<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n&lt;script src=&quot;\/jQueryLibrary\/jquery-1.4.4.min.js&quot; type=&quot;text\/javascript&quot;&gt;&lt;\/script&gt;\r\n&lt;script src=&quot;\/jQueryLibrary\/jquery.SPServices-0.5.6.min.js&quot; type=&quot;text\/javascript&quot;&gt;&lt;\/script&gt;\r\n&lt;script src=&quot;\/jQueryLibrary\/jquery.tmpl.js&quot; type=&quot;text\/javascript&quot;&gt;&lt;\/script&gt;\r\n<\/pre>\n<h2>Summary\u00a0\u00a0\u00a0\u00a0<\/h2>\n<p>By combining libraries like SPServices and jQuery templates it is quite straightforward to display a list of upcoming birthdays from a standard SharePoint Contacts list.\u00a0\u00a0\u00a0\u00a0<\/p>\n<p>The strength of the use of SPServices here is to get the necessary data in a straightforward way; the strength of the use of jQuery templates is to display the data in a nicely formatted way. Using a combination of both libraries I think the best of both worlds are used the appropriate way.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The use of jQuery templates is very neat. It easily displays content in a structured way according to a defined template. Microsoft created the jQuery template plugin (https:\/\/github.com\/nje\/jquery-tmpl) which can be used by referencing the template script file.\u00a0 In this &#8230; <a class=\"more-link\" href=\"https:\/\/www.itidea.nl\/index.php\/spservices-and-jquery-templates-upcoming-birthday-overview\/\">Read More &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[11,42,33],"class_list":["post-967","post","type-post","status-publish","format-standard","hentry","category-sharepoint-2010","tag-jquery","tag-sharepoint-2010","tag-spservices"],"_links":{"self":[{"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/posts\/967","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/comments?post=967"}],"version-history":[{"count":13,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/posts\/967\/revisions"}],"predecessor-version":[{"id":2037,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/posts\/967\/revisions\/2037"}],"wp:attachment":[{"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/media?parent=967"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/categories?post=967"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/tags?post=967"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}