top of page

Avoiding OneDrive and SharePoint naming issues with Power Automate.

Introduction

When your team uses file systems such as SharePoint, certain folder and file names will cause issues. Examples include names using back and forward slashes, quotation marks, and special characters like question marks. SharePoint and OneDrive will check these and stop you from saving the file with an illegal name.


When using these systems with Power Automate flows, the same name check can cause failures. This is because your files will be blocked from saving. To avoid this, these characters first need to be removed from file names.


In this blog post we’ll use a step-by-step guide to remove illegal characters, increasing productivity in your team by automating repetitive tasks!




Step 1: Create a variable for the string you want to update.

Start by creating a Power Automate flow and adding an action. Search for ‘initialise variable’ under the built-in Variable actions.


Once added, give your variable a name. For instance, we’ve chosen ‘varTitle’ . Then, update the type to ‘String’ for storing the file name as a piece of text. Other variables include integer for use with numbers, and arrays for lists of data.


Tip: Best practices are important. We recommend using the word ‘var’ (short for ‘Variable’) at the beginning with a short, descriptive name afterward.

Next, add the ‘Set variable’ action to update your new, empty variable with the file name. First, use the Name dropdown to select your previously initialised variable. Then, enter the new content in the Value box. In this case, we’re choosing to use the ‘Title’ as provided by the flow’s trigger.


Tip: We recommend updating the name of each of your actions to be more descriptive. This helps you when referring to actions later. This is achieved by clicking the 3 dots in the top right of an action and clicking ‘Rename’.



Step 2: Defining your Illegal characters.

Insert a ‘Compose’ action. This is used for declaring content for us to use at different points in our flow.



Click in the Inputs section of your new Compose action and search for the ‘createArray’ formula in the expressions tab.



This expression takes multiple parameters and creates an Array object which we can search through. Add your illegal characters surrounded by single quotation marks. We’ve used characters from the following source: Restrictions and limitations in OneDrive and SharePoint - Microsoft Support.


Our example:

createArray('~', '\', '"', '#', '%', '&', '*', ':', '<', '>', '?', '/', '\\', '{', '|', '}')

A comprehensive list of Power Automate expressions, including `createArray`, definitions and usages can be found at the following link: Reference guide for expression functions - Azure Logic Apps | Microsoft Learn.





Step 3: Removing your Illegal characters.

For each item in the newly created array, we’re going to search inside our file name and remove each illegal character with an ‘Apply to each’ action. This action works like a loop to iterate through each array item passed into it, allowing us to use the same logic multiple times for different inputs.



In the apply to each action we added, we need to add some data for it to use. We’re going to use the illegal characters defined earlier, by clicking in the ‘Select and output from previous steps’ box and searching for the ‘Outputs’ of the illegal characters action.


Inside of the Apply to each action, insert another Compose action. We’re going to use this to define the expression used to find and remove the illegal character.


Click in the ‘Inputs’ field of this new Compose action, switch to the ’Expression’ tab on the pop-up and add the ‘replace’ formula. This formula takes 3 inputs (known as parameters).


The first parameter is the variable file name we created earlier. Click on the ‘Dynamic content’ view to search for the variable and add it to the formula, then click back to the ‘Expression’ tab.


The second parameter is the current illegal character we’re looking for in the Apply to each loop, which is defined as:

item()

The last parameter is a blank character string, defined as:

‘’

We use a blank character string as empty space to replace the illegal character, effectively removing it.


Finally, we wrap the whole expression with a `trim` expression. This removes any leading or trailing whitespace from the name which may be left over. In full:

trim(replace(variables('varTitle'),item(),''))


Now that the Compose action is created, we update the variable to store this new value using the ‘Set variable’ action.



Step 4: Testing our flow!

Now that we have written out our logic for removing the illegal characters, it’s time to test our hard work to ensure its success.


We start by defining one last Compose action, this is to output the value of our cleanly formatted file name. 


Your flow is now complete! It is now time to test the flow. Simply click on the ‘Test’ button in the top right of your session window to continue. We’ve created a manually triggered flow, so we’ll select the ‘Manually’ option and specify our input.



If you don’t have a manually triggered flow, consider what you need to do to trigger this logic. An example may be creating a Dataverse record to be turned into a file in SharePoint or OneDrive.


We’re using the input text:

“\#hello_world?”.png


Clicking the ‘Test’ button below, we see our name was updated successfully in the result of our last Compose action! The output of this compose action can now safely be added into your SharePoint actions.


Congratulations! You’ve now learned how to remove Illegal characters from a String in Power Automate!


We hope this post has shed some light on optimising your file naming in Power Automate while using OneDrive and SharePoint. We’re eager to hear your experiences and insights on this journey! Your feedback not only motivates us but also helps others in the community. If you’re interested in learning more about the Power Platform, please visit our blog page to continue your learning journey!


 
 
 

댓글


bottom of page