Advertisement
yaramohamed78

Sans HR - V2-JS-2017

Jun 22nd, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'use strict';
  2.  
  3. ExecuteOrDelayUntilScriptLoaded(initializePage, "sp.js");
  4.  
  5. function initializePage()
  6. {
  7.     var engLanguage = true;
  8.    // var context = new SP.ClientContext.get_current();
  9.     // var user = context.get_web().get_currentUser();
  10.     var context;
  11.     var user;
  12.     var hostweburl;
  13.     var appweburl;
  14.     var appContextSite;
  15.     var list;
  16.     var employeeList;
  17.     var web;
  18.     var collListItem;
  19.     var collListItem1;
  20.     var collListItem2;
  21.     var isManager = false;
  22.     var requestList;
  23.     var firstLoad = true;
  24.     var tempList = [];
  25.     var link = "https://ejadasharepoint-1786e4322383f6.sharepoint.com/sites/SANSOnlinePortalProject/SANS_HRAppraisalForm/Pages/Default.aspx?SPHostUrl=https%3A%2F%2Fejadasharepoint%2Esharepoint%2Ecom%2Fsites%2FSANSOnlinePortalProject&SPLanguage=en-US&SPClientTag=0&SPProductNumber=16%2E0%2E6601%2E1208&SPAppWebUrl=https%3A%2F%2FEjadaSharePoint-1786e4322383f6%2Esharepoint%2Ecom%2Fsites%2FSANSOnlinePortalProject%2FSANS_HRAppraisalForm";
  26.     var pageLink = "https://ejadasharepoint-1786e4322383fb.sharepoint.com/sites/SANSOnlinePortalProject/SANS_UserInbox/Pages/Default.aspx?SPHostUrl=https%3A%2F%2Fejadasharepoint%2Esharepoint%2Ecom%2Fsites%2FSANSOnlinePortalProject&SPLanguage=en-US&SPClientTag=0&SPProductNumber=16%2E0%2E6614%2E1203&SPAppWebUrl=https%3A%2F%2FEjadaSharePoint-1786e4322383fb%2Esharepoint%2Ecom%2Fsites%2FSANSOnlinePortalProject%2FSANS_UserInbox"
  27.     // This code runs when the DOM is ready and creates a context object which is needed to use the SharePoint object model
  28.     $(document).ready(function ()
  29.     {
  30.         $('#example').on('click', 'tr', function () {
  31.             var rid = $(this).find('td:first').text();
  32.             var state = $(this).find('td').eq(3).html();
  33.             window.open(link + "?rid=" + rid + "&manager=" + isManager + "&state=" + state, "_self");
  34.         });
  35.        
  36.         $("#langBtn").click(function ()
  37.         {
  38.             if (localStorage.getItem('lang') == 'en')
  39.             {
  40.                 localStorage.setItem('lang', 'ar');
  41.                 window.open(pageLink + "&lang=ar", "_self");
  42.               //  window.location.href = window.location.pathname + "?" + $.param({ 'lang': 'ar' });
  43.  
  44.             }
  45.             else
  46.             {
  47.                 localStorage.setItem('lang', 'en');
  48.                 window.open(pageLink + "&lang=en", "_self");
  49.               //  window.location.href = window.location.pathname + "?" + $.param({ 'lang': 'en' });
  50.             }
  51.             //location.reload();
  52.         });
  53.         getUserName();
  54.         SP.SOD.executeFunc('sp.js', 'SP.ClientContext', getUrl);
  55.  
  56.  
  57.     });
  58.  
  59.     // This function prepares, loads, and then executes a SharePoint query to get the current users information
  60.     function getUserName()
  61.     {
  62.         context = new SP.ClientContext.get_current();
  63.         user = context.get_web().get_currentUser();
  64.         context.load(user);
  65.        // context.load(user,'Include(Title)');
  66.         context.executeQueryAsync(onGetUserNameSuccess, onGetUserNameFail);
  67.     }
  68.  
  69.     // This function is executed if the above call is successful
  70.     // It replaces the contents of the 'message' element with the user name
  71.     function onGetUserNameSuccess()
  72.     {
  73.         //console.log('Hello '+ user.get_title());
  74.        // $('#message').text('Hello ' + user.get_title());
  75.     }
  76.  
  77.     // This function is executed if the above call fails
  78.     function onGetUserNameFail(sender, args) {
  79.         alert('Failed to get user name. Error:' + args.get_message());
  80.     }
  81.  
  82.     //Get the url for the sharepoint site
  83.     function getUrl() {
  84.         hostweburl = getQueryStringParameter("SPHostUrl");
  85.         hostweburl = hostweburl.replace("%2FUserInbox", "");
  86.         appweburl = getQueryStringParameter("SPAppWebUrl");
  87.         hostweburl = decodeURIComponent(hostweburl);
  88.         appweburl = decodeURIComponent(appweburl);
  89.         var scriptbase = hostweburl + "/_layouts/15/";
  90.         $.getScript(scriptbase + "SP.Runtime.js",
  91.             function () {
  92.                 $.getScript(scriptbase + "SP.js",
  93.                 function () { $.getScript(scriptbase + "SP.RequestExecutor.js", execOperation); }
  94.                 );
  95.             }
  96.         );
  97.        // event.preventDefault();
  98.     }
  99.  
  100.     function execOperation() {
  101.         context = new SP.ClientContext(appweburl);
  102.         var factory = new SP.ProxyWebRequestExecutorFactory(appweburl);
  103.         context.set_webRequestExecutorFactory(factory);
  104.         appContextSite = new SP.AppContextSite(context, hostweburl);
  105.         web = appContextSite.get_web();
  106.         context.load(web);
  107.         context.executeQueryAsync(getEmployeeInfo, onFail);
  108.     }
  109.  
  110.     function getEmployeeInfo()
  111.     {
  112.  
  113.         employeeList = web.get_lists().getByTitle("Employees");
  114.  
  115.            var camlQuery = new SP.CamlQuery();
  116.             camlQuery.set_viewXml(
  117.                 "<View><Query><Where><Eq><FieldRef Name='EmployeeName' /><Value Type='User'>" + user.get_title() + "</Value></Eq></Where></Query></View>"
  118.                 );
  119.             collListItem1 = employeeList.getItems(camlQuery);
  120.             context.load(collListItem1);
  121.             context.executeQueryAsync(getEmployeeType, onFail);
  122.     }
  123.  
  124.     function getEmployeeType()
  125.     {
  126.  
  127.         var listItemInfo = '';
  128.         var listItemEnumerator = collListItem1.getEnumerator();
  129.  
  130.         while (listItemEnumerator.moveNext()) {
  131.             var oListItem = listItemEnumerator.get_current();
  132.             console.log(oListItem.get_item('EmployeeName'));
  133.             listItemInfo += '\n Employee: ' + oListItem.get_item('EmployeeName')['$5K_1'];
  134.             if (oListItem.get_item('EmployeeType') == "Manager") {
  135.                 isManager = true;
  136.             }
  137.  
  138.         }
  139.         context.executeQueryAsync(getRequestList, onFail);
  140.     }
  141.  
  142.     function getRequestList()
  143.     {
  144.             list = web.get_lists().getByTitle("AppraisalRequests");
  145.             var camlQuery = new SP.CamlQuery();
  146.             if (isManager == true)
  147.             {
  148.                    camlQuery.set_viewXml(
  149.                         "<View><Query><Where><Eq><FieldRef Name='Manager' /><Value Type='User'>" + user.get_title() + "</Value></Eq></Where></Query></View>"
  150.                         );
  151.             }
  152.             else
  153.             {
  154.                 camlQuery.set_viewXml(
  155.                      "<View><Query><Where><Eq><FieldRef Name='Employee' /><Value Type='User'>" + user.get_title() + "</Value></Eq></Where></Query></View>"
  156.                      );
  157.             }
  158.             collListItem = list.getItems(camlQuery);
  159.             context.load(collListItem);
  160.             context.executeQueryAsync(fillRequests, onFail);
  161.  
  162.     }
  163.  
  164.     function  fillRequests()
  165.     {
  166.         var listItemEnumerator = collListItem.getEnumerator();
  167.         //var table = document.getElementById("example").getElementsByTagName('tbody')[0];
  168.         var table = $('#example').DataTable();
  169.         if (firstLoad) {
  170.             $("#example tbody tr").remove();
  171.             firstLoad = false;
  172.         }
  173.         while (listItemEnumerator.moveNext())
  174.         {
  175.             var oListItem = listItemEnumerator.get_current();
  176.             table.row.add([oListItem.get_item('RequestID'), oListItem.get_item('Employee').get_lookupValue(), oListItem.get_item('Manager').get_lookupValue(), oListItem.get_item('RequestStatus'), oListItem.get_item('RequestDate')]).draw();
  177.  
  178.         }
  179.  
  180.     }
  181.  
  182.     // This function is executed if the above call fails
  183.     function onFail(sender, args) {
  184.         alert(args.get_message());
  185.     }
  186.     function getQueryStringParameter(paramToRetrieve) {
  187.         var params =
  188.             document.URL.split("?")[1].split("&");
  189.         for (var i = 0; i < params.length; i = i + 1) {
  190.             var singleParam = params[i].split("=");
  191.             if (singleParam[0] == paramToRetrieve)
  192.                 return singleParam[1];
  193.         }
  194.     }
  195.  
  196.  
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement