Create Shortened (‘TinyURL’) links from your SharePoint Workflow
When we released the Muhimbi URL Shortener for SharePoint, we knew that even though it was a great product, we couldn’t please everyone. For example, all generated Short URLs point to list items’ Form View, from where the item can be edited, deleted or opened in an external application. This behaviour is by design, but some customers prefer to have the short URL point directly to the file, e.g. a Generated PDF.
In this article we describe how to generate a short URL from a SharePoint Designer workflow with full control over which view the URL points to using both MuSH and the Workflow Power Pack.
A quick introduction for those not familiar with the product: The Muhimbi Workflow Power Pack for SharePoint allows custom C# or VB.NET code to be embedded in SharePoint Designer Workflows without the need to resort to complex Visual Studio based workflows, the development of bespoke Workflow Activities or long development cycles.
The solution presented below creates a short URL for the current item the workflow is acting on. An optional value can be specified in Parameter 1 to control if the generated URL points to the file or display form using the ‘displayform’ and ‘file’ values.
Create the workflow as follows:
-
Download and install the Muhimbi Workflow Power Pack for SharePoint.
-
Download and install the Muhimbi URL Shortener for SharePoint.
-
We need to be able to access functionality in the Muhimbi.SharePoint.URLShortener assembly. Add this reference to the relevant Web Application using the Workflow Power Pack Central Administration screens as described in the Administration Guide.
-
Make sure you have the appropriate privileges to create workflows on a site collection.
-
Create a new workflow using SharePoint Designer.
-
On the Workflow definition screen associate the workflow with the list or library of your choice, tick the box next to ‘ Automatically start this workflow when a new item is created’ and proceed to the next screen.
-
Click the Actions button and insert the Execute Custom Code action .
-
Click this variable and create a new Workflow Variable named shortURL using string as the type.
-
Click parameter 1 and enter file or displayform or leave it empty to default to displayform.
-
Insert the following C# based code by clicking this code.
using Muhimbi.SharePoint.URLShortener;
// ** Where do we want the shortened URL to point to?
string target = MyWorkflow.Parameter1 == null ? “displayform” : (String) MyWorkflow.Parameter1; string sourceURL = null; // ** Point the short URL to the file or the file’s info form switch (target.ToLower()) { case “file”: sourceURL = String.Format(”{0}/{1}”, MyWorkflow.Web.Url, MyWorkflow.Item.Url); break; case “displayform”: sourceURL = String.Format(”{0}/{1}?ID={2}”, MyWorkflow.Web.Url, MyWorkflow.List.Forms[PAGETYPE.PAGE_DISPLAYFORM].Url,
MyWorkflow.Item.ID);
break; default: throw new ArgumentException(“Unknown target specified, use ‘file’ or ‘displayform’.”); } // ** Add a description to the short URL, amend if necessary string description = “Generated from workflow”; // ** Carry out the shortening process ShortenedURL shortURL = Muhimbi.SharePoint.URLShortener.URLShortener.CreateURLViaWebService( MyWorkflow.Site.WebApplication.Id, sourceURL, description); // ** Return the fully qualified URL back to the workflow MyWorkflow.ReturnValue = shortURL.GetFullyQualifiedShortURL();
-
Click the Actions button, select Log to History List, click this message, set the Source to Workflow Data and the Field to shortURL.
-
Close the Workflow Designer and add an item to your list or library to trigger the workflow.
-
Once the workflow has finished, click the Completed link to see the generated short URL.
Naturally this is just a simple example. Under normal circumstances you would do something more useful with the shortURL workflow variable, for example embed it in an email.
Clavin is a Microsoft Business Applications MVP who supports 1,000+ high-level enterprise customers with challenges related to PDF conversion in combination with SharePoint on-premises Office 365, Azure, Nintex, K2, and Power Platform mostly no-code solutions.