如何控制RequestExecutor的顺序

时间:2015-09-04 06:09:16

标签: javascript jquery json sharepoint-2013 office365

我使用SharePoint Office 365开发了一个Intranet站点。

在母版页文件中,有一个菜单栏(使用List存储URL和名称)。

我想控制按钮在“管理员”组用户中时可见的“管理员”按钮。

我确认Check check right功能可以正常工作。

但是在生成菜单栏的功能之后执行检查功能。 所以它无法获取变量来控制按钮显示/隐藏。

如何首先执行check admin right功能?感谢

以下是我的代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<%@ Master Language="C#" %>
<%@ Register tagprefix="SharePoint" namespace="Microsoft.SharePoint.WebControls" assembly="Microsoft.SharePoint, Version=16.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<html dir="ltr" xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">
<meta name="WebPartPageExpansion" content="full" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<SharePoint:RobotsMetaTag runat="server"></SharePoint:RobotsMetaTag>
<title>Intranet</title>
<link rel="stylesheet" type="text/css" href="../../SitePages/Site.css">

<script type="text/javascript" src="jquery-1.11.3.min.js"></script>
<script type="text/javascript">

    function GetQueryStringParams(sParam) 
    { 
       var sPageURL = window.location.search.substring(1); 
       var sURLVariables = sPageURL.split('&'); 
        for (var i = 0; i < sURLVariables.length; i++)  
        { 
            var sParameterName = sURLVariables[i].split('='); 

            if (sParameterName[0] == sParam)  

            {
                return sParameterName[1]; 
            }
        }
    }


    var isAdmin=false;
    var appweburl="https://XXX.sharepoint.com/XXX/";    

    $(document).ready(function () {
        var scriptbase = appweburl+ "/_layouts/15/";
        $.getScript(scriptbase + "SP.RequestExecutor.js", execCrossDomainRequest1);     
    });


    function execCrossDomainRequest1() {

        var executor1 = new SP.RequestExecutor(appweburl);
        executor1.executeAsync(
            {
              url: appweburl + "/_api/web/currentuser",
              method: "GET",
              headers: { "Accept": "application/json; odata=verbose" },
              success: successHandlerCurrentUser,
              error: errorHandler
            }
        );              

        var executor9 = new SP.RequestExecutor(appweburl);
        executor9.executeAsync(
            {                               
              url: appweburl + "/_api/web/lists/getbytitle('Site_TopMenuBar')/items?$orderby=Sort_x0020_Order",
              method: "GET",
              headers: { "Accept": "application/json; odata=verbose" },
              success: successHandler,
              error: errorHandler
            }
        );          

    }

    var currentId;
    function successHandlerCurrentUser(data) {
        var jsonObject = JSON.parse(data.body);
        currentId=jsonObject.d.Id;

        var executor6 = new SP.RequestExecutor(appweburl);
        executor6.executeAsync(
            {
              url: appweburl + "/_api/Web/SiteGroups/GetById(77)/Users",    
              method: "GET",
              headers: { "Accept": "application/json; odata=verbose" },
              success: successHandlerGroup,
              error: errorHandler
            }
        );

        $('#username').html(jsonObject.d.Title);
    }

    function successHandlerGroup(data) {

        var jsonObject = JSON.parse(data.body);
        var iMember=false;
        //console.log(currentId);

        for (x=0;x<jsonObject.d.results.length;x++){
        //console.log(jsonObject.d.results[x].Id);
            if (jsonObject.d.results[x].Id==currentId){
                iMember=true;
                break;
            }               
        }
            if (iMember==true){
                isAdmin=true;
                //console.log(isAdmin);
                }
                else
                {
                isAdmin=false;
                //console.log(isAdmin);
                }       
    }       

    function successHandler(data) {         
        var jsonObject = JSON.parse(data.body);
        var announcementsHTML = "";

        var results = jsonObject.d.results;
        $("#mainMenuMaster").find("tr").remove();
        row='<tr>';
        for (var i = 0; i < results.length; i++) {
            if (results[i].Disabled==false){    
                    if (results[i].Title=='Admin') {
                    //console.log('menu-' + isAdmin);       
                    //as it execute before admin right checking, so that isAdmin always false;
                        if (isAdmin==true){                 
                            row+='<td class="hvr-fade" style="height:25px" align="left"><a  style="color:white; text-decoration: none; margin: 10px;" title="'+results[i].Tooltip+'" href="'+results[i].URL+'">'+results[i].Title +'</a></td>';
                        }
                    }
                    else
                    {
                        row+='<td class="hvr-fade" style="height:25px" align="left"><a  style="color:white; text-decoration: none; margin: 10px;" title="'+results[i].Tooltip+'" href="'+results[i].URL+'">'+results[i].Title +'</a></td>';
                    }

            }
        }
        row+='</tr>';
        $('#mainMenuMaster> tbody:last').append(row);
    }

    function errorHandler(data, errorCode, errorMessage) {
        console.log("Could not complete cross-domain call: " + errorMessage);
    }


    function getUserWebPermissionREST() {
        //Permission for admin to show or hide the entries on memory board using ShowOnHomePage Field

        var perm = new SP.BasePermissions();
        perm.set(SP.PermissionKind.manageWeb);

        $.ajax({
            url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/doesuserhavepermissions(@v)?@v={'High':'" + perm.$4_1.toString() + "', 'Low':'" + perm.$5_1.toString() + "'}",
            type: "GET",
            headers: { "accept": "application/json;odata=verbose" },
            success: function (data) {
                var d = data.d.DoesUserHavePermissions;
                if (d === true) {

                    //Show Check Box if Full Control
                }

                else {
                    //hide Check Box

                }
            },

          error: function (err) {
                alert(JSON.stringify(err));
            }
        });
    }       

    function errorHandlerAddItem(data, errorCode, errorMessage) {
    }



    </script>
     <asp:ContentPlaceHolder id="head" runat="server"></asp:ContentPlaceHolder>     
