Showing results for 
Search instead for 
Do you mean 
Community Home Request Access Read Blogs Share Your Ideas Search Community View My Settings
Copper Super Contributor
Posts: 16
Registered: ‎09-13-2012
Accepted Solution

Creating Attachment from E-Mail-Attachment via SData-Outlook-AddIn

[ Edited ]

Hi, I don't know where exactly to post this question, as it's going to be an Outlook-AddIn working with SData.



Following situation:


we have a custom entity called license with an unique custom id ('TargetId'). The entity attachment has been extended with the property 'LicenseId'. Because we have to order these licenses externally we get an email every once in a while with the license files attached. The attached files names are including this 'TargetId' so it's not a problem to assign them to right license (entity).


The problem is to create a new attachment via SData (done) and link it to the custom entity since the payload of the SData Attachment System Endpoint does not include any custom properties.



This is how far I got until now: (note that this is just the code for testing)


// Creating an AtomEntry with a custom payload to provide some additional data
AtomEntry entAttachment = new AtomEntry(); SDataPayload plAttachment = new SDataPayload() { ResourceName = "Attachment", Namespace = "", Values = { { "LicenseId", "Q6UJ9A000O9M" }, { "attachDate", SyndicationDateTimeUtility.ParseRfc3339DateTime("2013-04-11T11:27:00+00:00") }, { "description", "VX1-Lizenz" }, { "dataType", "R" } } }; entAttachment.SetSDataPayload(plAttachment); try {
// This is basically the code from the Sage SalesLogix Endpoint Whitepaper 7.5.4 on PDF page 10 string filePath = @"c:\_Daten\Sage\TestDaten\12345678_DY3QC5F_0000_1.lic"; string contentType = MimeHelper.FindMimeType(filePath); string fileName = Path.GetFileName(filePath); FileStream stream = File.OpenRead(filePath); AttachedFile file = new AttachedFile(contentType, fileName, stream); RequestOperation operation = new RequestOperation(HttpMethod.Post, entAttachment) { Files = { file } }; SDataRequest request = new SDataRequest("", operation) { UserName = username, Password = password };
// This creates an attachment and uploads the file SDataResponse response = request.GetResponse();
// The following code is just used to verify the process AtomEntry responseEntry = (AtomEntry)response.Content; SDataPayload responsePayload; string text = string.Empty; responsePayload = responseEntry.GetSDataPayload(); foreach (KeyValuePair<string, object> dicEntry in responsePayload.Values) { text += string.Format("'{0}' : ", dicEntry.Key); if (dicEntry.Value != null) text += string.Format("'{0}'", dicEntry.Value); text += "\n"; } MessageBox.Show(text); } catch (Exception ex) { MessageBox.Show(ex.Message); }


The output of the MessageBox showing the variable 'text' is the following:


'attachDate' : '2013-04-11T11:27:00+00:00'
'dataType' : 'R'
'defectId' :
'description' : 'VX1-Lizenz'
'documentType' :
'fileName' : 12345678_DY3QC5F_0000_1.lic'
'physicalFileName' : '!Q6UJ9A0000GZ12345678_DY3QC5F_0000_1.lic'
'fileSize' : '760'
'accountId' :
'contactId' :
'contractId' :
'historyId' :
'leadId' :
'opportunityId' :
'productId' :
'returnId' :
'ticketId' :
'activityId' :
'salesOrderId' :
'createDate' : '2013-04-11T13:37:47+00:00'
'createUser' : 'ADMIN       '
'modifyDate' : '2013-04-11T13:37:47+00:00'
'modifyUser' : 'ADMIN       '

As you can see, the custom property 'LicenseId' (along with some other custom properties) is missing. Also the 'AttachmentId' is not provided (why btw?)


I am aware of the possibility to extract the AttachmentId from the physicalFileName and then - using the Dynamic Endpoint - editing the attachment afterwards, but is there a possibility to do this all in one step?

I thought it may be possible to add custom properties to the Attachments System Endpoint. Although the sentence 'System endpoints are generated from a fixed API.' from the whitepaper diminishes my hope... Can anyone help me out?



Edit: We're using SLX (including HotFixes 1-16 and WebHotFixes 1-6)

Copper Super Contributor
Posts: 16
Registered: ‎09-13-2012

Re: Creating Attachment from E-Mail-Attachment via SData-Outlook-AddIn

After looking into the sourcecode of the Desktop Manager I found out how to get the key of the entity payload. I then used this key to get the corresponding attachment payload via the dynamic endpoint, write the needed data and then update it. It works fine for me, even with 20 or more attachments to create at a time.
Copper Contributor
Posts: 69
Registered: ‎05-07-2012

Re: Creating Attachment from E-Mail-Attachment via SData-Outlook-AddIn

can you please post your code? thx

Copper Super Contributor
Posts: 16
Registered: ‎09-13-2012

Re: Creating Attachment from E-Mail-Attachment via SData-Outlook-AddIn

Hi, took a while responding to your request, sry.


Here is the some code I'm using:


// OAttachment is a class from Microsoft.Office.Interop.Outlook
// This code has been taken from the source code of the SLX Desktop Manager version 7.5.4 and edited according to our needs
// 'this' is the class VX1ImportSDataClient which inherits the class SDataClient from the SLX Desktop Manager 7.5.4
public string CreateAttachment(OAttachment attachment) { AtomEntry creationEntry = new AtomEntry(); creationEntry.SetSDataPayload(new SDataPayload() { ResourceName = "Attachment", Namespace = "", Values = { { "attachDate", DateTime.UtcNow.ToUniversalTime().ToString("o") }, { "description", attachment.DisplayName }, { "dataType", "R" } } }); string contentType = MimeHelper.FindMimeType(attachment.FilePath); string fileName = this.ValidateFileName(attachment.DisplayName); FileStream fileStream = File.OpenRead(attachment.FilePath); AttachedFile attachedFile = new AttachedFile(contentType, fileName, fileStream); RequestOperation operation = new RequestOperation(HttpMethod.Post, creationEntry) { Files = { attachedFile } }; string uri = (new SDataUri(this.Server).AppendPath("sdata", "slx", "system", "-", "attachments")).ToString(); SDataRequest creationRequest = new SDataRequest(uri, operation) { UserName = this.UseWindowsAuth ? null : this.UserName, Password = this.UseWindowsAuth ? null : this.Password }; SDataPayload responsePayload = ((AtomEntry)creationRequest.GetResponse().Content).GetSDataPayload(); return responsePayload.Key; }


public void UpdateAttachment(string attachmentId, string licenseId, string salesorderId, string accountId, string contactId)
    SDataSingleResourceRequest updateRequest = new SDataSingleResourceRequest(NewSDataService());
    updateRequest.ResourceKind = "Attachments";
    updateRequest.ResourceSelector = string.Format("'{0}'", attachmentId);

    AtomEntry updateEntry = updateRequest.Read();
    SDataPayload updatePayload = updateEntry.GetSDataPayload();
    if (!string.IsNullOrEmpty(licenseId)) updatePayload.Values["LicenseId"] = licenseId;
    if (!string.IsNullOrEmpty(salesorderId)) updatePayload.Values["SalesorderId"] = salesorderId;
    if (!string.IsNullOrEmpty(accountId)) updatePayload.Values["AccountId"] = accountId;
    if (!string.IsNullOrEmpty(contactId)) updatePayload.Values["ContactId"] = contactId;

    updateRequest.Entry = updateEntry;