Birthday Javascript Sharepoint 2013
Below is the code to pull birthdays using REST API search Sharepoint 2013.
<div id="resultsDiv"></div>
<script type="text/javascript">// <![CDATA[
$(document).ready(function () {
var e = ExecuteOrDelayUntilScriptLoaded(executeQuery(), "sp.js");
});
Date.prototype.AddDays=function(days)
{
this.setDate(this.getDate() + days);
return this;
}
function executeQuery() {
Results = {
element: '',
url: '',
init: function (element) {
Results.element = element;
var birthday = 'Birthday01';
var space = '%20'; var colon = '%3A'; var quote = '%22'; var gt = '%3E'; var lt = '%3C'; var amp = '&';
// Get current date
var currentTime = new Date();
var startMonth = currentTime.getMonth()+1;
var day = currentTime.getDate();
// Get current date + 30
var endTime = new Date();
var endTime = currentTime.AddDays(30);
var endMonth = endTime.getMonth()+1;
var endDay = endTime.getDate();
var querytext = "";
// build query with the magic 2000 year
if(startMonth!='12')
{
querytext += birthday + gt + quote + day + '-' + startMonth + '-' + '2000' + quote + space + 'AND' + space + birthday + lt + quote + endDay + '-' + endMonth + '-' + '2000' + quote;
}
else
{
querytext += birthday + gt + quote + day + '-' + startMonth + '-' + '2000' + quote + space + 'OR' + space + birthday + lt + quote + endDay + '-' + endMonth + '-' + '2000' + quote;
}
//create your own query dynamically using below url or use fixed url to get all the results using *.
Results.url = _spPageContextInfo.webAbsoluteUrl + "/_api/search/query?querytext=%27" + querytext + "%27&sourceid=%27B09A7990-05EA-4AF9-81EF-EDFAB16C4E31%27&selectproperties=%27Title,"+ birthday +",Path%27&sortlist=%27"+ birthday +":ascending%27";
Results.url ="https://yoursitecollurlhere/_api/search/query?querytext=%27Refinabledate00%3E%222-7-2000%22%27&sourceid=%27B09A7990-05EA-4AF9-81EF-EDFAB16C4E31%27&selectproperties=%27Title,Refinabledate00,RefinableString02,Path%27&sortlist=%27RefinableString02:ascending%27&rowlimit=50"
},
load: function () {
$.ajax(
{
url: Results.url,
method: "GET",
headers: {
"accept": "application/json; odata=verbose",
},
success: Results.onSuccess,
error: Results.onError
}
);
},
onSuccess: function (data) {
var results = data.d.query.PrimaryQueryResult.RelevantResults.Table.Rows.results;
var months = [ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ];
var html = "<div class='birthday'>";
for (var i = 0; i < results.length; i++) {
//"04-Aug" is the format that is getting saved as----"1-Feb"
var Birthdate = [];
var month1 = [];
var day1 = [];
Birthdate = results[i].Cells.results[4].Value;
if(Birthdate.Length = 5){
day1 = Birthdate.slice(0,1);
month1 = Birthdate.slice(2,6);
}else{
day1 = Birthdate.slice(0,2);
month1 = Birthdate.slice(3,6);
}
if(day1 + month1 = months[date.getMonth()])
var name = results[i].Cells.results[2].Value;
//Get Current Date and MOnth
var m_names = new Array("Jan", "Feb", "Mar",
"Apr", "May", "Jun", "Jul", "Aug", "Sep",
"Oct", "Nov", "Dec");
var d = new Date();
var curr_date = d.getDate();
curr_date1 = curr_date+3;
var curr_month = d.getMonth();
Currddmmm = curr_date + "-" + m_names[curr_month];
Currddmm3 = curr_date1 + "-" + m_names[curr_month];
document.getElementById("demo").innerHTML = n;
if(Birthdate = Currddmmm){
alert(name + "birthday is" + Birthdate);
}
var date = new Date(Date.parse(results[i].Cells.results[3].Value));
html += "<span>";
html += "<a href='"+link+"'>" + name + "</a>";
html += " "
html += date.getDate() + " "+ months[date.getMonth()];
html += " ";
}
if (results.length == 0)
{
html += "No Results Found";
}
html += "</div>";
Results.element.html(html);
},
onError: function (err) {
alert(JSON.stringify(err));
}
}
Results.init($('#resultsDiv'));
Results.load();
}
// ]]></script>
- Create a Refinable Property like RefinableString or Refinabledate according to the requirement. In my case I had to try both and had an issue with Refinabledate since the month was getting appended to the year. Lets say someone has mar 23rd as birthday in User profile property, the date was showing as 01-03-2023. Hence I had to assign it to a string.
- Since in our environment it was a dd-mmm(no year) format which was a custom profile property, It was showing 23rd-March in my case.
- Once RefinableString02 was assigned it was showing the same dd-mmm format.
- For getting the right url you need to have the querytext as * and source id to the people, and select the properties required which is "Title,RefinableString02,Path".
- Then fire an ajax query as below with the rest url as below:https://yoursitecollurl/_api/search/query?querytext=%27Refinabledate00%3E%222-7-2000%22%27&sourceid=%27B09A7990-05EA-4AF9-81EF-EDFAB16C4E31%27&selectproperties=%27Title,Refinabledate00,RefinableString02,Path%27&sortlist=%27RefinableString02:ascending%27&rowlimit=50
- Once you get results for the query, iterate through results with condition according to which dates you want to show the birthdays for. For this you need to get the current date which is in same format as the date that your refinablestring is displaying and compare it inside the iteration block.
- The use the HTML to display the results.
- Let me know if you have any questions or if we can further simplify the code.
- Just copy the code to js file and add content editor webpart and reference.
- Use chrome debugger tools to see what the results look like. You can copy from the results if you want to get a key value pair. Just right click on the pop-up and copy path.
<div id="resultsDiv"></div>
<script type="text/javascript">// <![CDATA[
$(document).ready(function () {
var e = ExecuteOrDelayUntilScriptLoaded(executeQuery(), "sp.js");
});
Date.prototype.AddDays=function(days)
{
this.setDate(this.getDate() + days);
return this;
}
function executeQuery() {
Results = {
element: '',
url: '',
init: function (element) {
Results.element = element;
var birthday = 'Birthday01';
var space = '%20'; var colon = '%3A'; var quote = '%22'; var gt = '%3E'; var lt = '%3C'; var amp = '&';
// Get current date
var currentTime = new Date();
var startMonth = currentTime.getMonth()+1;
var day = currentTime.getDate();
// Get current date + 30
var endTime = new Date();
var endTime = currentTime.AddDays(30);
var endMonth = endTime.getMonth()+1;
var endDay = endTime.getDate();
var querytext = "";
// build query with the magic 2000 year
if(startMonth!='12')
{
querytext += birthday + gt + quote + day + '-' + startMonth + '-' + '2000' + quote + space + 'AND' + space + birthday + lt + quote + endDay + '-' + endMonth + '-' + '2000' + quote;
}
else
{
querytext += birthday + gt + quote + day + '-' + startMonth + '-' + '2000' + quote + space + 'OR' + space + birthday + lt + quote + endDay + '-' + endMonth + '-' + '2000' + quote;
}
//create your own query dynamically using below url or use fixed url to get all the results using *.
Results.url = _spPageContextInfo.webAbsoluteUrl + "/_api/search/query?querytext=%27" + querytext + "%27&sourceid=%27B09A7990-05EA-4AF9-81EF-EDFAB16C4E31%27&selectproperties=%27Title,"+ birthday +",Path%27&sortlist=%27"+ birthday +":ascending%27";
Results.url ="https://yoursitecollurlhere/_api/search/query?querytext=%27Refinabledate00%3E%222-7-2000%22%27&sourceid=%27B09A7990-05EA-4AF9-81EF-EDFAB16C4E31%27&selectproperties=%27Title,Refinabledate00,RefinableString02,Path%27&sortlist=%27RefinableString02:ascending%27&rowlimit=50"
},
load: function () {
$.ajax(
{
url: Results.url,
method: "GET",
headers: {
"accept": "application/json; odata=verbose",
},
success: Results.onSuccess,
error: Results.onError
}
);
},
onSuccess: function (data) {
var results = data.d.query.PrimaryQueryResult.RelevantResults.Table.Rows.results;
var months = [ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ];
var html = "<div class='birthday'>";
for (var i = 0; i < results.length; i++) {
//"04-Aug" is the format that is getting saved as----"1-Feb"
var Birthdate = [];
var month1 = [];
var day1 = [];
Birthdate = results[i].Cells.results[4].Value;
if(Birthdate.Length = 5){
day1 = Birthdate.slice(0,1);
month1 = Birthdate.slice(2,6);
}else{
day1 = Birthdate.slice(0,2);
month1 = Birthdate.slice(3,6);
}
if(day1 + month1 = months[date.getMonth()])
var name = results[i].Cells.results[2].Value;
//Get Current Date and MOnth
var m_names = new Array("Jan", "Feb", "Mar",
"Apr", "May", "Jun", "Jul", "Aug", "Sep",
"Oct", "Nov", "Dec");
var d = new Date();
var curr_date = d.getDate();
curr_date1 = curr_date+3;
var curr_month = d.getMonth();
Currddmmm = curr_date + "-" + m_names[curr_month];
Currddmm3 = curr_date1 + "-" + m_names[curr_month];
document.getElementById("demo").innerHTML = n;
if(Birthdate = Currddmmm){
alert(name + "birthday is" + Birthdate);
}
var date = new Date(Date.parse(results[i].Cells.results[3].Value));
html += "<span>";
html += "<a href='"+link+"'>" + name + "</a>";
html += " "
html += date.getDate() + " "+ months[date.getMonth()];
html += " ";
}
if (results.length == 0)
{
html += "No Results Found";
}
html += "</div>";
Results.element.html(html);
},
onError: function (err) {
alert(JSON.stringify(err));
}
}
Results.init($('#resultsDiv'));
Results.load();
}
// ]]></script>
Comments
Post a Comment