Need a little help with some javascript in Sharepoint column values

Seven0fNine
Seven0fNine used Ask the Experts™
on
Hi, I am having a little trouble with some javascript to extract a user name from a Sharepoint column, verify that the user exists in a valid sharepoint group and then if true, to populate another field with 'Yes'... I am using the following Sharepoint columns in a New form:

User name                 William Smith
IT Support Group      Yes

What I would like to achieve is when someone enters a name in the 'User name' column, javascript to fire off to see if this user is in the following Sharepoint group 'IT Support'. If the user is part of this group then for 'Yes' to be automatically populate the column 'IT Support Group'.

I can validate the group for the 'logged in user'. However, I need help with the following:

1) fire off the javascript when a username has been entered in the User Name field and extract its value?
2) return 'yes' to the IT Support column (if user exists in IT Support group).

Below is the code I am currently using in Script Editor:

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js" type="text/javascript"></script>
     <script language="javascript" type="text/javascript">
      $(document).ready(function() {
         var x = GetDept();
              alert (x);
                  $("input[Title='IT Support Group']").val(x);
});
     

function GetDept() {
	var url = _spPageContextInfo.webAbsoluteUrl +'/_api/web/currentuser/groups'
		$.getJSON(url, function (data) {
			$.each(data.value, function (key, value) {
				  if (value.Title == 'IT Support') {               
                                return 'Yes';             
                                  }
					});
			});
	}
 
 function getQueryString(key){
         var regex=new RegExp('[\\?&amp;]'+key+'=([^&amp;#]*)');
         var qs=regex.exec(window.location.href);
         return qs[1];
       }

</script>

Open in new window


Thank you in advance for any help!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Zvonko זְאֵבSystems architect
Top Expert 2006

Commented:
My advice is not to do that SharePoint Person and Group queries in JavaScript.
Can you do some calculated column SharePoint view and read that SharePoint list?

If you still want to do it in JavaScript then do it this way: read all memebers from your Group and lookup the searched User name.
The code for collecting all user names from a given Group ID is here:
https://docs.microsoft.com/en-us/previous-versions/office/developer/sharepoint-2010/hh185012(v%3Doffice.14)
var siteUrl = '/sites/MySiteCollection ';

function retrieveAllUsersInGroup() {

    var clientContext = new SP.ClientContext(siteUrl);
    var collGroup = clientContext.get_web().get_siteGroups();
    var oGroup = collGroup.getById(7);
    this.collUser = oGroup.get_users();
    clientContext.load(collUser);


    clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}

function onQuerySucceeded() {

    var userInfo = '';

    var userEnumerator = collUser.getEnumerator();
    while (userEnumerator.moveNext()) {
        var oUser = userEnumerator.get_current();
        this.userInfo += '\nUser: ' + oUser.get_title() + 
            '\nID: ' + oUser.get_id() + 
            '\nEmail: ' + oUser.get_email() + 
            '\nLogin Name: ' + oUser.get_loginName();
    }
      
    alert(userInfo);
}

function onQueryFailed(sender, args) {

    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial