博客地址:http://blog.csdn.net/FoxDave
本篇讲述如何通过REST操作文件夹和文件。
使用REST操作文件夹
在你知道某个文档库中的文件夹的URL时,可以使用如下的代码获取它。
url: http://site url/_api/web/GetFolderByServerRelativeUrl('/Shared Documents') method: GET headers: Authorization: "Bearer " + accessToken accept: "application/json;odata=verbose" or "application/atom+xml"下面的XML是一个请求返回信息的示例。
<content type="application/xml"> <m:properties> <d:ItemCount m:type="Edm.Int32">0</d:ItemCount> <d:Name>Shared Documents</d:Name> <d:ServerRelativeUrl>/Shared Documents</d:ServerRelativeUrl> <d:WelcomePage/> </m:properties> </content>如果想要创建文件夹,使用下面的代码。
url: http://site url/_api/web/folders method: POST body: { '__metadata': { 'type': 'SP.Folder' }, 'ServerRelativeUrl': '/document library relative url/folder name'} Headers: Authorization: "Bearer " + accessToken X-RequestDigest: form digest value accept: "application/json;odata=verbose" content-type: "application/json;odata=verbose" content-length:length of post body更新文件夹。
url: http://site url/_api/web/GetFolderByServerRelativeUrl('/Folder Name') method: POST body: { '__metadata': { 'type': 'SP.Folder' }, 'Name': 'New name' } Headers: Authorization: "Bearer " + accessToken X-RequestDigest: form digest value "IF-MATCH": etag or "*" "X-HTTP-Method":"MERGE", accept: "application/json;odata=verbose" content-type: "application/json;odata=verbose" content-length:length of post body最后当然是删除文件夹。
url: http://site url/_api/web/GetFolderByServerRelativeUrl('/Folder Name') method: POST Headers: Authorization: "Bearer " + accessToken X-RequestDigest: form digest value "IF-MATCH": etag or "*" "X-HTTP-Method":"DELETE"使用REST操作文件
获取一个文件夹下所有文件的代码如下。
url: http://site url/_api/web/GetFolderByServerRelativeUrl('/Folder Name')/Files method: GET headers: Authorization: "Bearer " + accessToken accept: "application/json;odata=verbose" or "application/atom+xml"通过文件名获取指定文件的代码如下。
url: http://site url/_api/web/GetFolderByServerRelativeUrl('/Folder Name')/Files('file name')/$value method: GET headers: Authorization: "Bearer " + accessToken或者通过URL。
url: http://site url/_api/web/GetFileByServerRelativeUrl('/Folder Name/file name')/$value method: GET headers: Authorization: "Bearer " + accessToken下面的示例演示了如何将一个文件添加到指定的文件夹。
url: http://site url/_api/web/GetFolderByServerRelativeUrl('/Folder Name')/Files/add(url='a.txt',overwrite=true) method: POST body: "Contents of file" Headers: Authorization: "Bearer " + accessToken X-RequestDigest: form digest value content-length:length of post body下面展示了如何使用PUT方法(注意对于文件只能用PUT)来更新一个文件。
url: http://site url/_api/web/GetFileByServerRelativeUrl('/Folder Name/file name')/$value method: POST body: "Contents of file." Headers: Authorization: "Bearer " + accessToken X-RequestDigest: form digest value X-HTTP-Method:"PUT" content-length:length of post body如果你想要更新一个文件的元数据metadata,你需要将文件当做列表项去对待,参考前一讲提到的内容,构建一个如https://<site url>/_api/web/lists/getbytitle('Documents')/items(<item id>)这样的URL去发送请求。
如果想要签出一个文件,使用下面的请求。
url: http://site url/_api/web/GetFileByServerRelativeUrl('/Folder Name/file name')/CheckOut(), method: POST headers: Authorization: "Bearer " + accessToken X-RequestDigest: form digest value对应的签入操作为。
url: http://site url/_api/web/GetFileByServerRelativeUrl('/Folder Name/file name')/CheckIn(comment='Comment',checkintype=0) method: POST headers: Authorization: "Bearer " + accessToken X-RequestDigest: form digest value如果想要删除一个文件,使用下面的请求。
url: http://site url/_api/web/GetFileByServerRelativeUrl('/Folder Name/file name') method: POST headers: Authorization: "Bearer " + accessToken X-RequestDigest: form digest value IF-MATCH: etag or "*" X-HTTP-Method:"DELETE"通过REST操作大文件
通过REST可以操作的最大文件大小为2GB,示例如下。
url: http://site url/_api/web/GetFolderByServerRelativeUrl('/Folder Name')/Files/Add(url='file name', overwrite=true) method: POST body: contents of binary file headers: Authorization: "Bearer " + accessToken X-RequestDigest: form digest value content-type: "application/json;odata=verbose" content-length:length of post body下面的代码展示了如何应用上面的请求结合跨域库来实现文件的创建。
function uploadFileBinary() { XDomainTestHelper.clearLog(); var ro; if (document.getElementById("TxtViaUrl").value.length > 0) { ro = new SP.RequestExecutor(document.getElementById("TxtWebUrl").value, document.getElementById("TxtViaUrl").value); } else { ro = new SP.RequestExecutor(document.getElementById("TxtWebUrl").value); } var body = ""; for (var i = 0; i < 1000; i++) { var ch = i % 256; body = body + String.fromCharCode(ch); } var info = { url: "_api/web/lists/getByTitle('Shared Documents')/RootFolder/Files/Add(url='a.dat', overwrite=true)", method: "POST", binaryStringRequestBody: true, body: body, success: success, error: fail, state: "Update" }; ro.executeAsync(info); }通过REST操作列表项的附件
下面的请求展示了如何获取指定列表项所有的附件文件。
url: http://site url/_api/web/lists/getbytitle('list title')/items(item id)/AttachmentFiles/ method: GET headers: Authorization: "Bearer " + accessToken accept: "application/json;odata=verbose" or "application/atom+xml"也可以通过文件名获取指定的附件。
url: http://site url/_api/web/lists/getbytitle('list title')/items(item id)/AttachmentFiles('file name')/$value method: GET headers: Authorization: "Bearer " + accessToken accept: "application/json;odata=verbose" or "application/atom+xml"如果想向列表项添加附件,使用下面的代码。
url: http://site url/_api/web/lists/getbytitle('list title')/items(item id)/AttachmentFiles/ add(FileName='file name') method: POST headers: Authorization: "Bearer " + accessToken body: "Contents of file." X-RequestDigest: form digest value content-length:length of post body如果想更新列表项中的附件,使用下面的代码。
url: http://site url/_api/web/lists/getbytitle('list title')/items(item id)/AttachmentFiles('file name')/$value method: POST body: "Contents of file." headers: Authorization: "Bearer " + accessToken "X-HTTP-Method":"PUT" X-RequestDigest: form digest value content-length:length of post body本文就介绍到这里。