Wednesday, 14 September 2011

Extract User Name for Workflow Email

We've been searching hard for a solution or workaround to the problem of including a User's name in a Workflow email in SharePoint 2007 (it's a known omission), and were just about to give up when fellow hive member Steve came up with this nifty workaround, by adding some JavaScript to the NewForm.aspx file.

We can take advantage of the fact that SharePoint always knows who you are ...
The idea is to grab the text displayed at the top right of the browser that says, "Welcome John Smith", then render it into a hidden field called Username in the list. You can then call the hidden field in the Workflow email and Robert, as they say, is your mum's brother.

1. So, in SharePoint Designer, open the NewForm.aspx file associated with the list you're attaching the Workflow to.
2. Go to line 66 and replace this code:

<asp:Content ContentPlaceHolderId="PlaceHolderTitleAreaClass" runat="server">
<script id="onetidPageTitleAreaFrameScript">
   document.getElementById("onetidPageTitleAreaFrame").className="ms-areaseparator";
</script>
</asp:Content>

with this:

<asp:Content ContentPlaceHolderId="PlaceHolderTitleAreaClass" runat="server">
<script id="onetidPageTitleAreaFrameScript">
   var userText = document.getElementById('ctl00_m_g_ca8da27f_3ce7_4d29_9adf_e1a96ec0ff6b_ctl00_ctl04_ctl05_ctl00_ctl00_ctl04_ctl00_ctl00_TextField');
   userText.value = document.getElementById("zz6_Menu").innerText.substring(8);
   userText.parentNode.parentNode.parentNode.style.display = "none";
   //document.getElementById("onetidPageTitleAreaFrame").className="ms-areaseparator";
</script>
</asp:Content>

The reference to zz6_Menu is for identifying where on the page the logged-in user's name is appearing (the "Welcome" drop-down menu). But you should double-check this by going View Source on the NewForm.aspx, as I've found the identifier varies depending on your SharePoint set-up.

The getElementById method is identifying the List field where you want to paste the User's Name. You'll have to View Source in order to find out what the ID Number is for the field in your list.

The final line of the new script hides the Username field (used for storing the name) on the Input form. You don't want your end users filling in their name manually.

I found that the original line of script was preventing the new script from working, so I moved it to the end of the sequence and the whole thing started working. So, just to be safe, I commented out the original line of script.

Once this is done, you can add a call in your Workflow Email to pick up the User's name from the Username field you've already added to your Custom List.

For added thoroughness, you should add an amended version of this code to the EditForm.aspx page, so that if the user comes back to the list later to edit their data, they are unable to edit the text of their name. Just open the EditForm.aspx page and alter the code at line 67 to:

<asp:Content ContentPlaceHolderId="PlaceHolderTitleAreaClass" runat="server">
<script id="onetidPageTitleAreaFrameScript">
var userText = document.getElementById('ctl00_m_g_8f8fe63d_e6dc_4f9e_b021_8c653a7bf520_ctl00_ctl04_ctl01_ctl00_ctl00_ctl04_ctl00_ctl00_TextField');
userText.parentNode.parentNode.parentNode.style.display = "none";
//document.getElementById("onetidPageTitleAreaFrame").className="ms-areaseparator";
</script>
</asp:Content>

Note the different value for GetElementById, as this is a different field in a different form.

Hope this helps someone.