christopheraucq

Validation of a Person or Group field in a SharePoint form

Blog Post created by christopheraucq Champion on Mar 5, 2015

Why this article?

Because the Related Item column is not yet available in the Nintex Form in O365.

 

This article is a follow-up to Delegation in O365

In my workflow, I really want to display the link to the document that needs approval in the task.
So, I decide to validate the Delegated action by checking if DelegatedTo is empty of not in the SharePoint Form.


In SharePoint 2013, the EditForm.aspx contains a Javascript validation function called PreSaveItem().
The trick here is to use this name to execute your own code. Note that the PreSaveItem() function of SharePoint will still run.

 

I need to validate only if the Content Type is Workflow Approve. But this field is not displayed if there is only one Content Type.

That is why I am also testing the empty value in the following line
if (strContentType == "Workflow Approve" || strContentType === '')

 

In my case, I have choosed to use the Person or Group column type.
In SharePoint, you cannot test a Person or Group column like a Single text column.
With the help of jQuery, I get an handle to the DelegatedTo field.
I make sure the name is resolved. If this is the case, I get the User object and get the Key property.

var ppDiv = $("[id$='ClientPeoplePicker'][title='" + strControlDelegatedTo + "']");

var spPP = SPClientPeoplePicker.SPClientPeoplePickerDict[ppDiv[0].id];

spPP.AddUnresolvedUserFromEditor(true);

var myUser = '';

if (!spPP.HasInputError) {

   var userKeys = spPP.GetAllUserInfo();

   myUser = userKeys[0];

}

if (myUser != undefined) {

   strDelegatedTo = myUser.Key;

}


If it is not empty, the field can pass the validation, otherwise, I display the validation message and I reset the Task Outcome value to avoid that the message is displayed if the user click Save afterwards.

 

Let's add some Javascript in the SharePoint EditForm.aspx. To accomplish that, open your Workflow Tasks list.

In The List Ribbon, click Form Web Parts - Default Edit Form.
Click Add a Web Part: Media and content - Script Editor and click Add.
Click Edit Snippet and paste this code:

 

<script language="javascript" type="text/javascript"
src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script language="javascript" type="text/javascript">
$(document).ready(function() {
});
function PreSaveItem() {
    //Only for Workflow Approve Content Type
    var strContentType = $("select[title='Content Type'] option:selected").text();
    if (strContentType == "Workflow Approve" || strContentType === '') {
        var strTaskOutcome = $("select[title='Task Outcome'] option:selected").text();
        if (strTaskOutcome != "Delegated") {
            return true;
        }
        var strDelegatedTo = "";
        var strControlDelegatedTo = "DelegatedTo";
        var ppDiv = $("[id$='ClientPeoplePicker'][title='" + strControlDelegatedTo + "']");
        var spPP = SPClientPeoplePicker.SPClientPeoplePickerDict[ppDiv[0].id];
        spPP.AddUnresolvedUserFromEditor(true);
        var myUser = '';
        if (!spPP.HasInputError) {
            var userKeys = spPP.GetAllUserInfo();
            myUser = userKeys[0];
        }
        if (myUser != undefined) {
            strDelegatedTo = myUser.Key;
        }
        if (strDelegatedTo != "") {
            return true;
        } else {
            alert('Please select the delegated person.');
            $("input[title='DelegatedTo']").focus();
            $("select[title='Task Outcome']").val("");
            return false;
        }
        return false;
    }
    return true;
}
</script>

 

Hope this helps

Christophe Raucq

Outcomes