1

I have a list which stores information about new sharepoint site requests. Where i have 2 fields (people and Groups)

  • Primary Site Collection Admin
  • Secondary Site Collection admin

Both of this fields are pointing to SpSite owners Groups in the site. Now when users are filling up the NewForm.aspx i need to validate that they are unique users.

How do we do that? Code I am using is:

<script language="javascript" src="/sites/services/SiteAssets/scripts/jquery-1.11.1.min.js" type="text/javascript"></script><script type="text/javascript">

function PreSaveAction()
{
var PickerPerson1 = getPickerInputElement("Primary_x0020_Site_x0020_Adminis_41bc75f7-fb89-4d45-897a-372aee8074b6_$ClientPeoplePicker");

alert(PickerPerson1);

}

 function getPickerInputElement(identifier) 
   {   
      var tags = document.getElementsByTagName('DIV');   
      for (var i=0; i < tags.length; i++) 
      {       
       var tempString = tags[i].id;           
       if ((tempString.indexOf('UserField_upLevelDiv') > 0))
    {   
        if(identifier == tempString)
       { 
       var innerSpans = tags[i].getElementsByTagName("SPAN");     
         for(var j=0; j < innerSpans.length; j++) 
          {   
           if(innerSpans[j].id == 'content') 
             {   
              return innerSpans[j].innerHTML;  
             } 
          }   
       }     
       }   
      }   
      return null;   
   } 
</script>

1 Answer 1

1

Use SharePoint JavaScript PreSaveAction() (Example) to validate the data in the two fields. The people picker values can be read by using either Javascript or spjs-utility.js or SPUtility.js

function getPickerInputElement(identifier) {
var tags = document.getElementsByTagName('DIV');
for (var i=0; i &lt; tags.length; i++) {
var tempString = tags[i].id;
//alert('tags[' + i + '].id = ' + tempString);
if ((tempString.indexOf(identifier) &gt; 0) &amp;&amp; (tempString.indexOf('UserField_upLevelDiv') &gt; 0)){
//alert('HIT for ' + identifier + ' id=' + tags[i].id + ' value=' + tags[i].value);
var innerSpans = tags[i].getElementsByTagName("SPAN");
for(var j=0; j &lt; innerSpans.length; j++) {
//alert('innerSpans[' + j + '].id = ' + innerSpans[j].id);
if(innerSpans[j].id == 'content') {
//alert('HIT for ' + identifier + ' id=' + innerSpans[j].id + ' innerHTML=' + innerSpans[j].innerHTML);
return innerSpans[j].innerHTML;
}
}
}
}
return null;
}

Usage

var person = getPickerInputElement("ff18");

See here for information: http://www.balestra.be/2011/11/get-the-value-of-a-sharepoint-people-picker-field-via-javascript.html

Or use spjs-utility.js

ExecuteOrDelayUntilScriptLoaded(function(){
    var myPeoplePickerValue = getFieldValue("MyPeoplePickersFieldInternalName","","; ","loginName");
    alert(myPeoplePickerValue);
},'sp.ribbon.js');

Or SPUtility.js

var peopleField = SPUtility.GetSPField('Assigned To');
5
  • Thank You for your response Nadeem, is there more of a OOTB inbuilt feature to implement that. Commented Nov 21, 2014 at 13:32
  • This could have been done using Calculated Columns but unfortunately SharePoint does not support person or group fields in calculated columns. Commented Nov 21, 2014 at 14:03
  • Nadeem I added the following to my CEWP in the Newform.aspx page but i got null value returned from the function. Any idea of such behavior. If the value is numm how can i compare with another control: <script language="javascript" src="/sites/services/SiteAssets/scripts/jquery-1.11.1.min.js" type="text/javascript"></script><script type="text/javascript"> function PreSaveAction() { var PickerPerson1 = getPickerInputElement("Primary_x0020_Site_x0020_Adminis_41bc75f7-fb89-4d45-897a-372aee8074b6_$ClientPeoplePicker"); alert(PickerPerson1); } </script> Commented Dec 17, 2014 at 16:58
  • Does this javascript work for sp2013 Commented Dec 17, 2014 at 20:03
  • @allegrooctopus, I haven't tested it. Commented Dec 18, 2014 at 5:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.