SharePoint: Extract String From URL By Splitting Slashes

by Rajiv Sharma 57 views

Hey guys! Ever found yourself needing to pull specific bits of information from a URL in SharePoint Online? It's a pretty common task, especially when you're working with dynamic content or building workflows that rely on URL parameters. Today, we're diving deep into how you can extract strings from URLs by splitting them at the forward slash ("/") using SharePoint Online Designer Workflow. This is super useful for scenarios where you need to dissect a URL and grab the last segment, like a file name or a category. Let’s get started and make those URLs work for you!

Understanding the Challenge

In SharePoint Online, you might encounter URLs like /sites/fahad/Auto/Level 1/Level 1.1. The goal here is to split this string and extract the last part, which in this case is Level 1.1. Then, in the subsequent split, you'd want to grab Level 1. This kind of extraction is incredibly handy for various applications, such as:

  • Categorizing Documents: Imagine you have a document library where files are organized into folders, and the URL reflects this structure. You can use this method to automatically categorize documents based on the folder they reside in.
  • Creating Dynamic Navigation: You might want to build dynamic navigation elements that change based on the current URL. Extracting the last segment can help you highlight the active page or section.
  • Building Breadcrumb Navigation: Breadcrumbs are a great way to help users understand their location within a site. By splitting the URL, you can generate a breadcrumb trail that shows the user's path.
  • Workflow Automation: In workflows, you might need to trigger actions based on specific parts of a URL. For example, you could send a notification when a file is added to a particular folder.

To tackle this, we'll use SharePoint Designer Workflow, a powerful tool that allows you to automate tasks without writing code. We’ll walk through the steps, providing clear explanations and examples along the way. So, buckle up, and let's get into the nitty-gritty of URL splitting!

Prerequisites

Before we jump into the solution, let’s make sure you have everything you need. Here’s a quick checklist:

  • SharePoint Online Access: You’ll need access to a SharePoint Online site where you have the necessary permissions to create and modify workflows. Usually, site owners or those with design permissions can create workflows.
  • SharePoint Designer: Make sure you have SharePoint Designer installed on your machine. It’s a free tool from Microsoft that allows you to build and manage workflows, forms, and other components in SharePoint.
  • Basic Understanding of SharePoint Workflows: A little familiarity with SharePoint Designer workflows will go a long way. If you're new to workflows, don't worry! We'll explain each step, but having a basic grasp of how workflows operate will be beneficial.
  • Target URL: Have a sample URL ready to test with. In our example, we’re using /sites/fahad/Auto/Level 1/Level 1.1, but you can use any URL that you need to split.

With these prerequisites in place, you'll be well-prepared to follow along and implement the solution. Let’s move on to the step-by-step guide on how to extract those strings!

Step-by-Step Guide to Splitting URLs in SharePoint Designer Workflow

Alright, let's get our hands dirty and walk through the process of splitting URLs in SharePoint Designer Workflow. We'll break it down into manageable steps, so you can easily follow along. By the end of this guide, you’ll be a URL-splitting pro!

Step 1: Open SharePoint Designer and Connect to Your Site

First things first, launch SharePoint Designer. Once it’s open, you’ll need to connect to your SharePoint Online site. Here’s how:

  1. Click on Open Site in the SharePoint Designer interface.
  2. Enter the URL of your SharePoint Online site in the dialog box that appears. It usually looks something like https://yourtenant.sharepoint.com/sites/yoursite.
  3. SharePoint Designer will prompt you to log in with your Microsoft 365 credentials. Make sure you use an account with the necessary permissions to create workflows.
  4. Once you’re logged in, SharePoint Designer will load your site, and you’ll be ready to start creating a workflow.

Step 2: Create a New List Workflow

Now that we’re connected to the site, let’s create a new workflow. We’ll be creating a list workflow, which means it will be associated with a specific list or library in your SharePoint site.

  1. In the SharePoint Designer navigation pane, click on Workflows.
  2. In the Workflows tab, click on List Workflow.
  3. You’ll be prompted to select a list or library for your workflow. Choose the list where you want to use the URL splitting functionality. For testing purposes, you might want to create a new list specifically for this.
  4. Give your workflow a descriptive name, such as “URL Splitting Workflow.”
  5. You can also add a description to help you remember what the workflow does. This is especially useful if you have multiple workflows in your site.
  6. Choose the workflow options. You can set the workflow to start automatically when an item is created or modified, or you can start it manually. For our example, let’s choose to start it manually.
  7. Click OK to create the workflow. SharePoint Designer will open the workflow designer, where you can start adding actions and conditions.

Step 3: Define Variables

Variables are like containers that hold data during the workflow's execution. We’ll need a few variables to store the URL, the split parts, and the results. Here are the variables we’ll create:

  1. OriginalURL (Type: String): This variable will store the original URL that we want to split.
  2. LastSegment (Type: String): This variable will store the last segment of the URL after the first split.
  3. SecondLastSegment (Type: String): This variable will store the second-to-last segment of the URL after the second split.

