Showing posts with label Attachment. Show all posts
Showing posts with label Attachment. Show all posts

Sunday, September 19, 2021

PowerApps: Upload File To SharePoint

 Hello Friends,

Welcome back with another post on PowerApps. Today, we will talk something different. Sometimes, you had faced a situation where you need to upload a document to document library. As we don't have any File Upload control in PowerApps, then how do we achieve this? Let's try.

For this, we have to first know the process, we have to follow. We will use the attachment control, provided by the List Screen. Along with this, we will use Power Automate to save the file in SharePoint. Additionally, we will make some manipulation to get the file content. Let's start-

  1. First of all open the PowerApps and create a blank canvas app. I named it "UploadFileToSharePoint". Save this app.
  2. Now add a New Screen of type "Form". Now connect it's data source to any of the list of SharePoint. Basically, we have to get the attachment control from the Form. The moment you link this form with any list, all the fields control will automatically gets populated on form.
  3. Check, if the Attachments control is there. If not, then click on Fields provided just below the Data Source option in right panel and choose "Attachments" control.
  4. Now add a blank screen and cut the attachment control from above screen and paste it here.
  5. Now, make some changes to the properties of this attachment control:-
    1. Set Items property as blank
    2. Set BorderColor property as "Transparent"
    3. Set Tooltip property as blank
    4. Set DisplayMode property as "DisplayMode.Edit"
  6. Now, from the right panel:-
    1. Set Max attachments as 1
    2. Set NoAttachmentsText as blank
    3. Set AddAttachmentText as "Select file"
    4. Set MaxAttachmentsText as "Click on upload button to Save the file."
    5. Set Tooltip as "Select file"
  7. Now add a button named "btnUpload" and Text as "Upload".
  8. In order to save the file in SharePoint, we need two information-
    1. File Name
    2. File Content (in Base64)
  9. The attachment control does not return the content in base64 format.
  10. For that we need to do some customization.
  11. We will add an Image control. The content of file will be linked to this image control and then we will use this image content to fetch the base64 value of file. Let's see-
  12. Add an Image control set the Image property to "Last(DataCardValue9.Attachments).Value"
  13. Here DataCardValue9 is the attachment control.
  14. You may set visible property of this image control to false.
  15. Click on btnUpload and select it's OnSelect property. Here we will fetch the base64 value of file.
  16. Add below code-
  17. Set(
        vrImageContent,
        JSON(
            Image1.Image,
            JSONFormat.IncludeBinaryData
        )
    );
    
  18. When you bind this variable to  a label, you will find something like below-
  19. The data starts from something ""data:appli...;base64,...""
  20. We need to remove the string ""data:appli...;base64," and the ending double quote which is present at the end of the data. After removing these, we will get the final base64 value of file content. For this, add below code just after the above one-
  21. Set(
        vrBase64Value,
        Mid(
            vrImageContent,
            Find(
                ",",
                vrImageContent
            ) + 1,
            Len(
                Mid(
                    vrImageContent,
                    Find(
                        ",",
                        vrImageContent
                    ) + 1
                )
            ) - 1
        )
    );
    
  22. This code is fetching the data by skipping first set of non desired string ("data:appli...;base64,) and then extracting the next actual content by skipping the last character which is double quote (").
  23. Now we will use this final value to upload the file in SharePoint. This completes the first part.
  24. Now coming to second part.
  25. Create a document library named "FileFromPowerApps".
  26. Now open Power Automate. Click on "+ Create". then select "Instant cloud flow".
  27. Choose the trigger type as "PowerApps". Give a name "SaveFileToSharePoint" and click on Create.
  28. A blank flow will be created with PowerApps as default action.
  29. Now click on "+ New step" and choose "Create file (SharePoint)" action.

  30. Select the SharePoint site, where you had created the document library in "Site Address" field.
  31. Select the document library name by clicking on folder icon showing in "Folder Path" that you had recently created to store the documents.
  32. Now the File Name and File Content. For this, click on respective field boxes then click on "Ask in PowerApps" from Dynamic Content window.
  33. It will create the input parameters for PowerApps by default. Now click on "File Content" field and delete the input parameter reflecting over there. Then click on Expression window and select the function "base64ToBinary" and pass that input parameter to this function. This was done because the file content we will receive from PowerApps will be in base64 format and it needs to be converted into binary format before saving.
  34. Save this flow and come back to PowerApps. Add a button and select its "OnSelect" property. Click on Action >> Power Automate from top bar menu. A list of available supported Power Automates will be displayed. Choose the recently created Power Automate. Wait for couple of seconds till it add the Power Automate to the OnSelect action. After getting added, it will look like below screenshot. Now copy the function showing "SaveFileToSharePoint.Run(" and paste the same at the end in our btnUpload >> OnSelect property code.
  35. This Power will ask two values-
    1. File Name >> Last(DataCardValue9.Attachments).Name
    2. File Content >> vrBase64Value
  36. Close the function bracket.

  37. a
  38. Save the PowerApps.
  39. The complete code is-
  40. Set(
        vrImageContent,
        JSON(
            Image1.Image,
            JSONFormat.IncludeBinaryData
        )
    );
    Set(
        vrBase64Value,
        Mid(
            vrImageContent,
            Find(
                ",",
                vrImageContent
            ) + 1,
            Len(
                Mid(
                    vrImageContent,
                    Find(
                        ",",
                        vrImageContent
                    ) + 1
                )
            ) - 1
        )
    );
    SaveFileToSharePoint.Run(
        Last(DataCardValue9.Attachments).Name,
        vrBase64Value
    );
    
  41. Now, it's time to test it.
  42. Before that, please remove the button we have added temporarily to get the Power Automate function.
  43. Please find below a series on snaps to show the working of functionality.



  44. Clearly file has been uploaded successfully and is able to open.
  45. This way we can achieve the functionality. This was done for a single file.
  46. In case, if you wish to upload multiple files, 
    1. You need to reset the Max Attachments count accordingly.
    2. Instead of Image control, you need to take gallery control with Image and Title.
    3. Change the function for properties (OnAddFile, OnRemoveFile, OnUndoRemoveFile) and assign the attachments to a local collection.
    4. On btnUpload >> OnSelect, use ForAll function to save files one by one automatically.

