Anniversary Web part SharePoint JavaScript




  1. Create a Refinable Property like Refinabledate according to the requirement. Go to site settings > Search schema. In the managed properties, search for refinabledate00(if this is already mapped select an empty one) and map it to the SPS-Anniversary crawled property(by default sharepoint crawls the AD user profile properties with a prefix of SPS-).
  2. Run a full crawl for the refinabledate to get all the info generated. To test if refinable property has values run a search query
  3. https://yoursitecollurlhere/_api/search/query?querytext=%27*%27&sourceid=%27B09A7990-05EA-4AF9-81EF-EDFAB16C4E31%27&selectproperties=%27Title,Refinabledate01,Path%27&sortlist=%27Refinabledate01:ascending%27&rowlimit=500
  4. Once you get a set of results as below, you can paste the url into the below code
  5. 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.
  6. The use the HTML to display the results.
  7. Let me know if you have any questions or if we can further simplify the code.
  8. Just copy the code to js file and add content editor webpart and reference.
  9. 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="resultsDiv1"></div>
<script type="text/javascript">// <![CDATA[
$(document).ready(function () {
SP.SOD.executeFunc('sp.js');
   ExecuteOrDelayUntilScriptLoaded(executeQuery1, "sp.js");
   
});
Date.prototype.AddDays=function(days)
{
    this.setDate(this.getDate() + days);
    return this;
}
function executeQuery1() {
    Results = {
        element: '',
        url: '',
        init: function (element) {
            Results.element = element;
        var Anniversary = 'Anniversary01';
        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 += Anniversary + gt + quote + day + '-' + startMonth + '-' + '2000' + quote + space + 'AND' + space + Anniversary + lt + quote + endDay + '-' + endMonth + '-' + '2000' + quote;
        }
        else
        {
        querytext += Anniversary + gt + quote + day + '-' + startMonth + '-' + '2000' + quote + space + 'OR' + space + Anniversary + lt + quote + endDay + '-' + endMonth + '-' + '2000' + quote;
        }
            Results.url = _spPageContextInfo.webAbsoluteUrl + "/_api/search/query?querytext=%27" + querytext + "%27&sourceid=%27B09A7990-05EA-4AF9-81EF-EDFAB16C4E31%27&selectproperties=%27Title,"+ Anniversary +",Path%27&sortlist=%27"+ Anniversary +":ascending%27";
Results.url ="yoursitecollurlhere/_api/search/query?querytext=%27*%27&sourceid=%27B09A7990-05EA-4AF9-81EF-EDFAB16C4E31%27&selectproperties=%27Title,Refinabledate01,Path%27&sortlist=%27Refinabledate01:ascending%27&rowlimit=500"
        },
        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='Anniversary'>";
            for (var i = 0; i < results.length; i++) {
//"04-Aug" is the format that is getting saved as----"1-Feb"
var Anniversary = [];
var month1 = [];
var day1 = [];
Anniversary = results[i].Cells.results[3].Value;
if(Anniversary.length == 21){
currentAni = Anniversary.slice(0,4);
Aniyear = Anniversary.slice(5,9);
}else if (Anniversary.length == 22){
currentAni = Anniversary.slice(0,5);
Aniyear = Anniversary.slice(6,10);
}else if (Anniversary.length == 20){
currentAni = Anniversary.slice(0,3);
Aniyear = Anniversary.slice(4,8)
}
/*if(Anniversary.Length = 5){
day1 = Anniversary.slice(0,1);
    month1 = Anniversary.slice(2,6);
}else{
day1 = Anniversary.slice(0,2);
    month1 = Anniversary.slice(3,6);
}*/
        var name = results[i].Cells.results[2].Value;
//Get Current Date and MOnth
var m_names = new Array("1", "2", "3", "4", "5", "6", "7", "8", "9","10", "11", "12");

var d = new Date();
var curr_date = d.getDate();
curr_date5 = curr_date+5;
curr_date4 = curr_date+4;
curr_date3 = curr_date+1;
curr_date2 = curr_date+2;
curr_date1 = curr_date+3;
prev_date1 = curr_date-1;
prev_date2 = curr_date-2;
var curr_month = d.getMonth();
Prevddmm2 = m_names[curr_month] + "/" + prev_date2;
Prevddmm1 = m_names[curr_month] + "/" + prev_date1;
Currddmm5 = m_names[curr_month] + "/" + curr_date5 ;
Currddmm4 = m_names[curr_month]  + "/" + curr_date4 ;
Currmmdd =  m_names[curr_month] + "/" + curr_date;
Currddmm3 = m_names[curr_month]  + "/" + curr_date3;
Currddmm2 =  m_names[curr_month] + "/" + curr_date2;
Currddmm1 = m_names[curr_month]  + "/" + curr_date1;
var curr_year = (new Date()).getFullYear();
if((currentAni == Currmmdd) || (currentAni == Currddmm3) || (currentAni == Currddmm2) || (currentAni == Currddmm1)|| (currentAni == Currddmm4)|| (currentAni == Currddmm5)|| (currentAni == Prevddmm1)||(currentAni == Prevddmm2)){
//alert(name + " Anniversary is " + Anniversary); 
var years = curr_year - Aniyear;
var date = new Date(Date.parse(results[i].Cells.results[3].Value));
                var link = results[i].Cells.results[5].Value
html += "<span>";
                html += currentAni;
                html += " "
                html += "<a href='"+link+"'>" + name + "</a>" + " - " + years + "y" + "<br>";
                html += " ";
            }}
        if (results.length == 0)
            {
              html += "No Anniversarys found";
            }
            html += "</div>";
            Results.element.html(html);
        },
        onError: function (err) {
            alert(JSON.stringify(err));
        }
    }
    Results.init($('#resultsDiv1'));
    Results.load();
}
// ]]>
</script>












































Comments

Popular posts from this blog

Birthday Javascript Sharepoint 2013

Multivalue Dropdown Refiner Sharepoint 2013

Event Registration Sharepoint 2013