To create these variables:

  1. In the workflow designer, click on Local Variables in the ribbon.
  2. Click on Add Variable.
  3. Enter the variable name (e.g., “OriginalURL”), select the data type (e.g., “String”), and click OK.
  4. Repeat this process for the other variables.

Step 4: Get the URL

Next, we need to get the URL that we want to split. For this example, let’s assume the URL is stored in a field called “URL” in our list. We’ll use the “Set Workflow Variable” action to retrieve the URL from the list item and store it in our OriginalURL variable.

  1. In the workflow designer, click on Action in the ribbon.
  2. Select Set Workflow Variable from the list of actions.
  3. Click on the first variable link in the action description.
  4. Choose the OriginalURL variable from the list.
  5. Click on the second value link.
  6. Click on the fx button to open the lookup dialog.
  7. In the lookup dialog, select Current Item as the Data Source.
  8. Select the URL field as the Field from Source.
  9. Click OK to close the lookup dialog.
  10. Click OK again to close the action configuration dialog.

Now, the OriginalURL variable will contain the URL from the list item.

Step 5: Split the URL to Get the Last Segment

This is where the magic happens! We’ll use the “Find String in String” action to locate the last forward slash in the URL and then extract the segment after it.

  1. Add another Action to your workflow.
  2. Select Find String in String from the list of actions.
  3. Click on the first string link.
  4. Click on the fx button to open the lookup dialog.
  5. Select Workflow Variables as the Data Source.
  6. Choose the OriginalURL variable as the Field from Source.
  7. Click OK.
  8. Click on the second substring link and enter “/” (forward slash).
  9. Click on the third Start at position link and enter “0”.
  10. Click on the Output to Variable link and choose a new variable (click the New Variable button). Name it LastSlashPosition and set the type to Number. Click OK.
  11. Click OK to close the action configuration dialog.

Now we have the position of the last slash. Next, we’ll extract the last segment.

  1. Add another Action to your workflow.
  2. Select Extract Substring from Index of String from the list of actions.
  3. Click on the first string link.
  4. Click on the fx button to open the lookup dialog.
  5. Select Workflow Variables as the Data Source.
  6. Choose the OriginalURL variable as the Field from Source.
  7. Click OK.
  8. Click on the second start position link.
  9. Click on the fx button to open the lookup dialog.
  10. Select Workflow Variables as the Data Source.
  11. Choose the LastSlashPosition variable as the Field from Source.
  12. Click OK.
  13. Add 1 to the LastSlashPosition variable by clicking the plus sign and typing 1. This will start the extraction after the slash.
  14. Click OK.
  15. Click on the Output to Variable link and choose the LastSegment variable. Click OK.
  16. Click OK to close the action configuration dialog.

Step 6: Split Again to Get the Second-to-Last Segment

To get the second-to-last segment, we repeat a similar process, but this time, we’ll work with the OriginalURL up to the last slash.

  1. Add another Action to your workflow.
  2. Select Extract Substring from String, from the Start from the list of actions.
  3. Click on the first string link.
  4. Click on the fx button to open the lookup dialog.
  5. Select Workflow Variables as the Data Source.
  6. Choose the OriginalURL variable as the Field from Source.
  7. Click OK.
  8. Click on the second length link.
  9. Click on the fx button to open the lookup dialog.
  10. Select Workflow Variables as the Data Source.
  11. Choose the LastSlashPosition variable as the Field from Source.
  12. Click OK.
  13. Click OK to close the action configuration dialog.

Now we have a substring of the URL up to the last slash. Let’s find the second-to-last slash in this substring.

  1. Add another Action to your workflow.
  2. Select Find String in String from the list of actions.
  3. Click on the first string link.
  4. Click on the fx button to open the lookup dialog.
  5. Select Workflow Variables as the Data Source.
  6. Choose a new variable where you stored the substring from the previous step (let's call it URLBeforeLastSegment).
  7. Click OK.
  8. Click on the second substring link and enter “/”.
  9. Click on the third Start at position link and enter “0”.
  10. Click on the Output to Variable link and choose a new variable (let's call it SecondLastSlashPosition and set the type to Number). Click OK.
  11. Click on the option to find the last occurrence of the substring.
  12. Click OK to close the action configuration dialog.

Now we have the position of the second-to-last slash. Let’s extract the second-to-last segment.

  1. Add another Action to your workflow.
  2. Select Extract Substring from Index of String from the list of actions.
  3. Click on the first string link.
  4. Click on the fx button to open the lookup dialog.
  5. Select Workflow Variables as the Data Source.
  6. Choose the URLBeforeLastSegment variable as the Field from Source.
  7. Click OK.
  8. Click on the second start position link.
  9. Click on the fx button to open the lookup dialog.
  10. Select Workflow Variables as the Data Source.
  11. Choose the SecondLastSlashPosition variable as the Field from Source.
  12. Click OK.
  13. Add 1 to the SecondLastSlashPosition variable by clicking the plus sign and typing 1. This will start the extraction after the slash.
  14. Click OK.
  15. Click on the Output to Variable link and choose the SecondLastSegment variable. Click OK.
  16. Click OK to close the action configuration dialog.

Step 7: Log the Results (Optional)

To verify that our workflow is working correctly, we can log the extracted segments. This is super helpful for debugging.

  1. Add another Action to your workflow.
  2. Select Log to History List from the list of actions.
  3. Click on the message link.
  4. Click on the fx button to open the lookup dialog.
  5. Build a string that includes the extracted segments. You can use the following formula: Last Segment: [%Workflow:Variable:LastSegment%], Second Last Segment: [%Workflow:Variable:SecondLastSegment%]
  6. Click OK to close the lookup dialog.
  7. Click OK to close the action configuration dialog.

Step 8: Update the List Item (Optional)

If you want to store the extracted segments in the list item, you can use the “Update List Item” action.

  1. Add another Action to your workflow.
  2. Select Update List Item from the list of actions.
  3. Click on the this list link and make sure the current list is selected.
  4. Click on the Add button to set the fields.
  5. Choose the field where you want to store the LastSegment and set its value to the LastSegment variable.
  6. Repeat for the SecondLastSegment.
  7. Click OK to close the action configuration dialog.

Step 9: Publish and Test Your Workflow

We’re almost there! Now, it’s time to publish the workflow and see if it works as expected.

  1. In the workflow designer, click on Publish in the ribbon.
  2. Once the workflow is published, go to your list in SharePoint.
  3. Create a new item or edit an existing one.
  4. If you set the workflow to start manually, select the item and start the workflow from the item menu.
  5. Check the workflow history to see if the workflow ran successfully and if the segments were extracted correctly.
  6. If you added the “Log to History List” action, you can view the log messages to see the extracted segments.

And that’s it! You’ve successfully created a workflow that splits URLs and extracts the last and second-to-last segments. Pat yourself on the back!

Troubleshooting Common Issues

Even with the best guides, sometimes things don’t go as planned. Here are a few common issues you might encounter and how to troubleshoot them:

  • Workflow Doesn’t Start: Make sure the workflow is published and that the start options are configured correctly. If you set it to start manually, ensure you’re starting it from the item menu.
  • Incorrect Segment Extraction: Double-check the logic in your “Find String in String” and “Extract Substring” actions. Ensure the start positions and lengths are calculated correctly. Use the “Log to History List” action to log intermediate values and help you debug.
  • Variable Not Set: If a variable isn’t being set correctly, verify that the lookup is configured properly and that the source field contains the expected value.
  • Workflow Errors: Check the workflow history for error messages. These messages can provide valuable clues about what went wrong.

By systematically checking these areas, you can usually identify and resolve any issues you encounter. And remember, practice makes perfect! The more you work with SharePoint Designer workflows, the more comfortable you’ll become.

Best Practices for Working with URLs in SharePoint Workflows

To make your life easier and your workflows more robust, here are some best practices to keep in mind when working with URLs in SharePoint workflows:

  • Handle Empty URLs: Always check if the URL field is empty before attempting to split it. You can use a “Condition” action to check if the OriginalURL variable is empty. This prevents errors and makes your workflow more resilient.
  • Normalize URLs: If you’re dealing with URLs from different sources, they might have different formats. Consider normalizing the URLs by removing trailing slashes or ensuring they start with a specific protocol (e.g., https://).
  • Use Descriptive Variable Names: As we did in our example, use clear and descriptive names for your variables. This makes your workflow easier to understand and maintain.
  • Add Comments: Use comments to explain what each action does. This is especially helpful for complex workflows or when you’re working in a team.
  • Test Thoroughly: Always test your workflow with different URLs to ensure it handles all cases correctly. This includes URLs with varying lengths and structures.
  • Keep It Simple: If your workflow is getting too complex, consider breaking it down into smaller, more manageable workflows. This makes it easier to debug and maintain.

By following these best practices, you’ll be well-equipped to handle URL manipulation in SharePoint workflows like a pro. Now, go forth and conquer those URLs!

Conclusion

So, there you have it! We’ve walked through the process of extracting strings from URLs by splitting them at the forward slash in SharePoint Online Designer Workflow. We covered everything from setting up the workflow to handling common issues and implementing best practices. Whether you're categorizing documents, creating dynamic navigation, or automating tasks, this technique is a valuable addition to your SharePoint toolkit.

Remember, the key to mastering SharePoint workflows is practice. Don’t be afraid to experiment and try new things. The more you work with workflows, the more comfortable and confident you’ll become. And who knows? You might even discover new ways to use URL splitting to solve other challenges in your SharePoint environment.

Thanks for joining me on this URL-splitting adventure! I hope you found this guide helpful and informative. Now, go out there and make those URLs work for you. Happy workflowing, guys!