3/22/2009

SharePoint: Locking a Completed Task

 

Yet another JavaScript and CEWP trick!

 

Goal: Prevent a completed task from being edited.

Tools:

  • Content Editor Web Part (CEWP)
  • JavaScript that:
    • Finds table cells with the word “Completed”
    • Finds the parent row and then the second cell in the row
    • Replaces that cells contents with the name of the task and a hyperlink to a message

Steps:

  • Display the task list view to modify (each view will need its own web part and JavaScript)
  • Add a Content Editor Web Part:
    • Go to Site Actions, Edit Page
    • Add a Content Editor Web Part and move it below the task web part
    • Click in the web part, click Edit, Modify Shared Web Part, and in the Appearance section change "Chrome" to "None".
  • Add the JavaScript:
    • Click the Source Editor button
    • Type or paste the JavaScript (example below)
    • Note that this example assumes that the cell with the link and dropdown list is the second cell in the row (cell 1).   i.e. “childNodes[1]”

The JavaScript:

<script type="text/javascript" language="javascript">

  // for info: http://techtrainingnotes.blogspot.com
  var x = document.getElementsByTagName("TD") // find all of the TDs
  var i=0;
  for (i=0;i<x.length;i++)
  {

    if (x[i].className=="ms-vb2") //find the TDs styled for lists
    {

      if (x[i].innerHTML=="Completed")
      {
        var theTargetNode = x[i].parentNode.childNodes[1]

       // The following is all one line:
        theTargetNode.innerHTML = "<a class='ms-vb2' href='javascript:TaskHasBeenCompleted()'>" + theTargetNode.getElementsByTagName("TD")[0].childNodes[0].innerHTML + "</a>"
      }
    }

  }

function TaskHasBeenCompleted()
{
  alert('This task has been completed and cannot be edited.')
}

</script>

 

Watch-outs:

  • Remember to add this to your rebuild / disaster recovery plan documentation!
  • Users will be able to get to the task and edit it through a direct URL to the task edit page or through any other views you have not modified.

.

No comments:

Note to spammers!

Spammers, don't waste your time... all posts are moderated. If your comment includes unrelated links, is advertising, or just pure spam, it will never be seen.