Creating OOB Notes with Attachments in a Canvas App
1. Objective
and Context
We’ll create a Canvas
App to manage notes related to an OOB account table. By default, the Notes
table doesn’t include an attachment column. We’ll work around this limitation
by creating a custom card with an attachment control.
2. Setting Up
the App
- Create a new Canvas App in Power Apps.
- Connect the app to your data source (Dataverse).
3. Creating
the Form for Notes
- Add an Edit Form to your app. Connect the Form to Notes Table.
- Add a Form Field (In my case I utilized the Modified On Field) and remove everything inside the card (We'll use it for attachment)
- Inside the card, add an attachment
control. You can obtain the attachment control by either:
- Adding a SharePoint form and copying the
attachment control from there.
- Temporarily creating a document column in a Dataverse table and using its form control.
- Add an attachment control to your custom card (as described earlier).
- Set the onAdd, onRemove,
and onUndoRemove properties of the attachment control to the
following formula:
UpdateContext({locdATA: JSON(Image1.Image, JSONFormat.IncludeBinaryData)})
- When an attachment is added (onAdd event), removed (onRemove event), or undone after removal (onUndoRemove event), the formula will execute.
- It captures the binary data of the attached image using Image1.Image.
- The JSON function converts this binary data into a JSON representation, including the binary content.
5. Adding
Fields to the Form
- In the Form, add the following
fields:
- Document: This will store the base64 content of
the attachment.
- Mime type: To determine the document type.
- File name: To store the name of the attached file.
- Subject: For the note’s subject.
- Description: For additional details about the note.
- Is Document: Indicates whether the note contains an
attachment.
6. Image
Control for Attachments
- Add an image control to the custom card.
- Set the Image property of the image
control to:
First(AttachmentControl.Attachments).Value. - Set the Visible Property of the Image Control to false.
- Set the maximum file attachment count of the attachment control to 1.
7. Default Properties for Document and Mime Type Columns
For the Document string column’s default property, use the following formula:
- If(
-
Form1.Mode = FormMode.New,
-
If(
-
CountRows(AttachmentControl.Attachments) > 0,
-
Last(Split(
-
If(Substitute(locdATA, """", "") =
"null", "", Substitute(locdATA,
"""", "")),
-
","
-
)).Value
-
),
-
ThisItem.Document
- )
- If(
-
Form1.Mode = FormMode.New,
-
If(
-
CountRows(AttachmentControl.Attachments) > 0,
-
First(Split(
-
Last(Split(locdATA, ":")).Value,
-
";"
-
)).Value
-
),
-
ThisItem.'Mime Type'
- )
8. File Name
Text Input Default
Use the following
formula for the File name text input’s default property:
- If(
- Form1.Mode = FormMode.New,
- If(
- CountRows(AttachmentControl.Attachments) > 0,
- First(AttachmentControl.Attachments).Name
- ),
- ThisItem.'File Name'
- )
9. Is Document
Choice Control Default Selected Item
Set the default
selected item for the Is Document choice control to:
- If(
- CountRows(AttachmentControl.Attachments) > 0,
- ["Yes"],
- ["No"]
- )
10. Sav, Understand & Testing
Add a button on the screen to Submit and Test the form.
Add TextInput in the screen and reference the "First(AttachmentControl.Attachments).Value" to understand the outcome we get directly from the attachment control.
Add another TextInput and referene the Image1.Image to understand the usage of image control for getting the file contents of the attachment control.
11. Setting up related Notes (Optional)
If you want to create notes relevant to another Table Record you can also add the Regarding Column in the form.
To Learn More About Polymorphic LookUps
Download the sample solution from here:
And that’s it! You’ve
now created a Canvas App that allows you to manage OOB Notes with attachments.
Remember to customize the field names and controls according to your specific
requirements. Happy app building! 🚀📝📎
What if you want upload multiple attachments and want to create an Annotation for each image selected/uploaded? How do you loop the attachments and extract documentbody for each image?
ReplyDeleteBy adding every item in a collection, using that collection in a gallery, adding image control in the gallery template, loop throught gallerName.All Items
Delete