With this, I am concluding this post.
Happy Coding !!!
Will see you again with some new topics.

Stay Safe !
Stay Healthy !

Sunday, August 8, 2021

Power Automate: Send Multiple Attachments In Approval EMail

Hello Friends,

Welcome back with another post on Power Automate. In my last post, we had tried to send multiple attachments (uploaded in a list item) in email through Power Automate. In last, I asked you, what if you have to send all these attachments in "Start and wait for approval" email? Hope you have already implement. Anyways, let's try-

  1. First of all, please visit my last post to create the list and the power automate.
    1. Power Automate: Send Multiple Attachments In EMail
  2. Now, the way, we sent the attachments in an email is little bit different to what we have to do for approval mail action.
  3. For simple Email, the array format was:-
    1. {
      "Name":
      "ContentBytes":
      }
      
  4. For approval email, if you use the same format, it will through error of ContentBytes. Therefore, we will change this format. For approval email, we will use below format.
    1. {
      "Name":
      "content":
      }
      
  5. Rest things will be same. Means-
    1. Declare a variable of Array type using "Initialize variable" action with name as "MultipleAttachmentsArrayForApproval".


    2. Use "Apply to each", "Get attachment content" and "Append to array variable" actions to create the array of attachment contents.


    3. Now add action "Start and wait for approval" and input all the required information. Add this variable in attachments section. For this, please read my last post whose link is given above. We will apply same process here (see point no 27 of last post).

  6. All done. Save the workflow and test it by adding a new item in SharePoint list. Well, I have already one, so I will test in it.
  7. I had created a list item with 4 attachments-

  8. Just clicked on Save button. After waiting a couple of minutes, I got a mail in my mailbox.

  9. Wow, I got a mail to approve or reject the item request. The interesting part is that, all 4 attachments are also got delivered.
  10. This way, you can send attachments in approval email also.
  11. Please note that these attachments will show only in mail box. If you are using MS Teams, then you will get approval request there also but without attachment. Even, if you are using your mobile to approve/reject the request then their also you will not find the attachments.

  12. This way, you can send multiple attachments with Email as well as Approval Flow EMail.
With this, I am concluding this post.
Happy Coding !!!
Will see you again with some new topics.

Stay Safe !
Stay Healthy !

Power Automate: Send Multiple Attachments In EMail

 Hello Friends,