</head>
<body style="background-image: url(image/background.png);background-repeat:repeat;font-family:Helvetica, Arial;font-size:10pt">
<form id="form1" runat="server">&nbsp;
<center>
<table style="width:1280px;text-align:center;border:none" cellpadding="0" cellspacing="0" border="0">
<tr>

<!--
<td><img src="../SitePages/image/Logo1280.jpg" style="border:none;width:1280px" /></td>
-->
<td style="font-family:Helvetica, sans-serif, Arial; color:#55AAFF; background-image:url(../SitePages/image/Logo1280_182.jpg);background-repeat:no-repeat;width: 1280px; height:182px;" align="right" valign="bottom">
<b>
Welcome &nbsp;
<span id="username" style="color:#f58320;"/>
</b>
<b>&nbsp;&nbsp;|&nbsp;&nbsp;</b>
<a href="https://login.microsoftonline.com/logout.srf" style="color:black; font-weight:bold">Logout</a>
</td>
<td>&nbsp;</td>
</tr>

<tr><td style="background-color:#100b67;font-weight:bold;font-size:11pt;font-family:Helvetica, sans-serif, Arial;">
<table id="mainMenuMaster" cellpadding="2" cellspacing="2" style="border:none">
                        <tbody></tbody>
                    </table>
</td>
</tr>
<tr><td style="background-color:white">
<asp:ContentPlaceHolder Id="PlaceHolderMain" runat="server">
</asp:ContentPlaceHolder>
</td>
</tr>
<tr><td style="color:white;background-color:#100b67;font-size:9pt;font-family:Helvetica, sans-serif, Arial;">
(c) Copyright 2015
</td>
</tr>
<tr style="height:80px"><td style="background-image: url(image/footerRepeat.png); background-repeat:repeat-x;" align="right"><span id="username"></span></td>
</tr>
</table>
</center>
</form>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

感谢Juneyoung。

我的解决方案如下:

document.addEventListener("DOMContentLoaded", function(event) {
    checkAdminRights();
});

function checkAdminRights() {
    var userId = _spPageContextInfo.userId;
    var groupId = 42;   
    var requestHeaders = { "accept" : "application/json; odata=verbose" };

    $.ajax({
        url : _spPageContextInfo.webAbsoluteUrl + "/_api/web/sitegroups(" + groupId + ")/users/getbyid(" + userId + ")",
        contentType : "application/json;odata=verbose",
        headers : requestHeaders,
        success : userOwner,
        error : userNotOwner
    });
    function userOwner(data, request){
        var userName = data.d.LoginName;
        isAdmin=true;
    }
    function userNotOwner(error) {
        isAdmin=false;
    }       

}