Immediate Solutions for Everyday Business Problems

EndUserSharePoint.com: Extending Issues and Tasks: Part 4

Original Publication Date: Monday, October 13, 2008
Filed Under: Libraries and Lists, Paul Grenier, Tips and Tricks
SharePoint User Level: Power User

 

After parts 1, 2, and 3,  you built a slick-looking extension of SharePoint’s Tasks and Issues.  If you let the list of Issues get huge, people may complain about the time it takes to find the Issue they want in the dropdown.  Because we understand this interface and what our users will want, we’re taking the next logical step for this solution and offering a feature that the users didn’t ask for.  We’re going to create a way for the Issue to pre-populate on the Task form.

Let’s do the easy one first.  Navigate to your Task list and start a new Task.  Copy the URL without the ‘&Source=’ or anything after that.  Now, paste that into a Notepad file or your HTML editor and chop off the server name.  It should look something like this:

/Lists/Tasks/NewForm.aspx

We’re going to append something called a query string parameter with a value from our parent Issue.  The result will look like this:

/Lists/Tasks/NewForm.aspx?IssueID=1

Our target page, the Task list’s NewForm.aspx page, will read that string and use the information to pre-populate our form.

Next, navigate to your Issue list and display one of your test Issues.  Click Site Actions and choose Edit page.  Click Add a Web Part and add a new Content Editor Web Part.  In the Content Editor’s configuration panel, change the Title to “New Child Task Button.”  Click Source Editor and enter the following code:

<script type="text/javascript">
function NewChild(path,param1){
  var parentID = getID();
  var path = path+'?'+param1+"="+parentID;
  window.open(path);
}

function getID() {
  var qs = location.search.substring(1, location.search.length);
  var args = qs.split("&");
  var vals = new Object();
  for (var i=0; i < args.length; i++) {
    var nameVal = args[i].split("=");
    var temp = unescape(nameVal[1]).split('+');
    nameVal[1] = temp.join(' ');
    vals[nameVal[0]] = nameVal[1];
  }
return vals["ID"];
}
</script>
<INPUT type="button" value="New Child Task"
onclick="NewChild('/Lists/Tasks/NewForm.aspx','IssueID')"/>

You don’t need to edit the script, but if the path to your Task list is different, change the button code it in the last line.  The button calls the NewChild script when it’s clicked passing the path to the Task list’s NewForm.aspx page and the parameter name.

You can test this now.  It should open a new window or tab and look like a new Task except with an added parameter.  Next, from your Task list’s NewForm.aspx page, use the “SharedView” trick to put the page in Edit mode.  Click Add a Web Part and add a new Content Editor Web Part.  In the Content Editor’s configuration panel, change the Title to “Pre-select Issue.”  Click Source Editor and enter the following code:

<script type="text/javascript">
_spBodyOnLoadFunctionNames.push("fillDefaultValues");
function fillDefaultValues() {
  var qs = location.search.substring(1, location.search.length);
  var args = qs.split("&");
  var vals = new Object();
  for (var i=0; i < args.length; i++) {
    var nameVal = args[i].split("=");
    var temp = unescape(nameVal[1]).split('+');
    nameVal[1] = temp.join(' ');
    vals[nameVal[0]] = nameVal[1];
  }
  setLookupFromFieldName("Lookup Issue", vals["IssueID"]);
//field name and query string parameter
  switchback("Lookup Issue"); //field name to lock
}

function setLookupFromFieldName(fieldName, value) {
  var theSelect = getTagFromIdentifierAndTitle("select","Lookup",fieldName);
  if (theSelect == null) {
    var theInput = getTagFromIdentifierAndTitle("input","",fieldName);
    ShowDropdown(theInput.id);

    var opt=document.getElementById(theInput.opt);
    setSelectedOption(opt, value);
    OptLoseFocus(opt);
  } else {
  setSelectedOption(theSelect, value);
  }
}

function setSelectedOption(select, value) {
  var opts = select.options;
  var l = opts.length;
  if (select == null) return;
  for (var i=0; i < l; i++) {
    if (opts[i].value == value) {
      select.selectedIndex = i;
      return true;
    }
  }
  return false;
}

function getTagFromIdentifierAndTitle(tagName, identifier, title) {
  var len = identifier.length;
  var tags = document.getElementsByTagName(tagName);
  for (var i=0; i < tags.length; i++) {
    var tempString = tags[i].id;
    if (tags[i].title == title && (identifier == "" || tempString.indexOf
(identifier) == tempString.length - len)) {
      return tags[i];
    }
  }
  return null;
}

function getDropDownImg () {
  var downArrow=document.getElementsByTagName('img')
  for(var i=0; i < downArrow.length; i++)  {
    if(downArrow[i].alt ==('Display lookup values')) {
      downArrow[i].onclick = fillDefaultValues;
    }
  }
}

function switchback(fieldName){
  var switchSelect = getTagFromIdentifierAndTitle("select","Lookup",fieldName);
  if (switchSelect == null) {
    var switchInput = getTagFromIdentifierAndTitle("input","",fieldName);
    switchInput.onmouseup = fillDefaultValues;
    getDropDownImg();
  } else {
  switchSelect.onchange = fillDefaultValues;
  }
}
</script>

There’s a lot going on here, and the basics have all been documented on various sites, so I won’t bore you.  However, I have a custom addition here that prevents someone from changing the “Lookup Issue” dropdown if it was set in the query string.  If you don’t want that, just delete the line from the first function that starts with “switchback…”  Otherwise, make sure that the field name switchback references matches the one on your page.

If your users want a link from the AllItems.aspx view or a similar page so they can spawn more child Tasks, you don’t need to change anything on the target page, you just need to build the dynamic link again that passes the query string parameter and ID value from the parent.

In that case,  your links need to reflect the ID of each row.  You can either use a calculated column (=”http://myserver/Lists/MyTasks/NewForm.aspx?IssueID=”&ID), a Data View Web Part (SPD solution), or Content Query Web Part (my favorite) with just a little customization to the XSL style sheet.

Good luck, and be creative!

Spread the word...
  • Digg
  • Facebook
  • StumbleUpon
  • Google Bookmarks
  • LinkedIn
  • Reddit

Notify me of comments to this article:


Comments

9 Responses to “EndUserSharePoint.com: Extending Issues and Tasks: Part 4”

  1. Gus on December 10th, 2008 11:24 am

    Hi Paul,

    Excellent functionality sorely missing in SharePoint.

    After a few grueling days of trial and error, I finally managed to get through the entire exercise. I would like suggest a few additions and some modification to your exercise so that others do not stumble as I did.

    Additions:
    1. My setup I have several sub-sites under the main Parent site collection. All of the sub-sites are required have their own Issues and Tasks. So, I implemented your solution on the Top level site (Site Content Types for Issues and Tasks) thinking that it would translate to the lower level sites. And, it worked perfectly, but only at the top level site. Apparently, the Lookup Column established at the top level only point to the Top Level Issues List. The needed functionality, at least for us, is to have the lookup column point to a Issues list relative to the respective site/sub-site. And, once this is accomplished, then (at the top level) show an aggregate view of all the issues/tasks from all the sub-sites. This would allow each site to identify and work on their specific issues and have the top view look over and accumulate metrics on the effectivity of all the sub-site issues.

    2. We use WorkFlows to automatically generate and assign tasks to individuals based on issues. Currently, if we assign a workflow to the Issues List it does not seem to be possible to assign the task automatically to the new functionality. This would help the automation of the entire Issues/Task process.

    3. It would be useful if we could rename the Issues / Task lists. Can you please describe how and where the necessary revisions need to be made in the code (we are Java neophytes) to use different list names in the process.

    Modifications:
    1. As I did not have the publishing feature enabled on my team sites I stumbled across the need to do so only after I had gone down the path to your Part 2 and then had to re-group and delete the functionality and lists until I installed the feature.

    2. The problem with the code cut from the site were insidious. I would suggest changing the mechanism (if possible) where the code is in plain text. As mentioned in Part 1, the ” and ‘ were mi-interpreted and in some cased difficult to find.

    Once Again, Thank You for the very useful tool and tutorial. I personally, learned a great del for this example.

    Gus

  2. AutoSponge on December 10th, 2008 4:29 pm

    @Gus,

    Thank you for your great documentation. I was aware of the problem with code blocks, that’s why we started using a plug-in to handle them this month. I’ll make a note to go back and clean up these older articles.

    To change the list you’re targeting, change the path in the button: onclick=”NewChild(’/Lists/Tasks/NewForm.aspx’,'IssueID’)”

    Assigning the lookup value in the workflow may be possible but it may take formatting the string to update the new list item as (17#;Task)–(I’m doing this off the top of my head right now, could have the syntax wrong).

    Keep up the good work, you’ll be a WSS guru in no time!

    ~Paul

  3. Gus on December 10th, 2008 4:59 pm

    Hi Paul,

    Thanks for your very prompt reply..
    I think that perhaps I made Addition 1 too complicated.. Anyway, the issue is not in the NewChild function. Rather, the problem is with the “IssueLookup” Column used in “…Task” ContentType. As I mentioned, I established this content type at the Top Level site collection. And it works perfectly there.

    However, when I try to use this same content type in sub-sites (with the same name Issues and Tasks lists) the “LookupIssue” drop down in the Tasks List is only populated with the Issues from the top level site only. Apparently, there is some code either in the ItemStyle.xsl or in the functionality of the LookUp function in the columns definitions that does not allow the column lookup to be translated to sub-sites.

    Any help/guidance would be appreciated.

    Thanks Again,

    Gus

  4. AutoSponge on December 10th, 2008 8:32 pm

    @Gus,

    What I’ve done in the past with similar setups is create child Content Types for the sub sites. Then the queries at the top (CQWP) need to check the box for “Include child content types”.

    If each site needs it’s own Issues and Tasks lists, you’ll need Site Columns at that level as well. ‘Sub Site 1 Tasks’ has a Site Column that looks up on ‘Sub Site 1 Issues’.

  5. Gus on December 24th, 2008 11:11 am

    Hi Paul,

    I have been trying, without success, to get this solution to work on other than the top most collection. And, I am stuck on Part 2 of your series.

    I have set up a new site with several sub-sites. And, the solution only works at the top level only.

    After quite a bit of head scratching and debugging I have hit an impasse. I will give some insight of my debugging below:

    I am using your “ItemStyle.xsl” and have been using the “testing” style. I can see all the variables appear to be reported correctly. However, the links do not work from the lower level collections. And, once I apply the “IssueTasks” style no links are available even though there are tasks linked to the issue.

    In order to go a bit further I added a new Style in the ItemStyle.xls that modified the “IssueTasks” style. In this new style I deleted the <div {@ID}… and I added the value reporting statement. This gave me some more information. But, the links still do not work.

    I confirmed that the formation of the link is the same as the working site. And, I also confirmed that the ListId is correct. However, I am not able to find a method to confirm either the WebID or SiteID’s.

    Finally, I tried loading both working and non-working Issues “DispForm.asp” in SPD. And, here I found that the non-working page errors on loading identifying an error in the master page.

    Sorry for the long post. But, I don’t seem to be able to get further without some other input from you.

    Any help would be appreciated. And, Happy Holidays!

    Gus

  6. Gus on December 26th, 2008 1:48 pm

    Gus

    Hi Once Again,

    After a bit less hair on this I have found partial resolution to, at least, the correct link formation to the tasks:
    Apparently, there is a problem with the “ContentQueryMain.xslt” file. The existing file lacks the correct URL resolution to any sub-site. The correction that needs to be made to the above file is the adition of “$SiteURL” to the “$UseCopyUtil” XSL statement, i.e.,

    BEFORE –

    AFTER –

    Editing the above file with the same method as used for the “ItemStyle.xsl” will then correctly resolve the links to the correct sub-sites.

    However, while the links are now functioning (using the Testing template) the “IssueTasks” template still does not display any links on the relevant Issue. If I remove the <Div “{@ID}”… the links are then visible.

    Any Ideas would be appreciated.

    Gus

  7. Gus on December 26th, 2008 3:26 pm

    I finally found the remaining problem… It was an errant quote (”) in the initShowHide CEWP that was not allowing the <Div to be displayed.

    So, thanks anyway.

    Gus

  8. Ben on February 23rd, 2009 11:17 pm

    Paul,

    I keep running into this error when adding the last CEWP within the new task page…

    The dropdown menu will not prepopulate with the issue you navigated from to create a new task – all of the names are exactly as you have created in this article.

    The error I receive is on the new task form page…

    ‘id’ is null or not an object

  9. AutoSponge on February 23rd, 2009 11:31 pm

    @Ben

    One problem we had with these older posts is the code syntax. Wordpress or your browser sometimes changes the quotes to slanted quotes.

    Make sure you have no slanted quotes or slanted double-quotes.

Leave a Reply