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-
- First of all open the PowerApps and create a blank canvas app. I named it "UploadFileToSharePoint". Save this app.
- 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.
- 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.
- Now add a blank screen and cut the attachment control from above screen and paste it here.
- Now, make some changes to the properties of this attachment control:-
- Set Items property as blank
- Set BorderColor property as "Transparent"
- Set Tooltip property as blank
- Set DisplayMode property as "DisplayMode.Edit"
- Now, from the right panel:-
- Set Max attachments as 1
- Set NoAttachmentsText as blank
- Set AddAttachmentText as "Select file"
- Set MaxAttachmentsText as "Click on upload button to Save the file."
- Set Tooltip as "Select file"
- Now add a button named "btnUpload" and Text as "Upload".
- In order to save the file in SharePoint, we need two information-
- File Name
- File Content (in Base64)
- The attachment control does not return the content in base64 format.
- For that we need to do some customization.
- 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-
- Add an Image control set the Image property to "Last(DataCardValue9.Attachments).Value"
- Here DataCardValue9 is the attachment control.
- You may set visible property of this image control to false.
- Click on btnUpload and select it's OnSelect property. Here we will fetch the base64 value of file.
- Add below code-
Set( vrImageContent, JSON( Image1.Image, JSONFormat.IncludeBinaryData ) );
- When you bind this variable to a label, you will find something like below-
- The data starts from something ""data:appli...;base64,...""
- 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-
Set( vrBase64Value, Mid( vrImageContent, Find( ",", vrImageContent ) + 1, Len( Mid( vrImageContent, Find( ",", vrImageContent ) + 1 ) ) - 1 ) );
- 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 (").
- Now we will use this final value to upload the file in SharePoint. This completes the first part.
- Now coming to second part.
- Create a document library named "FileFromPowerApps".
- Now open Power Automate. Click on "+ Create". then select "Instant cloud flow".
- Choose the trigger type as "PowerApps". Give a name "SaveFileToSharePoint" and click on Create.
- A blank flow will be created with PowerApps as default action.
- Now click on "+ New step" and choose "Create file (SharePoint)" action.
- Select the SharePoint site, where you had created the document library in "Site Address" field.
- Select the document library name by clicking on folder icon showing in "Folder Path" that you had recently created to store the documents.
- Now the File Name and File Content. For this, click on respective field boxes then click on "Ask in PowerApps" from Dynamic Content window.
- 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.
- 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.
- This Power will ask two values-
- File Name >> Last(DataCardValue9.Attachments).Name
- File Content >> vrBase64Value
- Close the function bracket.
- Save the PowerApps.
- The complete code is-
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 );
- Now, it's time to test it.
- Before that, please remove the button we have added temporarily to get the Power Automate function.
- Please find below a series on snaps to show the working of functionality.
- Clearly file has been uploaded successfully and is able to open.
- This way we can achieve the functionality. This was done for a single file.
- In case, if you wish to upload multiple files,
- You need to reset the Max Attachments count accordingly.
- Instead of Image control, you need to take gallery control with Image and Title.
- Change the function for properties (OnAddFile, OnRemoveFile, OnUndoRemoveFile) and assign the attachments to a local collection.
- 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 !
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.