我正在尝试使用他们的REST API从Sharepoint列表构建一些半动态的员工目录页面。

我知道有足够的Javascript惹我麻烦,所以我感到我的解决方案不够理想,可能也不理想。

目的是允许我们的Web编辑者通过简单地添加简短脚本和员工的电子邮件地址来为各个部门创建唯一的页面。我考虑过的另一个选择是,仅在每个程序的列表中添加一个字段,但是整个字段的一致性不够,因此能够设置列表并在每个页面上排序可能是最好的选择。

该解决方案可以正常工作,但有时排序顺序变得异常混乱,我猜是因为脚本读取列表的顺序。

这是前端代码:

<script type="text/javascript"
src="getstaffdirectory2.js"></script>

<script>
$(document).ready(function() {
GetStaffDirectory('[email protected]');
GetStaffDirectory('[email protected]');
GetStaffDirectory('[email protected]');
GetStaffDirectory('[email protected]');
GetStaffDirectory('[email protected]');
});
</script>

<div class="white-space-2"></div>
<div class="row">
<div id="GetStaffDirectory"></div>
</div>


不太优雅? :-)

这是脚本:

function GetStaffDirectory(UserEmail) {
var res;
var selectStr = '$select=';
var expandStr = '$expand=';
var filterStr = '$filter=(EMAIL eq \''+UserEmail+'\')';

var requestURL = '/_api/web/lists/GetByTitle(\'Staff
Directory\')/items?'+filterStr; // + '?' + selectStr + '&' + expandStr + '&'
+ filterStr;
console.log(requestURL);
$.ajax({
    url: requestURL,
    headers: {
        Accept: 'application/json;odata=verbose'
    },
    method: 'GET',
    //Success Function
    success: function (data) {
        res = data.d.results;
        console.log(res);
        // College Name Function
        switch(res[0].LOC){
            case "BHC" : {
                            loc1="Brookhaven College";
                            break;
                         }
            case "CVC" : {
                            loc1="Cedar Valley College";
                            break;
                         }
            case "EFC" : {
                            loc1="Eastfield College";
                            break;
                         }
            case "ECC" : {
                             loc1="El Centro College";
                            break;
                         }
            case "MVC" : {
                            loc1="Mountain View College";
                            break;
                         }
            case "NLC" : {
                            loc1="North Lake College";
                            break;
                         }
            case "RLC" : {
                            loc1="Richland College";
                            break;
                         }
            case "DCO" : {
                            loc1="Lecroy Center/ Dallas Colleges Online";
                            break;
                         }
            case "DSC" : {
                            loc1="District Service Center";
                            break;
                         }
            case "DO" :  {
                            loc1="District Office";
                            break;
                         }
            default : {
                loc1="";
                break;
            }
        }
        if((res[0].Office_x0020_Number == null)||(res[0].Office_x0020_Number
== undefined)){
             officeNum = "";
         }
        else {
         officeNum = '<li class="contact-office">' +
res[0].Office_x0020_Number + '</li>\n'
         }
         if((res[0].biolink == null)||(res[0].biolink == undefined)){
              biolinkURL = "";
          }
         else {
          biolinkURL = '<li class="contact-bio"><a href="' + res[0].biolink + '">Online Bio<span class="sr-only"> for ' + res[0].Title + '</span></a></li>\n'
          }
          if((res[0].staff_x002d_photo == null)||(res[0].staff_x002d_photo
== undefined)){
               staffphoto = '/logo-' +
res[0].LOC + '-square.svg'
           }
           else {
            staffphoto = res[0].staff_x002d_photo;
          }

        var contactString =
        '<div class="col-sm-4">\n' +
        '<div class="contact-box height-sm-400 ' + res[0].LOC + '">\n' +
        '<img class="contact-photo" src="' + staffphoto + '">\n' +
        '<div class="contact-name">' + res[0].FIRST + ' ' + res[0].Title +
'</div>\n' +
        '<div class="contact-title">' + res[0].Title1 + '</div>\n' +
        '<div class="contact-dept">' + res[0].department + '</div>\n' +
        '<ul class="contact-info">\n' +
        '<li class="contact-location">' + loc1 + '</li>\n' +
        officeNum +
        '<li class="contact-email"><a href="mailto:' + res[0].EMAIL + '">' +
res[0].EMAIL + '</a></li>\n' +
        '<li class="contact-phone">' + res[0].PHONE + '</li>\n' +
        biolinkURL +
        '</ul>\n</div>\n</div>\n</div>\n';
        $("#GetStaffDirectory").append(contactString);
    },
    //Error Function
    error: function (jQxhr, errorCode, errorThrown) {
        res = jQxhr;
        console.log(res);
        $("#AjaxLoader0").hide();
        $(".AjaxMessage").html(errorThrown);
    },
    dataType: 'json' //Make me a JSON
});
};


有没有更简单的方法来确保脚本按页面上列出的顺序提取数据?我猜想有一个数组的方法,但我不熟悉它们的工作方式。

最佳答案

最好在一个请求中做所有您需要的事情,以提高性能。更改GetStaffDirectory()以使用字符串数组和类似的过滤器;

var filterStr = '$filter=((EMAIL eq userEmails[0]) or (EMAIL eq userEmails[1]))'


而且,您现在可以添加排序逻辑;

$orderby=EMAIL desc

关于javascript - 从SharePoint Rest API排序结果,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53897896/

10-17 00:34