Advertisement
yaramohamed78

Sans HR - v3

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