Advertisement
yaramohamed78

Sans HR - V2 - Not working

Jun 12th, 2017
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
ASP 7.23 KB | None | 0 0
  1. 'use strict';
  2.  
  3. ExecuteOrDelayUntilScriptLoaded(initializePage, "sp.js");
  4.  
  5. function initializePage()
  6. {
  7.     var context = SP.ClientContext.get_current();
  8.     var user = context.get_web().get_currentUser();
  9.     var context;
  10.     var hostweburl;
  11.     var appweburl;
  12.     var appContextSite;
  13.     var list;
  14.     var employeeList;
  15.     var web;
  16.     var collListItem;
  17.     var collListItem1;
  18.     var isManager = false;
  19.     var requestList;
  20.  
  21.     // This code runs when the DOM is ready and creates a context object which is needed to use the SharePoint object model
  22.     $(document).ready(function ()
  23.     {
  24.         getUserName();
  25.         SP.SOD.executeFunc('sp.js', 'SP.ClientContext', getUrl);
  26.     });
  27.  
  28.     // This function prepares, loads, and then executes a SharePoint query to get the current users information
  29.     function getUserName()
  30.     {
  31.         context.load(user);
  32.         context.executeQueryAsync(onGetUserNameSuccess, onGetUserNameFail);
  33.     }
  34.  
  35.     // This function is executed if the above call is successful
  36.     // It replaces the contents of the 'message' element with the user name
  37.     function onGetUserNameSuccess()
  38.     {
  39.         $('#message').text('Hello ' + user.get_title());
  40.     }
  41.  
  42.     // This function is executed if the above call fails
  43.     function onGetUserNameFail(sender, args) {
  44.         alert('Failed to get user name. Error:' + args.get_message());
  45.     }
  46.  
  47.  
  48.     function getUrl() {
  49.         hostweburl = getQueryStringParameter("SPHostUrl");
  50.         hostweburl = hostweburl.replace("%2FUserInbox","");
  51.         appweburl = getQueryStringParameter("SPAppWebUrl");
  52.         hostweburl = decodeURIComponent(hostweburl);
  53.         appweburl = decodeURIComponent(appweburl);
  54.         alert(hostweburl);
  55.         alert(appweburl);
  56.         var scriptbase = hostweburl + "/_layouts/15/";
  57.         alert(scriptbase);
  58.         $.getScript(scriptbase + "SP.Runtime.js",
  59.             function () {
  60.                 $.getScript(scriptbase + "SP.js",
  61.                 function () { $.getScript(scriptbase + "SP.RequestExecutor.js", execOperation); }
  62.                 );
  63.             }
  64.         );
  65.         event.preventDefault();
  66.     }
  67.     function execOperation()
  68.     {
  69.         context = new SP.ClientContext(appweburl);
  70.         var factory = new SP.ProxyWebRequestExecutorFactory(appweburl);
  71.         context.set_webRequestExecutorFactory(factory);
  72.         appContextSite = new SP.AppContextSite(context, hostweburl);
  73.         web = appContextSite.get_web();
  74.         context.load(web);
  75.         employeeList = web.get_lists().getByTitle("Employees");
  76.         console.log(employeeList);
  77.         var camlQuery = new SP.CamlQuery();
  78.         camlQuery.set_viewXml(
  79.     "<View><Query><Where><Eq><FieldRef Name='EmployeeName' /><Value Type='User'>" + user.get_title() + "</Value></Eq></Where></Query></View>"
  80.     );
  81.         collListItem1 = employeeList.getItems(camlQuery);
  82.         context.load(collListItem1);
  83.         var listItemEnumerator = collListItem1.getEnumerator();
  84.  
  85.         while (listItemEnumerator.moveNext()) {
  86.             console.log("Here");
  87.             var oListItem = listItemEnumerator.get_current();
  88.             listItemInfo += '\n Employee: ' + oListItem.get_item('EmployeeName')['$5K_1'];
  89.             console.log(oListItem.get_item('Employee'));
  90.             console.log(oListItem.get_item('Manager'));
  91.  
  92.         }
  93.         //GET EMPLOYEE TYPE
  94.         getEmployeeType();
  95.     }
  96.     function getEmployeeType()
  97.     {
  98.     //    employeeList = web.get_lists().getByTitle("Employees");
  99.     //    console.log(employeeList);
  100.     //    var camlQuery = new SP.CamlQuery();
  101.     //    camlQuery.set_viewXml(
  102.     //"<View><Query><Where><Eq><FieldRef Name='EmployeeName' /><Value Type='User'>" + user.get_title() + "</Value></Eq></Where></Query></View>"
  103.     //);
  104.     //    collListItem1 = employeeList.getItems(camlQuery);
  105.     //    context.load(collListItem1);
  106.     //    var listItemEnumerator = collListItem1.getEnumerator();
  107.  
  108.     //    while (listItemEnumerator.moveNext())
  109.     //    {
  110.     //        console.log("Here");
  111.     //        var oListItem = listItemEnumerator.get_current();
  112.     //        listItemInfo += '\n Employee: ' + oListItem.get_item('EmployeeName')['$5K_1'];
  113.     //        console.log(oListItem.get_item('Employee'));
  114.     //        console.log(oListItem.get_item('Manager'));
  115.  
  116.     //    }
  117.  
  118.     }
  119.  
  120.     function getRequests()
  121.     {
  122.         list = web.get_lists().getByTitle("AppraisalRequests");
  123.         var camlQuery = new SP.CamlQuery();
  124.         if (isManager == true) {
  125.  
  126.         }
  127.         else
  128.         {
  129.  
  130.         }
  131.     }
  132.  
  133.     //function execOperation()
  134.     //{
  135.  
  136.     //    list = web.get_lists().getByTitle("AppraisalRequests");
  137.        
  138.     //    alert(list.count);
  139.     //    alert(employeeList.count);
  140.     //    var camlQuery = new SP.CamlQuery();
  141.     //    camlQuery.set_viewXml(
  142.     //        '<View><Query><Where><IsNotNull><FieldRef Name="ID"/></IsNotNull></Where></Query></View>'
  143.     //    );
  144.     //    collListItem = list.getItems(camlQuery);
  145.        
  146.     //    console.log(user.get_title());
  147.  
  148.  
  149.  
  150.  
  151.        
  152.     //    context.load(collListItem);
  153.        
  154.     //    context.executeQueryAsync(onSuccess, onFail);
  155.     //}
  156.  
  157.     function onSuccess()
  158.     {
  159.         alert("List loaded Successfully");
  160.         var count = collListItem.get_count();
  161.         alert(count);
  162.         var listItemInfo = '';
  163.         var listItemEnumerator = collListItem.getEnumerator();
  164.  
  165.         while (listItemEnumerator.moveNext())
  166.         {
  167.             //  alert("Enter");
  168.             var oListItem = listItemEnumerator.get_current();
  169.             //  alert(oListItem.get_id());
  170.             listItemInfo += '\n Manager: ' + oListItem.get_item('Manager')['$5K_1']+
  171.                 '\n Employee: ' + oListItem.get_item('Employee')['$5K_1'];
  172.            console.log(oListItem.get_item('Manager'));
  173.            console.log(oListItem.get_item('Employee'));
  174.  
  175.         }
  176.         alert(listItemInfo.toString());
  177.         alert("Here");
  178.         var count = collListItem1.get_count();
  179.         alert(count);
  180.         var listItemInfo = '';
  181.         var listItemEnumerator = collListItem1.getEnumerator();
  182.  
  183.         while (listItemEnumerator.moveNext()) {
  184.             //  alert("Enter");
  185.             var oListItem = listItemEnumerator.get_current();
  186.             //  alert(oListItem.get_id());
  187.             listItemInfo += '\n Employee: ' + oListItem.get_item('EmployeeName')['$5K_1'];
  188.             console.log(oListItem.get_item('EmployeeName'));
  189.             console.log(oListItem.get_item('EmployeeType'));
  190.             if (oListItem.get_item('EmployeeType') == "Manager")
  191.             {
  192.                 isManager = true;
  193.             }
  194.  
  195.         }
  196.         alert(isManager);
  197.         alert(listItemInfo.toString());
  198.         getRequests();
  199.  
  200.     }
  201.  
  202.     // This function is executed if the above call fails
  203.     function onFail(sender, args) {
  204.         alert(args.get_message());
  205.     }
  206.     function getQueryStringParameter(paramToRetrieve) {
  207.         var params =
  208.             document.URL.split("?")[1].split("&");
  209.         for (var i = 0; i < params.length; i = i + 1) {
  210.             var singleParam = params[i].split("=");
  211.             if (singleParam[0] == paramToRetrieve)
  212.                 return singleParam[1];
  213.         }
  214.     }
  215.  
  216.  
  217. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement