How to download sharepoint file and send as attachments

Hello all,
I am using Sharepoint integration to save all our attachments on SharePoint. Now I want to download files from SharePoint server and send those files as an attachment using BPM. Any guidance or sample code will be highly appreciated.

Regards,
Ali

Here is a starting point for the file download - assuming you can use code widgets (MT?) - with a few recommendations. This same thing can be done client side. There are several examples on this site for emailing using code or you could use a widget. It is worth noting, and not sure if 10.2.600 changed this, but SpDownFile is not available in BO widget and most likely because of return type.

I would recommend using Functions if on 10.2.600 for this. This works the same in a function, just different code. You can also skip the portion of writing out a file and then referencing that path in the email if you don’t want to worry about file cleanup, etc. but that code is not included. This also does not have any error handling and you would want that.

/*
Add Reference to Ice.Contracts.BO.Attachment
Add using System.IO
*/

string FileName = string.Empty;
int FileReference = 0;

var AttachmentRows =
    from fa in Db.XFileAttch
    join fr in Db.XFileRef on new {fa.Company, fa.XFileRefNum} equals new {fr.Company, fr.XFileRefNum }
    where fa.Company == Session.CompanyID && fa.Key1 == "somepart" && fa.Key2 == "somepartrev"
    select new {fr.XFileDesc, fa.XFileRefNum};
foreach (var AttachmentRow in AttachmentRows)
{
    FileName = AttachmentRow.XFileDesc;
    FileReference = AttachmentRow.XFileRefNum;
}

var bo = Ice.Assemblies.ServiceRenderer.GetService<Ice.Contracts.AttachmentSvcContract>(Db);
if (bo == null)
{
    throw new Ice.Common.EpicorServerException("Can't resolve instance of 'Ice.Attachment' service.");
}

using (bo)
{
    string TempPath = @"\\someserver\Files"; // Could use GetTempFileName if dealing with multiple files
    string Output = Path.Combine(TempPath,FileName);
    Dictionary<string,string> metaData = new Dictionary<string,string>();
    byte[] FileData = bo.SpDownloadFile(FileReference, ref metaData);
    File.WriteAllBytes(Output, FileData);
}
3 Likes

@danbedwards Thanks, I’ll give it a try in an hour or so and get to back to you with update. Thanks again for your response.

@danbedwards thanks, after few changes it works exactly the way I was looking for. Thanks for your help.

@dearmoawiz , Just curious, did you get this working for more than one attachment??