Welcome back with another post on Power Automate. Have you ever face any requirement where all the attachments of any list item needs to send through Email? Let's suppose a user submits a request in system along with multiple attachments. Now the details of this request needs to by sent to his/her manager along with the attachments, he/she has uploaded. How will you do? No Idea, let's try:-

  1. I have created a list "TestList" with only one column i.e. "Title".

  2. Let's create a Power Automate. For this click on App Launcher (9 dots) at top left corner and choose "Power Automate".
  3. Click on "+ Create" >> "Automated cloud flow". It will show a popup window where you need to input the name of the workflow. Then choose the type of trigger upon which this workflow will execute.
  4. Choose "When an item is created (SharePoint)".

  5. Click on "Create".
  6. It will create a blank workflow with a default action "When an item is created". Choose the SiteName and the list name upon which you need to execute this workflow.


  7. Now take a pause. First discuss how we will perform next. First we need to 
    1. Get all attachments list
    2. Get content of each attachment and make an array of the same with File Name and ContentBytes.
    3. Send this array as attachment in Email.
  8. Let's start-
  9. Choose an action Get attachments


  10. Now define a variable of Array type.


  11. Now we will use Apply to each action to get attachment contents and append in array.
  12. For this, first we will add action "Get attachment content" and fill properties as below. The Id field will be mapped with the Item ID (when an item is created) while File Identifier will be mapped with the Id (Get attachments).


  13. The moment you bind File Identifier field, you will see that "Apply to each" action automatically binds with the Get attachment content action.


  14. Now, add one more action "Append to array variable". We will append all attachments in the variable defined earlier.


  15. The format of array is-
    1. {
      "Name":
      "ContentBytes":
      }
      
  16. Now the last part. Add an action "Send an email (V2)". Specify the To, Subject, Body as per the requirement. Once done, click on "Show advanced options".


  17. It will expand the window and will show Cc, Bcc, Attachments options. By default the system provides to give display name and content separately. But as we have the array here, so we will click the toggle icon showing ahead to Attachments section. This toggle icon toggle the attachments linking process between single to array and vice versa.

  18. After toggle, click on box provided for attachments and select the array variable.
  19. That's all. Save the flow and test the same.
  20. To get the same tested, I came back to the SharePoint list and added one list with 4 attachments
    1. .txt file
    2. .docx file
    3. .pdf file
    4. .png file


  21. First, I check the Run history of workflow. I found it ran successfully.

  22. Now, I will check my mail box.
  23. Wow, I had an email in my inbox with the same attachments I had uploaded.


  24. This way, you can, send all the attachments, user has uploaded in system.
  25. With this, we have achieved our goal.
  26. Now, I am giving you a task. Let us suppose, I have to send all these attachments in approval mail "Start and wait for approval", then how would you do that? Try it.
  27. I will, show the same in my next post.
    1. Power Automate: Send Multiple Attachments In Approval EMail
With this, I am concluding this post.
Happy Coding !!!
Will see you again with some new topics.

Stay Safe !
Stay Healthy !

Saturday, May 22, 2021

Power Automate - Read Email And Save In SharePoint List

Hello Friends,

Welcome back with another post on Power Automate. Today, we will try to read an incoming mail in Office 365 Outlook and then try to save the content of mail, it's attachment and the mail itself in SharePoint list. Let's start-

  1. First of all, we will create a SharePoint list to store the email content. I had created a list named "OutlookEmailToSharePointList" as below-
  2. Now we will create flow in Power Automate. For this, we will click on Integrate >> Power Automate >> See your flows.

  3. It will open the Power Automate portal where you need to click on My flows >> New flow >> Automated cloud flow.

  4. It will open the popup where you can choose the type of flow trigger "When a new email arrives (V3)", give the name of flow and click on Create button.
  5. It will be created as below-
  6. Now, we will add Action to it. Click on "+ New step" and choose "Html to text (preview)" action.
  7. Now add the body tag in the body content of this action.
  8. This will convert the Email body HTML content to plain text. Actually, the body content of an email is in HTML format containing header, span, paragraph tags. If we save the content as such, it will be complex to read. Therefore first we convert the HTML content to plain text and then we will save it in SharePoint list.
  9. Now click on "+ New step" and choose Create Item (SharePoint) action. Provide the Site address and the SharePoint list we had created above.
  10. Now give the values to the columns.

  11. The important point to remember here is that we will use the output of "Html to text" action to pass in Body column.
  12. With this the content part is done. Now, what, if the email has attachments also?
  13. For this, we will first get all attachments and update the recently create SharePoint list item by attaching those attachments. So, click on "+ New step" and choose "Get Attachment (V2) Office 365 Outlook".
  14. Now pass "Message Id", "Attachments Attachment Id" and "To". The moment you select "Attachments Attachment Id", you will see that "Apply to each" action will automatically appears and embeds to the flow. This is because the flow knows that they may be multiple attachments. 😎




  15. Now click on "Add an action" and choose "Add attachment" action.
  16. The Id here is the id of item created in SharePoint list. You will find this in "Create Item" section in the dynamic content window opened at right side.
  17. The values for "File Name" and "File Content" will be available from "Get Attachment (V2)". 
  18. This will update all attachment one by one in the SharePoint list item.
  19. Now lastly, if you want to save the email (.eml) as well in SharePoint, then you need to first export that email and use Add Attachment action. Let's see below-
  20. Click on "+ New step" and choose "Export email (V2)".
  21. Pass "Message Id", "Original Mailbox Address" as "Message Id" and "To" from dynamic content window (when a new mail arrives (V3))-

  22. Lastly, once again use "Add Attachment" action and attach the email to SharePoint list item.
  23. Let's test it.
  24. I am sending below mail to my email id, I had I created this flow.

  25. Wait for a couple of minute to get flow executed and get completed. Let's see the SharePoint list.
  26. Wow, the record is here.




  27. This way, we can save email to SharePoint with attachments.
  28. If you wish add some filters upon the type of email you want to add in SharePoint, you can add those filter in the very first step "When a new email arrives (V3)".



With this, I am concluding this post.
Happy Coding !!!
Will see you again with some new topics.

Stay Safe !
Stay Healthy !