如何获取上传到sharepoint上的文件的信息,以及expand的使用

背景:在使用sharepoint管理web应用时,往往会用到sp的文档库,和相关的restfull API用于文档的存取

问题:如何使用restAPI上传文件到sharepoint,并上传之后获取文件的上传时间,作者等信息?

解决方案:

1.上传:

{

var url = _spPageContextInfo.siteServerRelativeUrl
                  + "/_api/web/getfolderbyserverrelativeurl('" + libName + folderPath + "')/files" 
                  + "/add(overwrite=true,url='" + fileName + "')";

//_spPageContextInfo.siteServerRelativeUrl:

//libName :library name; eg:DocLib;

//folderPath ;eg:OPP-123456789/version1/Zac/;

//fileName:the name of file that you selected;

 var dfd = $.Deferred();
        var reader = new FileReader();
        reader.onloadend = function (evt) {
            if (evt.target.readyState == FileReader.DONE) {
                var buffer = evt.target.result;
                var completeUrl = _spPageContextInfo.siteServerRelativeUrl
                  + "/_api/web/getfolderbyserverrelativeurl('" + libname + foldername + "')/files"
                  + "/add(overwrite=true,url='" + filename + "')";

                $.ajax({
                    url: url  ,
                    type: "POST",
                    data: buffer,
                    processData: false,
                    headers: {
                        "accept": "application/json;odata=verbose",
                        "X-RequestDigest": $("#__REQUESTDIGEST").val(),
                    },
                    success: function (data) {
                        dfd.resolve(data);
                    }
                });
            }
        };
        reader.readAsArrayBuffer(file);

}

2.获取文件信息

sharepoint为了减少冗余,将Author等user对象通过Id和文件信息关联起来,所以在使用appUrl + "/_api/web/getfilebyserverrelativeurl('" + fileRelativeUrl + "') 这样的api去火取文件信息时,取到的Author或ModifiedBy均是相应user在该站点的id;这个时候,一种解决办法是获取到id再去站点的userList取到用户信息(EMail,Name等),另一个更好的解决办法,就是使用restAPI的expand和select关键字去将关联user对象进行扩展查询,这个时候如果在多个关联的list之间的查询时,就会极大的减少api的调用次数。

 

function getFileInfo(fileRelativeUrl) {

//fileRelativeUrl:"/SiteName/DocLib/OPP-0000000000/2/demo.PNG"
        var dfd = $.Deferred(),
            appUrl = app.config.ENV.siteAbsoluteUrl,
            fileInfo = {
                modifiedBy: "",
                lastModified: ""
            },
            fileInfoUri = appUrl + "/_api/web/getfilebyserverrelativeurl('" + fileRelativeUrl + "')/?$expand=ListItemAllFields/Modified,ModifiedBy/Title&$select=ListItemAllFields/Modified,ModifiedBy/Title";
        $.ajax({
            url: fileInfoUri,
            method: "GET",
            dataType: "JSON",
            headers: {
                "accept": "application/JSON;odata=verbose"
            },
            success: function (data) {
                fileInfo.modifiedBy = data.d.ModifiedBy.Title;
                fileInfo.lastModified = new Date(data.d.ListItemAllFields.Modified).format("dd MMM yyyy");
                dfd.resolve(fileInfo);
            },
            error: function (data) {
                dfd.resolve(fileInfo);
            },
            failed: function (data) {
                dfd.resolve(fileInfo);
            },
            async: true
        });  
        return dfd.promise();
    }

扩展:以下是一个多个List关联的sp query request:

_spPageContextInfo.siteServerRelativeUrl/_api/web/Lists/getbytitle('Maintenance')/items/?$expand=Role/Name,CalculatedRegion/Region,

CalculatedRegion/SubRegion1,CalculatedRegion/SubRegion2,CalculatedRegion/SubRegion3,

CalculatedIndustry/Industry_Vertical,CalculatedIndustry/Industry_Segment,

PrimaryContact/EMail,PrimaryContact/Title,PrimaryContact/SipAddress,PrimaryContact/Name

&$select=Role/Name,

CalculatedRegion/Region,

CalculatedRegion/SubRegion1,CalculatedRegion/SubRegion2,CalculatedRegion/SubRegion3,

CalculatedIndustry/Industry_Vertical,CalculatedIndustry/Industry_Segment,

PrimaryContact/EMail,PrimaryContact/Title,PrimaryContact/SipAddress,PrimaryContact/Name

&$filter=Role/Name eq 'tntLead' and GBU eq 'Global' and CalculatedRegion/Region eq 'AMS' 

and CalculatedRegion/SubRegion1 eq 'All' and CalculatedRegion/SubRegion2 eq 'All' and CalculatedRegion/SubRegion3 eq 'All' 

and CalculatedIndustry/Industry_Vertical eq '_' and CalculatedIndustry/Industry_Segment eq '_'

posted @ 2016-08-03 15:12  王仲春  阅读(1628)  评论(0编辑  收藏  举报