[英]How to attach a attachment file to a list item using REST API in SharePoint 2013
I'd like to ask experts. 我想问专家。
Dose anyone know how to attach a attachment file to a list item using REST API in SharePoint 2013 ? 任何人都知道如何使用SharePoint 2013中的REST API将附件文件附加到列表项? I searched the bellow document.
我搜索了波纹管文件。 But there is no information about upload a file as list item's attachments.
但是没有关于将文件上传为列表项附件的信息。
http://msdn.microsoft.com/en-us/library/fp142386.aspx http://msdn.microsoft.com/en-us/library/fp142386.aspx
Additional info: 附加信息:
I found the bellow article. 我找到了吼叫文章。
http://chuvash.eu/2013/02/20/rest-api-add-a-plain-text-file-as-an-attachment-to-a-list-item/ http://chuvash.eu/2013/02/20/rest-api-add-a-plain-text-file-as-an-attachment-to-a-list-item/
According to the article, it's enable to upload a attachment file to a list item using bellow Javascript code. 根据该文章,它可以使用下面的Javascript代码将附件文件上传到列表项。 I'd like to use C#.
我想用C#。 I'm trying now, but I still didn't success.
我现在正在尝试,但我仍然没有成功。
var content = "Hello, this text is inside the file created with REST API";
var digest = $("#__REQUESTDIGEST").val();
var composedUrl = "/_api/web/lists/GetByTitle('List1')/items(1)/AttachmentFiles/add(FileName='readme.txt')";
$.ajax({
url: composedUrl,
type: "POST",
data: content,
headers: {
"X-RequestDigest": digest
}
})
There are a several approaches how to consume SharePoint REST API using .NET,some of them are listed below: 如何使用.NET来使用SharePoint REST API有几种方法,其中一些列在下面:
.NET Framework 4.5
) .NET Framework 4.5
) .NET Framework 1.1
) .NET Framework 1.1
) All of them allows to perform CRUD operations in SharePoint Online/SharePoint 2013 using REST interface. 所有这些都允许使用REST接口在SharePoint Online / SharePoint 2013中执行CRUD操作。
SPWebClient class demonstrates how to perform CRUD operations using WebClient . SPWebClient类演示了如何使用WebClient执行CRUD操作。
The following example demonstrates how to add attachment file to List in SharePoint Online: 以下示例演示如何将附件文件添加到SharePoint Online中的List:
var credentials = new SharePointOnlineCredentials(userName, securePassword);
AddAttachmentFile(webUrl, credentials, "Nokia Offices", 1, @"c:\upload\Nokia Head Office in Espoo.jpg");
public static void AddAttachmentFile(string webUrl,ICredentials credentials,string listTitle,int itemId,string filePath)
{
using (var client = new SPWebClient(new Uri(webUrl),credentials))
{
var fileContent = System.IO.File.ReadAllBytes(filePath);
var fileName = System.IO.Path.GetFileName(filePath);
var endpointUrl = string.Format("{0}/_api/web/lists/GetByTitle('{1}')/items({2})/AttachmentFiles/add(FileName='{3}')", webUrl,listTitle,itemId,fileName);
client.UploadFile(new Uri(endpointUrl), fileContent);
}
}
Dependencies: 依赖关系:
Try to use this: 试着用这个:
var executor = new SP.RequestExecutor(appweburl);
var digest = $("#__REQUESTDIGEST").val();
var content = "Hello, this text is inside the file created with REST API";
executor.executeAsync(
{
url: appweburl + "/_api/web/lists/getbytitle('List1')/items(1)/AttachmentFiles/add(FileName='readme.txt)",
method: "POST",
body: content,
headers: {
"X-RequestDigest": digest
},
success: function(data) {
toastr.success('Document attached successfully.');
},
error: function(err) {
toastr.error('Oops! Document attached created fail.');
}
}
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.