SharePoint Project Server List 列表CURD操作使用rest api接口

//#region 界面交互代码
var issuesUtils = issuesUtils || {};
(function () {
    /**
     * 点击问题提处理方案按钮事件
     */
    issuesUtils.issuuesSchemeBtn = function () {
        var item_id = commonUtils.getUrlParamValue('ID');
        issuesList.getIssuesItem(item_id, function (data) {
            console.log(JSON.stringify(data));
            var issuesSchemeUrl = '/sites/pwa/Lists/List/AllItems.aspx?IssuesId=' + data.ID;
            //location.href = issuesSchemeUrl;
        });

        issuesList.deleteItem(item_id, function (data) {
            alert("删除成功");
        });
    };
})();
//#endregion

//#region 问题list
/**
 * issues list
 */
var issuesList = issuesList || {};
(function () {

    var projectSiteName = function () {
        return location.pathname.replace('/', '').split('/')[2];
    };

    /**
     * 获取列表项目
     * @param {*} itemId 
     * @param {*} fun 
     */
    issuesList.getIssuesItem = function (itemId, fun) {
        var server_url = '/sites/pwa/' + projectSiteName() + '/_api/Lists/IssuesList/items(' + itemId + ')';
        commonUtils.executeRequest({
            type: commonUtils.ExecuteRequestType.READ,
            url: server_url,
            callBack: function (data) {
                if (fun) {
                    fun(data);
                }
            }
        });
    };

    issuesList.addIssuesItem = function (callBack) {
        var issuesData = {
            __metadata: { type: 'SP.Data.IssuesListItem' },
            Title: '我的测试问题1212'
        };
        var server_url = '/sites/pwa/' + projectSiteName() + '/_api/Lists/IssuesList/items';
        commonUtils.executeRequest({
            type: commonUtils.ExecuteRequestType.CREATE,
            data: issuesData,
            url: server_url,
            callBack: function (data) {

            }
        });
    };

    issuesList.updateItem = function (callBack) {
        var server_url = '/sites/pwa/' + projectSiteName() + '/_api/Lists/IssuesList/items(6)';
        var issuesData = {
            __metadata: { type: 'SP.Data.IssuesListItem' },
            Title: '修改标题111111111'
        };
        commonUtils.executeRequest({
            type: commonUtils.ExecuteRequestType.UPDATE,
            data: issuesData,
            url: server_url,
            callBack: function (data) {

            }
        });
    };

    /**
     * 删除问题
     * @param {*ID} itemId 
     * @param {*} callBack 
     */
    issuesList.deleteItem = function (itemId, callBack) {
        var server_url = '/sites/pwa/' + projectSiteName() + '/_api/Lists/IssuesList/items(' + itemId + ')';
        commonUtils.executeRequest({
            type: commonUtils.ExecuteRequestType.DELETE,
            url: server_url,
            callBack: callBack
        });
    }
})();
//#endregion

//#region 问题处理方案list
var issuesSchemeList = issuesSchemeList || {};
(function () {

})();
//#endregion

//#region 常用工具类
/**
 * 常用工具类
 */
var commonUtils = commonUtils || {};
(function () {
    /**
     * 根据参数名称获取URL参数值
     * @param {参数名称} paramName 
     */
    commonUtils.getUrlParamValue = function (paramName) {
        var reg = new RegExp("(^|&)" + paramName + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return unescape(r[2]);
        return undefined;
    };

    /**
     * 
     * @param {type:commonUtils.ExecuteRequestType,data:提交的数据对象,url:rest api url 地址,callBack:回调函数function(data){}} option 
     */
    commonUtils.executeRequest = function (option) {
        var ajax_type = undefined;
        var ajax_header = undefined;
        var ajax_data = undefined;
        switch (option.type) {
            case commonUtils.ExecuteRequestType.CREATE:
                //添加数据
                ajax_type = 'POST';
                ajax_header = {
                    "Accept": "application/json; odata=verbose",
                    "X-RequestDigest": $("#__REQUESTDIGEST").val()
                };
                break;
            case commonUtils.ExecuteRequestType.UPDATE:
                //修改数据
                ajax_type = 'POST';
                ajax_header = {
                    "Accept": "application/json; odata=verbose",
                    "X-RequestDigest": $("#__REQUESTDIGEST").val(),
                    "IF-MATCH": "*",
                    "X-HTTP-Method": "MERGE"
                };
                break;
            case commonUtils.ExecuteRequestType.READ:
                //读取数据
                ajax_type = 'GET';
                ajax_header = {
                    "Accept": "application/json; odata=verbose",
                };
                break;
            case commonUtils.ExecuteRequestType.DELETE:
                //删除数据
                ajax_type = 'POST';
                ajax_header = {
                    //"Accept": "application/json; odata=verbose",
                    "X-RequestDigest": $("#__REQUESTDIGEST").val(),
                    "IF-MATCH": "*",
                    "X-HTTP-Method": "DELETE"
                };
                break;
        }
        if (option.data) {
            ajax_data = JSON.stringify(option.data);
        }
        if (ajax_header && ajax_header) {
            $.ajax({
                url: option.url,
                type: ajax_type,
                //contentType: "application/json;odata=verbose",
                data: ajax_data,
                headers: ajax_header,
                complete: function (xhr, textStatus) {
                    if (xhr.status == 200) {
                        var jsondata = xhr.responseText ? JSON.parse(xhr.responseText).d : {};
                        if (option.callBack) {
                            option.callBack(jsondata);
                        }
                    } else {
                        console.error("出现错误,错误代码为" + xhr.status);
                    }
                },
                error: function (data, errorCode, errorMessage) {
                    console.error("出现错误:" + errorMessage);
                }
            });
        } else {
            console.error("调用commonUtils.executeRequest方法参数不合法");
        }
    }

    /**
     * 调用rest api类型,增删改查
     */
    commonUtils.ExecuteRequestType = {
        CREATE: 1,
        UPDATE: 2,
        READ: 3,
        DELETE: 4
    };
})();
//#endregion

 

posted on 2018-05-30 07:32  larryle  阅读(669)  评论(0编辑  收藏  举报