Slinger's Thoughts

May 20, 2008

Use variables to create html link in SharePoint

Filed under: SharePoint — Tags: , — slingeronline @ 3:29 pm

Had an interesting one present itself with an odd solution, but I’ll be damned it works and it’s very small, compared to the alternative.  Here’s what I wanted our site to do, and how I did it.  For our site we wanted to keep our client and vendor information separate.  The problem is they are accessing the same information.  Our solution was to make a copy of that information, as appropriate.  Think of it like this.  A document gets uploaded to a document library.  One of the metadata fields is the vendor’s name.  Our workflow will check some critical pieces of information, one of which is the vendor’s name, and if it finds a match, it will copy that file to a document library the vendor has permission to view.  You can see details on what I did here.

If you looked at what I had done, that workflow would check a field and match a value to determine which document library needed to receive the copy of the file.  If we have 150 vendors, that means that workflow would have 150 else if branches.  I can’t think that is a good thing.  What would be nicer, is to pull the vendor name, and use that in the html for the list that I am going to be updating.  The problem is, each of our vendors names would show up in the middle of the url.  So, if the file was for “Vendor1,” then the url I would want to copy the library item to would be http://oursharepointsite/vendor/doclibrary.  Well, the oursharepointsite and doclibrary portions of that url are not going to change, but the vendor is.  So what I needed to do was find some sort of syringe to inject a variable into that url so that “Vendor” in the url would match “Vendor1” in the item metadata.  One of my co workers came up with the solution for that.  What we need to do is create several variables to hold each section of the url, and then build the url using those variables.  Now you have the run down, let’s go through it step by step.  I’m going to assume that you have already created two subsites and document libraries in each.  For one of the metadata columns, we want our users to pick from a list, so that everything is consistent.  What we want to do, is copy an item from this document library to a document library on another site.  We’ll assume that both document libraries are identical with the exception of which site or subsite they reside on.  We’ll say the URL’s for these document libraries are http://mysite/documentlibrary and http://yoursite/name/documentlibrary. When we upload a document to our document library, we want a copy to be automatically placed in the other document library.  The trick is we probably have many different “name” subsites, so we need to make sure that we copy our document to the correct one. For this we will need four variables in our workflow for SharePoint Designer.  The first variable is the beginning of the URL, which we know will always be the same. The second variable will be the metadata item we are going to capture to insert into our URL, and the third variable will be the final part of the path to the document library we are copying the file to.  The fourth variable will be a dynamic string variable that is composed of these other three variables.  Our workflow will have two steps, the first of which will be setting all of these variables.  In the workflow designer, select the “Variables” button at the bottom and create the four variables we are going to need. I labelled mine, “URL”, “Name”, “Library”, and “FullURL.”  All of these variables will be strings.  Once we have our variables, we need to set what they are going to be.   Select “Actions” and then “Set Workflow Variable.”  Click on “variable” and select “Variable:URL” and then for the value to set the variable to, click the display builder, (the button to the left of the “fx” button, and then type in http://yoursite/. Click actions again, and set another workflow variable.  This time set the variable “library” to “documentlibrary” the same way we did before.  (If you need to know the path to the document library we want to copy the file to, open the document library in your browser, and remove the /Forms/Allitems.aspx from the URL in the browser.  We want everything between the “name” value we want to set and /Forms/.)  The variable that we want to insert will be a little different.  So far we have the beginning and end to our URL, We know which document library we want to update, and we know the beginning of the URL to get there. We need to get which subsite we will be copying this information to from our metadata.  Click actions, and Set workflow variable again, but this time, click the “Fx” button after clicking “value.” For “source” leave that as current item, and then select the field that contains the value we want to inject into the url.  Now we have the three pieces of our URL, we need to create one whole URL our of it.  Click on “Actions” and then “Build Dynamic String.” click on “Dynamic String” and in the dynamic string builder, all we are going to do is add lookups. Click on Add lookup, change the source to “workflow data”, and then select the first variable that would go into our URL, variable:URL.  Click Add Lookup again, change the source to “workflow data” again, and select the second variable that will go into constructing our URL, variable:name.  One more time, click on Add Lookup, change the source to workflow data, and select the last variable for our url, variable:library.  The variable that we want to store this in will be the fourth variable we created at the beginning, FullURL.  Now we have a full url that we can insert into our workflow, that will update dynamically depending on what the value in our metadata column is from our document library.  Once we have set all of the variables, we need to create a new step to copy all of this information to the appropriate document library.  This will work pretty much the same as my previous solution, with a few changes.

Click on Actions, and then “Copy Item Extended.” (If you don’t have the extended copy item in your workflow designer, you need to install “Useful SharePoint Designer Custom Workflow Activities,” from Codeplex, located here.)  Click on “this list,” leave “current item” selected and click “OK.” Click on “this url,” and then click on the “fx” button.  Select “workflow data” as the source, and then for “Field” select the workflow variable we created with the dynamic string, variable:FullURL.  Now, when a document gets uploaded to the document library we created the work flow on, a copy of the file will be placed in the appropriate document library on another site, as determined by our metadata column.  Easy, isn’t it?

Some things to watch out for; the URL and the metadata column need to match. “Vendor Name” and “VendorName” are not the same thing.  This is one reason why I would set the metadata column to a pick from list as opposed to letting our users enter text in this field.  Make sure that you also don’t allow your users to select multiple items.  This doesn’t work, as the center of your URL variable will contain all of the entries, and not just one.  I’m looking into how to fix this so that you can copy an item to multiple libraries depending on what this field contains in order to allow multiple selections, but I don’t have an answer yet.  For now, this will have to be done the old fashioned way, using “contains” as a condition for our workflow.

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: