in

SharePoint Blogs

The Best Place for SharePoint-related Blogs

Another Hack's SharePoint Experiences

I've been playing with SharePoint for a few years now. Every now and again I'll post something that I found interesting about SharePoint or computing in general.

Alright! Got the listeners to work!

Addendum: People have been asking me why I wanted to do this. I explained it in a previous blog (I think) but here's the short version:

I have an Issues List for internal system operations.  If a phone goes down, or a computer dies, a user could enter a ticket into the system. However, most of our users are ignorant as to who would fix each type of issue.  So I have two drop down boxes: a Category and Assigned to.  When you select a certain category, the assigned to box automatically changes to the person who handles that type of issue. The “listener“ therefore is used to listen to the onChange event for the category.

------------------------------------------------------------------------------

W00t!  I got the listeners to work.  Here is the JavaScript, and then I'll explain it below:

window.onload = startup;
function startup()
{
 setAssignment();
 document.forms[0].elements[getElementIndex("OWS:Category")].onchange = setAssignment;
}

function setAssignment()
{
 var getCategoryTagIndex, getAssignToTagIndex, getCategoryText;
 getAssignedToTagIndex = getElementIndex("AssignedTo");
 getCategoryTagIndex = getElementIndex("OWS:Category");
 getCategoryText = getSelectedElement(document.forms[0].elements[getCategoryTagIndex]);
 setAssignedTo(document.forms[0].elements[getCategoryTagIndex], document.forms[0].elements[getAssignedToTagIndex]); 
}

function setAssignedTo(CategoryObj,AssignedToObj)
{
 switch(CategoryObj.options[CategoryObj.selectedIndex].text)
 {
  case "LoopNet.com website error":
   searchAssignedTo("[User Name]", AssignedToObj);
   break;
  case "InsideLoop website error/suggestion":
   searchAssignedTo("[User Name", AssignedToObj);
   break;
  case "E-mail":
   searchAssignedTo("[User Name]", AssignedToObj);
   break;
  case "Network file access":
   searchAssignedTo("[User Name]", AssignedToObj);
   break;
  case "Phone problem":
   searchAssignedTo("[User Name]", AssignedToObj);
   break;
  case "Local workstation problem":
   searchAssignedTo("[User Name]", AssignedToObj);
   break;
  case "Other":
   searchAssignedTo("[User Name]", AssignedToObj);
   break;
  default:
   searchAssignedTo("[User Name]", AssignedToObj);
   break;
 }
}
function searchAssignedTo(userName, AssignedToObj)
{
 for(i = 0; i < AssignedToObj.options.length; i++)
 {
  if (userName.toLowerCase() == AssignedToObj.optionsIdea.text.toLowerCase())
  {
   AssignedToObj.optionsIdea.selected = true;
   return;
  }
 }
}
function getSelectedElement(selectObj)
{
 return selectObj.options[selectObj.selectedIndex].text;
}
function getElementIndex(elementName)
{
 var e;
 e = -1;
 for (i = 1; i
 {
  var temp;
  temp = document.forms[0].elementsIdea.name;
  if(temp.indexOf(elementName)>= 0)
   {
   e = i;
   break;
   }
 }
 return e; 
}


//-->

I hope this shows up, since it's javascript. Basically I put this at the end of my issues form, right after the end table tag. This is so that the page loads before the script runs, allowing time for the web part to load.  The first thing I do is set the window.onload event handler to be my startup function.  That way the values are set on every page load.  Within that I run two other commands.  First, I run the setAssignment function, to do the initial setting of the assigned task, just in case the user doesn't change it.  Then I set the onchange event for my Category to be the setAssignment again.  You'll notice a couple of things.  First of all, we aren't sure of the names of the form or the drop down list.  So instead of using the name, we use the forms[0] element and then search for the element index for the category drop down list.  So, here's a description of the functions:

startup: is the window.onload event handler
setAssignment: the event handler for the Category select drop down box. It calls the functions to find the indexes of the Assigned To box and the Category box, then passes those objects to the actual function that changes the assignment.
setAssignedTo: the function that actually changes the assignment. It takes two parameters: the assigned to box object and the category box object. It reads the category, then loops through a switch. The switch reads the category, and then sets the proper assignment. If none of the options are found, then it sets the assignment to the administrator (me).
searchAssignedTo: basically just a loop that checks the the name given to the name in the assignment box. If they are the same, it sets that selectedIndex to true. It could be included in the setAssignedTo function, but this saves having to code it a grip of times
getSelectedElement: gets the name of the element passed to it. I had it here for debugging purposes; it can be removed.
getElementIndex: gets the index of the element that matches the string you pass into it. Be careful here. You want ot make sure that you pass the right name in. I thought I could search for an element with the word Category in it, but you can't. You need to look for OWS:Category. So watch yourself. You can find these names by opening the form and then browsing through the source.

So that's basically it. Let me know if you have any questions on this, and I'll be happy to explain. It took a team effort here to come up with the right script. Hopefully this will save you some time.

Comments

No Comments

Leave a Comment

(required )  
(optional )
(required )  
Add

Need SharePoint Training? Attend a SharePoint Bootcamp!

Posts (c) their respective authors. Everything else (c) 2007 SharePoint Experts