Home > Forum > Actions > Attachment file handle with SDK

Attachment file handle with SDK
0

I try to read/load in an xml-file in the Webcon application about the attachment section.
Then I build myself a choice picker, where I can chose the selected file.
After that I want to run an SDK action, where I get the data from my xml-file.
In my code I implemented something but the file path is static at the moment and compares the file names.
Normally every read in file gets an ID in the database but I don't know how to access this data.
Has anyone experience with this and know what the best way it is to implement this?

The code excerpt from me is in the attachment below.
Thank you very much in advance.

MVP

Hi Maximilian,

I'm not sure whether I understand your question correctly.
In your choice field you select the attachment you want to process and you are currently struggling a bit to get the correct attachment?
If this is the case I would do the following:
User the ID of the attachment as the ID of the choice field and select the attachment based on the choice field value.
var attachment = args.Context.CurrentDocument.Attachments.FirstOrDefault(x=>x.ID = 123)

If this is not the case you could look at the signing repositories. Here's a line where the attachments of the current document are processesd.
https://github.com/WEBCON-BPS/BPSExt-Signing-DocuSign/blob/5784cf2a66e946bf8a093c3f386b0b27cb0566d6/WebCon.BpsExt.Signing.DocuSign/CustomActions/Helpers/DataHelper.cs#L56

Best regards,
Daniel

MVP
In reply to: Maximilian H.

Hi Daniel,

thank you very much for your answer.
I uploaded my example in the attachment.
Yes I read in the file about the attachment, then I have choice field where I choose the file.
Then I trigger my action and give everything into the C#-code.
In the C#-code I want to work with the ID of the selected file.

Your code snippet:
var attachment = args.Context.CurrentDocument.Attachments.FirstOrDefault(x=>x.ID = 123)
was read only property with the ID.

I hope that this declaration can help you a little bit more.
Thank you very much and best regards
Maximilian

Hi Maximilian,

yes it was a simple snippet. A more elaborate one would be:
SQL Query of the choice field:
SELECT [ATT_ID] as Id, ATT_Name as Label
FROM [dbo].[WFDataAttachmets]
where ATT_WFDID = {WFD_ID}
and [ATT_IsDeleted] = 0
and [ATT_FileType] = '.xlsx'

Code:
var idOfchoiceField = GetFromConfiguration
var choiceFieldValueId = args.Context.CurrentDocument.ChooseFields.FirstOrDefault(x=>x.ID == idOfchoiceField ).Value.ID
var attachment = args.Context.CurrentDocument.Attachments.FirstOrDefault(x=>x.ID == choiceFieldValueId)

Of course, there could be an issue, when the attachment hasn't been saved before. You could add the attachment in step 1 and select the field value in step 2 after which follows you action. But this is only a guess.

Best regards,
Daniel

In reply to: Daniel Krüger (Cosmo Consult)

Hi Maximilian,

sorry, I didn't look for any syntax error when writing it here. I didn't test or even build it. Of course the error is correct, it should be a comparison so it should be == and not only =

Best regards,
Daniel

Hey Daniel,

thank you for your reply.
I got no time to test it yesterday but today I tried it with your code.
In C# I took an ConfigEditableDropDownList but when I want to configure the SDK in Webcon I always get a error message. The photo is in the attachment.
And with your way I think there must be a parse to an integer, is this right?

Thank you very much and have a good evening
Maximilian

MVP
In reply to: Maximilian H.

Good Morning Daniel,

thank you very much for your help.
I tried to rebuild it, but I get an error message in my code.
I set up an new ConfigEditableDropDownList in my code, but I don't know if this is the right thing for it.
Maybe you could look at this again.

Thank you very much and best regards
Maximilian

When I look your code you were almost there.
args.Context.CurrentDocument.GetFieldValue(Configuration.ChoiceFormFieldID) already returns an PickerField object. You can either cast the returned object to a PickerField or get the pickerfield value via the PickerFields-Property
args.Context.CurrentDocument.PickerFields.GetByID(Configuration.ChoiceFormField).ID
The ID should be your attachment id.

MVP
In reply to: Maximilian H.

Hey Daniel,

thank you for your reply.
I got no time to test it yesterday but today I tried it with your code.
In C# I took an ConfigEditableDropDownList but when I want to configure the SDK in Webcon I always get a error message. The photo is in the attachment.
And with your way I think there must be a parse to an integer, is this right?

Thank you very much and have a good evening
Maximilian

Hi Maximilian,

I'm almost ready to start my blog but I'm not quite there yet. So there may be changes. :)
Anyway, I created a Custom Action for reading an xml file and populating an item list with the data. You will find a link to the source code at the end.
https://daniels-notes.de/posts/2021/custom-action-xml-to-item-list

@Everyone else
If you are interested in one of the following topics read on:

Creating a plugin configuration with a grid
Reading an attachment
Populating an item list
Logging to admin view in history
Tips for Debugging and VS build events

Best regards,
Daniel

MVP
In reply to: Adam

Hi Daniel
Hi, Can you reupload that solution to GitHub?

https://daniels-notes.de/posts/2021/custom-action-xml-to-item-list

Hi Adam,

I just fixed the link in the blog post.

The repository is now located here:
https://github.com/Daniel-Krueger/webcon_xmlToItemList

Keep in mind that a few things changed from WEBCONB PS 2021 to 2023 and 2024. :)

Best regards,
Daniel