C# Example: Upload Attachments

The following example shows how to upload attachments using C#.
Important: Run Swagger Code Generation before attempting to modify the code so that you do not receive reference errors. Refer to Using Swagger Code Generation for the procedure.
using System.IO;
using IO.Swagger.Api;

namespace Trebuchet.WebApi.IntegrationTests.ExamplesForCustomers.Attachments
{
    public class UploadAttachment
    {
        public void UploadAnAttachmentByBusinessObjectIdAndRecordId()
        {
            //Get an access token using CSM credentials
            var serviceApi = new ServiceApi("http://your server/CherwellApi/");
            var tokenResponse = serviceApi.ServiceToken("password",
                "your client id", null, "CSDAdmin", "CSDAdmin", null,
                "Internal");

            //Create a new Business Object api object and add the default header
            var businessObjectApi = new BusinessObjectApi("http://your server/CherwellApi/");
            businessObjectApi.Configuration.AddDefaultHeader("Authorization",
                "Bearer " + tokenResponse.AccessToken);

            //Get the Business Object summary for Incident
            var businessObjectSummaryIncident =
                businessObjectApi.BusinessObjectGetBusinessObjectSummaryByNameV1("Incident");

            var allFileBytes =
                File.ReadAllBytes(
                    "path to file");
            var totalSize = allFileBytes.Length;

            businessObjectApi.BusinessObjectUploadBusinessObjectAttachmentByIdAndRecIdV1(
                allFileBytes,
                "TextFile", businessObjectSummaryIncident[0].BusObId,
                "941c01f21ee2de2f65ace74428a5f5955a3e621314", 0, totalSize, string.Empty,
                string.Empty);
        }
    }
}
© Copyright 2018 Cherwell Software, LLC. All rights reserved.