C# 如何在SharePoint 2013中使用REST API将附件文件附加到列表项

C# 如何在SharePoint 2013中使用REST API将附件文件附加到列表项,c#,rest,sharepoint,sharepoint-2013,C#,Rest,Sharepoint,Sharepoint 2013,我想请教专家 有人知道如何在SharePoint 2013中使用REST API将附件文件附加到列表项吗? 我搜索了下面的文件。但没有关于将文件作为列表项附件上载的信息 其他信息: 我找到了下面的文章 根据本文,可以使用下面的Javascript代码将附件文件上传到列表项。我想用C。我现在正在努力,但还是没有成功 var content = "Hello, this text is inside the file created with REST API"; var digest = $(

我想请教专家

有人知道如何在SharePoint 2013中使用REST API将附件文件附加到列表项吗? 我搜索了下面的文件。但没有关于将文件作为列表项附件上载的信息

其他信息:

我找到了下面的文章

根据本文,可以使用下面的Javascript代码将附件文件上传到列表项。我想用C。我现在正在努力,但还是没有成功

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
    }
})
尝试使用以下方法:

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.');
         }
}
);

有几种方法可以使用.NET使用SharePoint REST API,下面列出了其中一些方法:

  • -提供一个基类,用于从URI标识的资源发送HTTP请求和接收HTTP响应。(
    .NET Framework 4.5
  • -提供向URI标识的资源发送数据和从该资源接收数据的常用方法。(
    .NET Framework 1.1
  • -提供WebRequest类的HTTP特定实现,比以前的实现更低级
所有这些都允许使用REST界面在SharePoint Online/SharePoint 2013中执行CRUD操作

演示如何使用执行CRUD操作

如何通过SharePoint REST API添加附件文件 以下示例演示如何将附件文件添加到SharePoint Online中的列表:

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);
        }
}
依赖项:

  • 使用表示的包装器
  • 依赖性

如果您将此问题转移到sharepoint.stackexchange.com,您可能会有更好的机会得到一个好答案。这仍然是一个javascript实现。@Vadim的答案包括一个C。