Add file to Notes via REST API

I'm trying to post a file via the REST API. I already create the note, I have the note ID and now I'm trying to upload a file to the note.

Documentation is not so clear to me.

When I cal the API like this:

Notes/0df989f0-7453-11ec-838a-065b9161e400/file/test.pdf how does the object need to be like? Is it a Note object? Where to add the file byte array?

  • Just have a look on my little php script which creates an outer note and then adds an attachment.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    <?php
    //////////////////////////////////////////////////////////
    // Create Note with Attachement Version 11.x ++
    //////////////////////////////////////////////////////////
    $name = "testnote";
    $filename = "testnote.pdf";
    $description = "uploaded file to sugar note";
    $node_id = "";
    $username = "jim";
    $password = "jim";
    $migrator = "1";
    $base_url = "http://localhost/sugarent1120/rest/v10";
    $platform = "integration";
    if(file_exists('auth.php')) include 'auth.php';
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    Harald Kuske
    Principal Solution Architect – Professional Services, EMEA
    hkuske@sugarcrm.com
    SugarCRM Deutschland GmbH

  • Thanks for the clear answer. It was hard to find from the documentation that I had to use the end point  "/Notes/"{id}"/file/filename". But now I have it. I transformed curl to c# and got it working finally, halelujah! After creating the note I use this method to add the attachment to the note:

    Fullscreen
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    public async Task<bool> AddFileToNote(string token, string id, string fileUrl)
    {
    try
    {
    using (var apiClient = new HttpClient())
    {
    apiClient.Timeout = new TimeSpan(0, 0, 10);
    using (var request = new HttpRequestMessage(new HttpMethod("POST"), $"{_baseUrl}/Notes/{id}/file/filename"))
    {
    request.Headers.TryAddWithoutValidation("OAuth-Token", token);
    request.Headers.TryAddWithoutValidation("Cache-Control", "no-cache");
    var multipartContent = new MultipartFormDataContent
    {
    { new ByteArrayContent(File.ReadAllBytes(fileUrl)), "filename", Path.GetFileName(fileUrl) }
    };
    request.Content = multipartContent;
    var response = await apiClient.SendAsync(request);
    string result = await response.Content.ReadAsStringAsync();
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX