Friday, 18 September 2015

B'day web part using jsom/csom

var Birthday = {};

Birthday.displayLimit = 100;

Birthday.context = null;

Birthday.web = null;

Birthday.listName = 'Employees';

Birthday.data = null;

Birthday.init = function () {
    SP.SOD.executeFunc('sp.js', 'SP.ClientContext', Birthday.load);
};

Birthday.load = function () {
    Birthday.context = new SP.ClientContext.get_current();
    Birthday.web = Birthday.context.get_web();
    Birthday.getTodaysBday();
};
Birthday.getTodaysBday = function () {
    try {
        //
        var d = new Date();
        var month = d.getMonth() + 1;
        var day = d.getDate();

        var list = Birthday.web.get_lists().getByTitle(Birthday.listName);
        var camlQuery = new SP.CamlQuery();
        camlQuery.set_viewXml('<View><Query><Where><And><Eq><FieldRef Name="CalDay" /><Value Type="Number">' + day + '</Value></Eq><Eq><FieldRef Name="CalMonth" /><Value Type="Number">' + month + '</Value></Eq></And></Where><OrderBy><FieldRef Name="Grade" Ascending="True" /></OrderBy></Query><RowLimit>' + Birthday.displayLimit + '</RowLimit></View>');
        Birthday.data = list.getItems(camlQuery);
        Birthday.context.load(Birthday.data);
        Birthday.excecuteQuery(Birthday.onGetTodaysBday, Birthday.onError);
    }
    catch (err) {
        console.log('Error: ' + err.message);
    }
};
Birthday.onGetTodaysBday = function () {
    var name = 'No Birthday Today', exp = '',  url = '#', html = '', href = '', empimage = '', empdesignation = '', emploc = '';
    var bdayListItemEnumerator = Birthday.data.getEnumerator();
    while (bdayListItemEnumerator.moveNext()) {
        var bdayListItem = bdayListItemEnumerator.get_current();
        bdayId = bdayListItem.get_id();
        bdayListItem.get_item('EmpImage') ? empimage = bdayListItem.get_item('EmpImage') : 1;
        bdayListItem.get_item('Title') ? name = bdayListItem.get_item('Title') : 1;
        bdayListItem.get_item('Designation') ? empdesignation = bdayListItem.get_item('Designation') : 1;
        bdayListItem.get_item('Location') ? emploc = bdayListItem.get_item('Location') : 1;

        //orgListItem.get_item('RelatedURL') ? url = orgListItem.get_item('RelatedURL').get_url() : 1;
        href = L_Menu_BaseUrl + '/Lists/Employee/DispForm.aspx?ID=' + bdayListItem.get_item('ID');

        //html += '<dd class="bdayItem"><div class="bdayImage"><img src="' + empimage + '" alt="Emp Image" /></div><div class="bdayDetails"><div class="empName">' + name + " " + lname + '</div><div class="empdesignation">' + empdesignation + '</div><div class="emploc">Location: ' + emploc + '</div></div></dd>';
        html += '<dd class="row"><img src="' + empimage + '" alt="Emp Image" width="80px" height="77px" /><div class="text"><h3>' + name + '</h3><p>Location: ' + emploc + '</p></div></dd>';
       
    }

    if (!Birthday.data.get_count()) {
        html += '<dd><p class="boxTitle">' + name + '</p></dd>';
    }

 
    if (Birthday.data.get_count() > 2) {
        //var urlpath = "http://" + window.location.host + L_Menu_BaseUrl + "/Style%20Library/Yazaki/js/Birthday/jquery.jcarousellite.js";
        //loadjscssfile(urlpath, "js");
        html = '<marquee direction="up" scrollamount="2"  onMouseOver="this.setAttribute(\'scrollamount\', 0, 0);" OnMouseOut="this.setAttribute(\'scrollamount\', 2, 0);">' + html + '</marquee>';
    }
    $('.bdayUsers').html(html);
};
Birthday.onError = function (sender, args) {
    console.log('Request failed. \nError: ' + args.get_message() + '\nStackTrace: ' + args.get_stackTrace());
};
Birthday.excecuteQuery = function (successHandler, errorHandler) {
    Birthday.context.executeQueryAsync(
    Function.createDelegate(this, successHandler),
    Function.createDelegate(this, errorHandler));
};

$(document).ready(function () {
    Birthday.init();
});

No comments:

Post a Comment