Home > Forum > Actions > Add an attachment within PowerShell action

Add an attachment within PowerShell action
0

Hello,

I’m looking for a way to add an attachment within a PowerShell-action. I’m having an action “Run a PowerShell script” where I’d like to add an attachment to the currently running instance with the help of a WebCon REST API call. The problem is that while processing actions WebCon locks down the instance and I couldn’t find a way to make any changes to the process.
I use the parameter “forceCheckout=1” – that works fine as long a user is editing the form but fails when called from inside a PowerShell action.

I see two options there:
1) I could do it with an asynchronous PowerShell process and upload the attachment when WebCon has finished processing actions, but this is very error prone and error handling is not possible within the action.
2) Create a static “technical” instance and upload the document there. After that I can move the document to the current instance with the help of an “add attachment” action.

Is there a better solution for adding an attachment within a PowerShell-action?

What I really want to do is copy documents to/from SharePoint Online. While there are plenty of SharePoint actions in WebCon, the connection to SharePoint Online with a technical user is inappropriate for us. In my opinion all these operations should be done in the context of the currently logged in user.

BTW: We are still on WebCon version 2023.1.2.44. There might be better solutions in the latest version, though I could not find any in the documentation.

Thank you for your help!
Balint

Hello,

this is a very interesting topic. I realized a similar requirement using the following solution:
1. We would need a separate database with small working table (we should not create any database objects in WEBCON databases)
2. Attachment upload to WEBCON instance will be realized using 3 actions described below:
3. At first a Powershell action should fetch the file from your source (Sharepoint, disk folder etc.) and store it on a temporary disk storage. like temp folder. Next, the file should be copied to your working table as image object.
4. Next, WEBCON add attachment action should be used with SQL mode to fetch attachment from your working table.
5. At last, next Powershell action should be used to perform clean up on both the disk storage and your working table.

It might look complicated, but real synchronous processing can be realized (if you really want to force the user to wait...), but if it takes up to 10 seconds, the user should have enough patience.

Alternative solution for bigger files - here we would need.a system step and the the entire operation is asynchronous. You might use OS level task scheduler task - such object can easily be created using Powershell. In such scheduler task you can perform the entire file download and attachment creation. We are limited here with WEBCON system timeout (approx. 2 minutes), so quite a big file can be uploaded. I realized also this kind of integration, but it is obviously much more complicated than the first solution.

Hope, it will help you.

Best wishes for the New Year

Jarek

In reply to: Jarosław Kommisarczyk

Hello,

this is a very interesting topic. I realized a similar requirement using the following solution:
1. We would need a separate database with small working table (we should not create any database objects in WEBCON databases)
2. Attachment upload to WEBCON instance will be realized using 3 actions described below:
3. At first a Powershell action should fetch the file from your source (Sharepoint, disk folder etc.) and store it on a temporary disk storage. like temp folder. Next, the file should be copied to your working table as image object.
4. Next, WEBCON add attachment action should be used with SQL mode to fetch attachment from your working table.
5. At last, next Powershell action should be used to perform clean up on both the disk storage and your working table.

It might look complicated, but real synchronous processing can be realized (if you really want to force the user to wait...), but if it takes up to 10 seconds, the user should have enough patience.

Alternative solution for bigger files - here we would need.a system step and the the entire operation is asynchronous. You might use OS level task scheduler task - such object can easily be created using Powershell. In such scheduler task you can perform the entire file download and attachment creation. We are limited here with WEBCON system timeout (approx. 2 minutes), so quite a big file can be uploaded. I realized also this kind of integration, but it is obviously much more complicated than the first solution.

Hope, it will help you.

Best wishes for the New Year

Jarek

Hello Jarek,
thank you for your thoughts.
Copying documents into a temporary database table is a good approach. It should be simple to create a general solution and reuse it in several processes.
Thanks,
Balint

MVP
In reply to: Balint

Hello Jarek,
thank you for your thoughts.
Copying documents into a temporary database table is a good approach. It should be simple to create a general solution and reuse it in several processes.
Thanks,
Balint

Jarosław's solution will work, it's really creative one, but i'd rather suggest solving it with and SDK, no need to store data on external hard drive, and additional custom table/database.

If you can create PowerShell script which will fetch the data from sharepoint, i believe you will be able to write such function in any programming language, especially with AI tools around.
This won't introduce additional points for documentation, and points of failure.

I'd have to ask - why using technical user in sharepoint is not ok, but having 'Webcon Service' user in Webcon is not a problem? (it exists in each webcon instance).

In reply to: Maksymilian Stachowiak

Jarosław's solution will work, it's really creative one, but i'd rather suggest solving it with and SDK, no need to store data on external hard drive, and additional custom table/database.

If you can create PowerShell script which will fetch the data from sharepoint, i believe you will be able to write such function in any programming language, especially with AI tools around.
This won't introduce additional points for documentation, and points of failure.

I'd have to ask - why using technical user in sharepoint is not ok, but having 'Webcon Service' user in Webcon is not a problem? (it exists in each webcon instance).

Hi Maksymilian,
fetching documents in PowerShell is not the problem. The problem is that WebCon locks the instance during action execution. So, you cannot add an attachment to the current instance with WebCon REST Api until all actions are not finished. You must store the document elsewhere (outside of the current instance) temporary and move the document to the current instance with an additional action “add attachment”.

We have a on-prem installation of WebCon.

Technical user doesn’t fit for us because
- All users are MFA enabled in Entra. There’s no exception :(
- We don’t synchronize local service user to Entra.
- Created by/Modified by should be the user who was logged in in WebCon
- It is not sate of the art – people should be authorized in SharePoint and not a technical user

My hope is that the guys at WebCon development will implement SSO in some near future version. Like they can fetch tokens for REST API calls against graph/ms365, it should be simple to do it also for SharePoint Client function call and I can get rid of my PowerShell actions.
Let's stay optimistic :)

Cheers,
Balint

Did you know that with WEBCON you can automate virtually any process? Even baking cookies 🍪
 
Speaking of cookies: we use the ones that are essential for our website to function properly, as well as additional ones that help us customize our content to your preferences. If you don’t mind cookies, click Accept. If you want to learn more, explore settings.
Settings