Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use strict';
- ExecuteOrDelayUntilScriptLoaded(initializePage, "sp.js");
- function initializePage()
- {
- var context = SP.ClientContext.get_current();
- var user = context.get_web().get_currentUser();
- var context;
- var hostweburl;
- var appweburl;
- var appContextSite;
- var list;
- var employeeList;
- var web;
- var collListItem;
- var collListItem1;
- var isManager = false;
- var requestList;
- // This code runs when the DOM is ready and creates a context object which is needed to use the SharePoint object model
- $(document).ready(function ()
- {
- alert("Before");
- // testFn();
- getUserName();
- SP.SOD.executeFunc('sp.js', 'SP.ClientContext', getUrl);
- //getUserName();
- alert("after");
- //testFn();
- });
- function testFn()
- {
- alert("Welcome");
- console.log("Test");
- }
- // This function prepares, loads, and then executes a SharePoint query to get the current users information
- function getUserName()
- {
- //alert("Here");
- //console.log("Start");
- //var bool = true;
- context.load(user);
- //console.log("Inside getUserNameFn");
- //if (bool)
- // {
- // console.log("Inside IF");
- // context.executeQueryAsync(getEmployeeType, onGetUserNameFail);
- // }
- context.executeQueryAsync(onGetUserNameSuccess, onGetUserNameFail);
- }
- // This function is executed if the above call is successful
- // It replaces the contents of the 'message' element with the user name
- function onGetUserNameSuccess()
- {
- $('#message').text('Hello ' + user.get_title());
- }
- // This function is executed if the above call fails
- function onGetUserNameFail(sender, args) {
- alert('Failed to get user name. Error:' + args.get_message());
- }
- function getEmployeeType()
- {
- //---------------------------------------------------------------------------
- console.log("Inside the fn");
- hostweburl = getQueryStringParameter("SPHostUrl");
- hostweburl = hostweburl.replace("%2FUserInbox", "");
- appweburl = getQueryStringParameter("SPAppWebUrl");
- hostweburl = decodeURIComponent(hostweburl);
- appweburl = decodeURIComponent(appweburl);
- context = new SP.ClientContext(appweburl);
- var factory = new SP.ProxyWebRequestExecutorFactory(appweburl);
- context.set_webRequestExecutorFactory(factory);
- appContextSite = new SP.AppContextSite(context, hostweburl);
- web = appContextSite.get_web();
- context.load(web);
- list = web.get_lists().getByTitle("Employees");
- //---------------------------------------------------------------------
- //-------------------------------------------------------------------
- console.log(list);
- // var camlQuery = new SP.CamlQuery();
- // camlQuery.set_viewXml(
- // '<View><Query><Where><EQ><FieldRef Name="Employess-Name"/><Value Type='Choice'>user</Value><Eq></Where></Query></View>'
- //);
- // var camlQuery = new SP.CamlQuery();
- // camlQuery.set_viewXml('<View><Query><Where><EQ><FieldRef Name=\'Employees-Name\'/>' +
- // '<Value Type=\'text\'>Yara Elsaiegh</Value></EQ></Where></Query><RowLimit>10</RowLimit></View>');
- // collListItem = list.getItems(camlQuery);
- // context.load(collListItem);
- // console.log("welcome");
- // console.log(collListItem);
- }
- function getUrl() {
- hostweburl = getQueryStringParameter("SPHostUrl");
- hostweburl = hostweburl.replace("%2FUserInbox","");
- appweburl = getQueryStringParameter("SPAppWebUrl");
- hostweburl = decodeURIComponent(hostweburl);
- appweburl = decodeURIComponent(appweburl);
- alert(hostweburl);
- alert(appweburl);
- var scriptbase = hostweburl + "/_layouts/15/";
- alert(scriptbase);
- $.getScript(scriptbase + "SP.Runtime.js",
- function () {
- $.getScript(scriptbase + "SP.js",
- function () { $.getScript(scriptbase + "SP.RequestExecutor.js", execOperation); }
- );
- }
- );
- event.preventDefault();
- }
- function execOperation()
- {
- context = new SP.ClientContext(appweburl);
- var factory = new SP.ProxyWebRequestExecutorFactory(appweburl);
- context.set_webRequestExecutorFactory(factory);
- appContextSite = new SP.AppContextSite(context, hostweburl);
- web = appContextSite.get_web();
- context.load(web);
- list = web.get_lists().getByTitle("AppraisalRequests");
- employeeList = web.get_lists().getByTitle("Employees");
- alert(list.count);
- alert(employeeList.count);
- var camlQuery = new SP.CamlQuery();
- camlQuery.set_viewXml(
- '<View><Query><Where><IsNotNull><FieldRef Name="ID"/></IsNotNull></Where></Query></View>'
- );
- collListItem = list.getItems(camlQuery);
- camlQuery = new SP.CamlQuery();
- console.log(user.get_title());
- camlQuery.set_viewXml(
- // "<Where><Eq><FieldRef Name='EmployeeName['$5K_1']' /><Value Type='User'>" + user.get_title() + "</Value></Eq></Where>"
- "<View><Query><Where><Eq><FieldRef Name='EmployeeName' /><Value Type='User'>"+user.get_title()+"</Value></Eq></Where></Query></View>"
- );
- //--------------------------------------------------------------------------------------
- //var r = list.GetItems(q);
- //--------------------------------------------------------------------------------------
- collListItem1 = employeeList.getItems(camlQuery);
- context.load(collListItem);
- context.load(collListItem1);
- context.executeQueryAsync(onSuccess, onFail);
- }
- function onSuccess()
- {
- alert("List loaded Successfully");
- var count = collListItem.get_count();
- alert(count);
- var listItemInfo = '';
- var listItemEnumerator = collListItem.getEnumerator();
- while (listItemEnumerator.moveNext())
- {
- // alert("Enter");
- var oListItem = listItemEnumerator.get_current();
- // alert(oListItem.get_id());
- listItemInfo += '\n Manager: ' + oListItem.get_item('Manager')['$5K_1']+
- '\n Employee: ' + oListItem.get_item('Employee')['$5K_1'];
- console.log(oListItem.get_item('Manager'));
- console.log(oListItem.get_item('Employee'));
- }
- alert(listItemInfo.toString());
- alert("Here");
- var count = collListItem1.get_count();
- alert(count);
- var listItemInfo = '';
- var listItemEnumerator = collListItem1.getEnumerator();
- while (listItemEnumerator.moveNext()) {
- // alert("Enter");
- var oListItem = listItemEnumerator.get_current();
- // alert(oListItem.get_id());
- listItemInfo += '\n Employee: ' + oListItem.get_item('EmployeeName')['$5K_1'];
- console.log(oListItem.get_item('EmployeeName'));
- console.log(oListItem.get_item('EmployeeType'));
- if (oListItem.get_item('EmployeeType') == "Manager")
- {
- isManager = true;
- }
- }
- alert(isManager);
- alert(listItemInfo.toString());
- getRequests();
- }
- function getRequests()
- {
- //Employee
- if (isManager == false)
- {
- requestList = web.get_lists().getByTitle("AppraisalRequests");
- }
- //list = web.get_lists().getByTitle("AppraisalRequests");
- //employeeList = web.get_lists().getByTitle("Employees");
- //alert(list.count);
- //alert(employeeList.count);
- //var camlQuery = new SP.CamlQuery();
- //camlQuery.set_viewXml(
- // '<View><Query><Where><IsNotNull><FieldRef Name="ID"/></IsNotNull></Where></Query></View>'
- //);
- //collListItem = list.getItems(camlQuery);
- //camlQuery = new SP.CamlQuery();
- //console.log(user.get_title());
- //camlQuery.set_viewXml(
- // // "<Where><Eq><FieldRef Name='EmployeeName['$5K_1']' /><Value Type='User'>" + user.get_title() + "</Value></Eq></Where>"
- // "<View><Query><Where><Eq><FieldRef Name='EmployeeName' /><Value Type='User'>" + user.get_title() + "</Value></Eq></Where></Query></View>"
- // );
- }
- // This function is executed if the above call fails
- function onFail(sender, args) {
- alert(args.get_message());
- }
- function getQueryStringParameter(paramToRetrieve) {
- var params =
- document.URL.split("?")[1].split("&");
- for (var i = 0; i < params.length; i = i + 1) {
- var singleParam = params[i].split("=");
- if (singleParam[0] == paramToRetrieve)
- return singleParam[1];
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement