Introduction
SharePoint lists play a crucial role in collaboration and data storage for many businesses. When developing Power Apps, displaying the version history of SharePoint list items can be a challenge. By default, the SharePoint data connection in Power Apps only shows the current state of list items. In this blog post, you will learn how to display the version history in a Power App for SharePoint lists, using the SharePoint REST API and Power Automate.
Step 1: Setting up the SharePoint Data Connection:
First, ensure that you have set up a SharePoint data connection in your Power App. Go to "Data" in the left sidebar and add a new data source. Select "SharePoint" from the list of available data connections and enter the URL of your SharePoint site.
Step 2: Using the SharePoint REST API with Power Automate
In this step, we'll delve deeper into how to make an HTTP request to SharePoint using Power Automate. This will allow us to retrieve the version history data.
- Create a Flow in Power Automate: Begin by navigating to Power Automate and creating a new flow. Select the trigger "Power Apps" as the starting point for the flow.
- Configure the Trigger: Choose the appropriate trigger for your scenario. For instance, you might want the flow to run when a specific action occurs in your Power App (e.g., when a button is clicked).
- Add an Action: Click on the (+) icon to add an action to the flow.
- Search for "Send an HTTP request to SharePoint": In the search bar, type "Send an HTTP request to SharePoint" and select this action.
- Configure the Action: In the "Site Address" field, enter the URL of your SharePoint site.Select the appropriate method (GET, POST, etc.). For version history retrieval, we'll use GET.In the "Uri" field, construct the REST API endpoint to access the version history. It will look something like this:
/_api/lists/getbyid('<List ID>')/items(<Item ID>)/versions
Replace <List ID> and <Item ID> with the specific list ID and item ID from your SharePoint site.
- Handling the Response: After the HTTP request is made, SharePoint will respond with the version history data in JSON format. This data needs to be processed in Power Automate.
- Parse JSON Action: Add a "Parse JSON" action after the HTTP request action.Use the sample data generated from a test run to create the schema. This will help Power Automate understand the structure of the data.
- Use the Parsed Data: Once the JSON data is successfully parsed, you can now access its properties and use them in subsequent steps of your flow.
Step 4: Storing Version History in a Collection with Detailed Code Explanation:
In this step, we will use the provided code snippet to demonstrate how to store the version history data in a collection in Power Apps. The code leverages the ClearCollect function in conjunction with a ForAll loop and a Table function to process and organize the data. For example this function is inside of an OnVisible action of a screen, where the users can view the detailed Version History.
- ClearCollect Function:
ClearCollect(collItemHistory; // Name of the collection ForAll( Table( ParseJSON( 'PowerApp-Action-ManagementgetItemHistory' .Run(SharePointIntegration.SelectedListItemID) .itemhistory ) ); { // Column mappings go here } ) );
- The ClearCollect function initializes or clears the collection named collItemHistory.
- ForAll Function: The ForAll function iterates over each record returned by the Table function.
- Table and ParseJSON Functions: The Table function converts the JSON array of version history data into a table structure. This table is used by ForAll for iteration.The ParseJSON function processes the JSON response from the SharePoint HTTP request. It extracts the version history data.
- Column Mappings: Inside the ForAll loop, we have a set of properties defined. These properties map to the desired columns in the version history data. For example:
id: Value.id; Title: Value.Title; CreatorLookupId: Value.CreatorLookupId; // ... (and so on)
- Each property corresponds to a specific column in the version history.
- Saving Data in the Collection: The data extracted from the version history is then stored in the collItemHistory collection.
- Column Names and Values: You should adjust the property names (e.g., id, Title, etc.) to match the actual column names in your version history data.
- Workflow and Item IDs: The SharePointIntegration.SelectedListItemID parameters are used to identify the SharePoint item for which you want to retrieve version history.
By using this code snippet, you can effectively process and store the version history data in the specified collection in Power Apps. Remember to customize the property names and adjust the code to match your specific SharePoint environment and data structure.
Step 5: Displaying Changed Values in a Gallery - Detailed Explanation:
In this step, we will delve deeper into how to display the version history in a Power Apps gallery and filter out only the changed values for each version. This involves using the Visible property of controls within the gallery.
- Use a Gallery to Display Version History: Begin by adding a gallery control to your Power App. This control will be used to display the version history.
- Set the Items Property of the Gallery: In the Properties pane, set the Items property of the gallery to the collItemHistory collection that was created earlier. This will bind the gallery to the version history data.
- Adjust the Layout of the Gallery: Customize the layout of the gallery to display the relevant information from the version history, such as the columns you've mapped in the previous steps.
- Use the Visible Property for Conditional Display: Now, to filter out and display only the changed values for each version, we'll utilize the Visible property of controls within the gallery.
- Example Formula for the Visible Property:
If( Text(ThisItem.WorkflowStatus) <> Text( First( Filter( collItemHistory; _UIVersionString = ThisItem._UIVersionString - 1 ) ).WorkflowStatus ); true; false )
- In this example formula, we compare the WorkflowStatus of the current version (ThisItem.WorkflowStatus) with the WorkflowStatus of the previous version.If they are not equal (<> denotes "not equal"), then the control is set to be visible (true), indicating that there has been a change. Otherwise, it is set to not be visible (false).
- Explanation of the Formula: ThisItem refers to the current item in the gallery.First(Filter(...)) retrieves the first item from the filtered collection where the _UIVersionString matches the current version minus one (indicating the previous version).We compare the WorkflowStatus of the current item with that of the previous version to determine if there has been a change.
- Customize the Formula for Different Columns: You can customize this formula for other columns as needed, depending on which values you want to track for changes.
By using the Visible property and a conditional formula within the gallery, you can effectively filter and display only the changed values for each version in your Power App. This provides a clear visual representation of the version history for the end users.
Conclusion:
By leveraging the SharePoint REST API in conjunction with Power Automate, we can display the version history of SharePoint list items in Power Apps. This solution opens up new possibilities for data storage and collaboration in SharePoint, seamlessly integrated into Power Apps.
Give it a try and discover how you can make the most of your SharePoint versions in Power Apps!
Comments