Advertisement
yaramohamed78

Sans HR - V1

Jun 12th, 2017
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
ASP 9.06 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.         alert("Before");
  25.         // testFn();
  26.         getUserName();
  27.         SP.SOD.executeFunc('sp.js', 'SP.ClientContext', getUrl);
  28.         //getUserName();
  29.         alert("after");
  30.         //testFn();
  31.     });
  32.  
  33.     function testFn()
  34.     {
  35.         alert("Welcome");
  36.         console.log("Test");
  37.     }
  38.     // This function prepares, loads, and then executes a SharePoint query to get the current users information
  39.     function getUserName()
  40.     {
  41.         //alert("Here");
  42.         //console.log("Start");
  43.         //var bool = true;
  44.         context.load(user);
  45.         //console.log("Inside getUserNameFn");
  46.       //if (bool)
  47.       //  {
  48.       //      console.log("Inside IF");
  49.       //      context.executeQueryAsync(getEmployeeType, onGetUserNameFail);
  50.       //  }
  51.         context.executeQueryAsync(onGetUserNameSuccess, onGetUserNameFail);
  52.     }
  53.  
  54.     // This function is executed if the above call is successful
  55.     // It replaces the contents of the 'message' element with the user name
  56.     function onGetUserNameSuccess()
  57.     {
  58.         $('#message').text('Hello ' + user.get_title());
  59.     }
  60.  
  61.     // This function is executed if the above call fails
  62.     function onGetUserNameFail(sender, args) {
  63.         alert('Failed to get user name. Error:' + args.get_message());
  64.     }
  65.  
  66.     function getEmployeeType()
  67.     {
  68.         //---------------------------------------------------------------------------
  69.         console.log("Inside the fn");
  70.         hostweburl = getQueryStringParameter("SPHostUrl");
  71.         hostweburl = hostweburl.replace("%2FUserInbox", "");
  72.         appweburl = getQueryStringParameter("SPAppWebUrl");
  73.         hostweburl = decodeURIComponent(hostweburl);
  74.         appweburl = decodeURIComponent(appweburl);
  75.         context = new SP.ClientContext(appweburl);
  76.         var factory = new SP.ProxyWebRequestExecutorFactory(appweburl);
  77.         context.set_webRequestExecutorFactory(factory);
  78.         appContextSite = new SP.AppContextSite(context, hostweburl);
  79.         web = appContextSite.get_web();
  80.         context.load(web);
  81.         list = web.get_lists().getByTitle("Employees");
  82.         //---------------------------------------------------------------------
  83.         //-------------------------------------------------------------------
  84.         console.log(list);
  85.       //  var camlQuery = new SP.CamlQuery();
  86.        // camlQuery.set_viewXml(
  87.        //     '<View><Query><Where><EQ><FieldRef Name="Employess-Name"/><Value Type='Choice'>user</Value><Eq></Where></Query></View>'
  88.         //);
  89.        
  90.        // var camlQuery = new SP.CamlQuery();
  91.        // camlQuery.set_viewXml('<View><Query><Where><EQ><FieldRef Name=\'Employees-Name\'/>' +
  92.          //   '<Value Type=\'text\'>Yara Elsaiegh</Value></EQ></Where></Query><RowLimit>10</RowLimit></View>');
  93.        
  94.         // collListItem = list.getItems(camlQuery);
  95.         // context.load(collListItem);
  96.         // console.log("welcome");
  97.         // console.log(collListItem);
  98.     }
  99.  
  100.     function getUrl() {
  101.         hostweburl = getQueryStringParameter("SPHostUrl");
  102.         hostweburl = hostweburl.replace("%2FUserInbox","");
  103.         appweburl = getQueryStringParameter("SPAppWebUrl");
  104.         hostweburl = decodeURIComponent(hostweburl);
  105.         appweburl = decodeURIComponent(appweburl);
  106.         alert(hostweburl);
  107.         alert(appweburl);
  108.         var scriptbase = hostweburl + "/_layouts/15/";
  109.         alert(scriptbase);
  110.         $.getScript(scriptbase + "SP.Runtime.js",
  111.             function () {
  112.                 $.getScript(scriptbase + "SP.js",
  113.                 function () { $.getScript(scriptbase + "SP.RequestExecutor.js", execOperation); }
  114.                 );
  115.             }
  116.         );
  117.         event.preventDefault();
  118.     }
  119.  
  120.     function execOperation()
  121.     {
  122.         context = new SP.ClientContext(appweburl);
  123.         var factory = new SP.ProxyWebRequestExecutorFactory(appweburl);
  124.         context.set_webRequestExecutorFactory(factory);
  125.         appContextSite = new SP.AppContextSite(context, hostweburl);
  126.         web = appContextSite.get_web();
  127.         context.load(web);
  128.         list = web.get_lists().getByTitle("AppraisalRequests");
  129.         employeeList = web.get_lists().getByTitle("Employees");
  130.         alert(list.count);
  131.         alert(employeeList.count);
  132.         var camlQuery = new SP.CamlQuery();
  133.         camlQuery.set_viewXml(
  134.             '<View><Query><Where><IsNotNull><FieldRef Name="ID"/></IsNotNull></Where></Query></View>'
  135.         );
  136.         collListItem = list.getItems(camlQuery);
  137.         camlQuery = new SP.CamlQuery();
  138.         console.log(user.get_title());
  139.         camlQuery.set_viewXml(
  140.           //  "<Where><Eq><FieldRef Name='EmployeeName['$5K_1']' /><Value Type='User'>" + user.get_title() + "</Value></Eq></Where>"
  141.             "<View><Query><Where><Eq><FieldRef Name='EmployeeName' /><Value Type='User'>"+user.get_title()+"</Value></Eq></Where></Query></View>"
  142.             );
  143.        
  144.         //--------------------------------------------------------------------------------------
  145.  
  146.  
  147.         //var r = list.GetItems(q);
  148.         //--------------------------------------------------------------------------------------
  149.  
  150.  
  151.  
  152.         collListItem1 = employeeList.getItems(camlQuery);
  153.         context.load(collListItem);
  154.         context.load(collListItem1);
  155.         context.executeQueryAsync(onSuccess, onFail);
  156.     }
  157.  
  158.     function onSuccess()
  159.     {
  160.         alert("List loaded Successfully");
  161.         var count = collListItem.get_count();
  162.         alert(count);
  163.         var listItemInfo = '';
  164.         var listItemEnumerator = collListItem.getEnumerator();
  165.  
  166.         while (listItemEnumerator.moveNext())
  167.         {
  168.             //  alert("Enter");
  169.             var oListItem = listItemEnumerator.get_current();
  170.             //  alert(oListItem.get_id());
  171.             listItemInfo += '\n Manager: ' + oListItem.get_item('Manager')['$5K_1']+
  172.                 '\n Employee: ' + oListItem.get_item('Employee')['$5K_1'];
  173.            console.log(oListItem.get_item('Manager'));
  174.            console.log(oListItem.get_item('Employee'));
  175.  
  176.         }
  177.         alert(listItemInfo.toString());
  178.         alert("Here");
  179.         var count = collListItem1.get_count();
  180.         alert(count);
  181.         var listItemInfo = '';
  182.         var listItemEnumerator = collListItem1.getEnumerator();
  183.  
  184.         while (listItemEnumerator.moveNext()) {
  185.             //  alert("Enter");
  186.             var oListItem = listItemEnumerator.get_current();
  187.             //  alert(oListItem.get_id());
  188.             listItemInfo += '\n Employee: ' + oListItem.get_item('EmployeeName')['$5K_1'];
  189.             console.log(oListItem.get_item('EmployeeName'));
  190.             console.log(oListItem.get_item('EmployeeType'));
  191.             if (oListItem.get_item('EmployeeType') == "Manager")
  192.             {
  193.                 isManager = true;
  194.             }
  195.  
  196.         }
  197.         alert(isManager);
  198.         alert(listItemInfo.toString());
  199.         getRequests();
  200.  
  201.     }
  202.  
  203.     function getRequests()
  204.     {
  205.         //Employee
  206.         if (isManager == false)
  207.         {
  208.             requestList = web.get_lists().getByTitle("AppraisalRequests");
  209.         }
  210.  
  211.  
  212.  
  213.  
  214.  
  215.  
  216.         //list = web.get_lists().getByTitle("AppraisalRequests");
  217.         //employeeList = web.get_lists().getByTitle("Employees");
  218.         //alert(list.count);
  219.         //alert(employeeList.count);
  220.         //var camlQuery = new SP.CamlQuery();
  221.         //camlQuery.set_viewXml(
  222.         //    '<View><Query><Where><IsNotNull><FieldRef Name="ID"/></IsNotNull></Where></Query></View>'
  223.         //);
  224.         //collListItem = list.getItems(camlQuery);
  225.         //camlQuery = new SP.CamlQuery();
  226.         //console.log(user.get_title());
  227.         //camlQuery.set_viewXml(
  228.         //  //  "<Where><Eq><FieldRef Name='EmployeeName['$5K_1']' /><Value Type='User'>" + user.get_title() + "</Value></Eq></Where>"
  229.         //    "<View><Query><Where><Eq><FieldRef Name='EmployeeName' /><Value Type='User'>" + user.get_title() + "</Value></Eq></Where></Query></View>"
  230.         //    );
  231.  
  232.  
  233.  
  234.  
  235.  
  236.  
  237.     }
  238.  
  239.     // This function is executed if the above call fails
  240.     function onFail(sender, args) {
  241.         alert(args.get_message());
  242.     }
  243.     function getQueryStringParameter(paramToRetrieve) {
  244.         var params =
  245.             document.URL.split("?")[1].split("&");
  246.         for (var i = 0; i < params.length; i = i + 1) {
  247.             var singleParam = params[i].split("=");
  248.             if (singleParam[0] == paramToRetrieve)
  249.                 return singleParam[1];
  250.         }
  251.     }
  252.  
  253.  
  254. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement