3
votes

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 ? 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

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/

According to the article, it's enable to upload a attachment file to a list item using bellow Javascript code. I'd like to use 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
    }
})
2
Maybe, it is a better chance that you'll get a good answer if you move this question to sharepoint.stackexchange.comAnatoly Mironov

2 Answers

2
votes

There are a several approaches how to consume SharePoint REST API using .NET,some of them are listed below:

  • HttpClient - Provides a base class for sending HTTP requests and receiving HTTP responses from a resource identified by a URI. (.NET Framework 4.5)
  • WebClient - provides common methods for sending data to and receiving data from a resource identified by a URI. (.NET Framework 1.1)
  • HttpWebRequest - provides an HTTP-specific implementation of the WebRequest class, more low-level then the previous ones

All of them allows to perform CRUD operations in SharePoint Online/SharePoint 2013 using REST interface.

SPWebClient class demonstrates how to perform CRUD operations using WebClient.

How to add attachment file via SharePoint REST API

The following example demonstrates how to add attachment file to List in 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);
        }
}

Dependencies:

0
votes

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