Detect Choice Value on Form

If you want to do anything clever with a Document Library form it can be harder than you'd like. There was a requirement recently to disable changes to a Library item if the status of the item (a Choice field) was a certain value.

To do this I had to detect the specific select Choice field value and then act on it. The HTML behind SharePoint Choice Fields is pretty unhelpful - it's difficult to address exactly what you want. Faced with a few options I chose to address on field type and field title;

<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.0.js" type="text/javascript"></script>

<script type="text/javascript"> 

$(document).ready( function() {

var statusValue = $("select[title='My Item Status']").find(":selected").text();

if (statusValue === "Completed")

{

$("input[type=button]").attr("disabled", "disabled");

$("input[value='Cancel']").removeAttr("disabled");

}

})</script>

 

So the above is detecting when the status (Choice field) has been set to Complete. It then disables all the form buttons to prevent the saving of any edits. So as not to panic the user, the final line re-enables the Cancel button so the user can close the form gracefully.

The above solution is easily circumvented by using Datasheet view or an external tool like Harmon.ie to edit the document properties. It should not be used when it's totally essential that the item not be edited. It's more of a gentle nudge that will work for 99% of users.

To easily place the above on a form, you could edit the page that the edit form resides on, add a Content Editor WP, and place a link to the script there. (This Post shows this method for InfoPath forms, but will work in the same way for this too).

 

Happy SharePointing

 

Disclaimer: The software, source code and guidance on this website is provided "AS IS"
with no warranties of any kind. The entire risk arising out of the use or
performance of the software and source code is with you.

Any views expressed in this blog are those of the individual and may not necessarily reflect the views of any organization the individual may be affiliated with.