Uploading multiple attachment to Quickbooks Online

18 Views Asked by At

I'm trying to upload multiple attachments in a single request in my .net application. I'm using their sdk to call their service. All I see is a upload which take is one attachable and a strem. Not sure how to pass in multiple.


public void AttachableUpload(BillDto billDto, objectNameEnumType entityRefType, string entityValue, Func<string, BasePathTypes, Stream> openFile)
{
    DataService svc = CreateDataService();
    long fileSize = 0;
    Batch batch = svc.CreateNewBatch();
    
    foreach (var documentInstance in billDto.DocumentInstances)
    {
        Attachable attachable = new Attachable();
        attachable.AttachableRef = GetAttachmentsForUpload(entityRefType, entityValue);
        string filePath = FileUtility.GetFileSavePathWithFile(
            documentInstance.PhysicalFileLocation,
            documentInstance.BuilderId.Value,
            documentInstance.PhysicalFileName,
            documentInstance.JobsiteId.ToNullableInt()
        );
        using (Stream stream = openFile(filePath, documentInstance.FileBasePathType))
        {
            fileSize += stream.Length;
            // Check if the file is a valid extension and the file size is less than 20 mb
            if (!IsQBOValidExtension(documentInstance.GetExtension()) || stream.Length / (1024 * 1024) >= 20)
            {
                continue;
            }

            batch.Add(attachable, "1234", OperationEnum.create);
            batch.Execute();

            attachable.ContentType = FileUtility.GetMimeTypeForFileExtension(filePath);
            attachable.FileName = documentInstance.Title.Value;
            Attachable attachableUploaded = Upload(svc, attachable, stream);
            // ToDo: Insert into Doc extension table
        }
    }
}

private bool IsQBOValidExtension(string fileExtension)
{
    return new string[] { "ai", "csv", "doc", "docx", "eps", "gif", "jpeg", "jpg", "ods", "pdf", "png", "rft", "tif", "txt", "xls", "xlsx", "xml" }.Contains(fileExtension);
}

public static Attachable Upload(DataService svc, Attachable attachable, System.IO.Stream stream)
{
    try
    {
        Attachable uploaded = svc.Upload(attachable, stream);
        return uploaded;
    }
    catch (IdsException ex)
    {
        LogUtility.WriteLog(LogTypes.QuickbooksErrorLog, new
        {
            error = "Error uploading attachment to QBO",
            message = ex.Message,
            exception = ex
        });
        return null;
    }
} 

Tried with Batch but not sure how to pass in list of streams

0

There are 0 best solutions below