I'm writing a custom form with HTML and jQuery so that I can then insert the captured information into a list, but I can't use the SPServices library. When I build the list using InfoPath, it ended up being too slow for my client, so I decided to custom code it. Some of the fields are people, so I need to get the numeric userid of the person that's entered. The "GetUserProfileByName" service does not return the userid and the "ResolvePrincipals" service returns the wrong userid. How can I get the correct userid?

Here's my code:

<script type="text/javascript">
function GetUserInfo(Username, objAccountName, objUserID, objEmail, objErrorPlaceholder) {
    if(Username.length == 0){
        var profileURL = "http://<domain>/_vti_bin/People.asmx";
        var soapEnv = "<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'> \
            <soap:Body> \
                <ResolvePrincipals xmlns='http://schemas.microsoft.com/sharepoint/soap/'> \
                    <principalKeys> \
                        <string>" + Username + "</string> \
                    </principalKeys> \
                    <principalType>User</principalType> \
                    <addToUserInfoList>1</addToUserInfoList> \
            </soap:Body> \
            url: profileURL,
            type: "POST",
            dataType: "xml",
            data: soapEnv,
            complete: function(xData){
                console.log($(xData.responseXML).find('*').html()); // DEBUGGING
                $(xData.responseXML).find("ResolvePrincipalsResponse").each(function() {
                    if($(this).find("IsResolved").text() == 'true'){
                        objErrorPlaceholder.text("This user could not be found, please enter the username or email address.");
            error: function(xData){
                objErrorPlaceholder.text("This user could not be found, please enter the username or email address.");
            contentType: "text/xml; charset=\"utf-8\""

        GetUserInfo($(this).val(), $('input[name=POCAccountName]'), $('input[name=POCID]'), $('input[name=POCEmail]'), $('span[name=POCUsernameError]'));
        // Submit data
        var ListName = 'MyList';
        var SiteUrl = '/sites/MySite/';
        var fieldNames = 'POC,POCEmail'.split(',');
            var fieldValues = new Array($('input[name=POCID]').val(),
        createListItem(ListName, SiteUrl, fieldNames, fieldValues);

/* Modified from: http://msdn.microsoft.com/en-us/library/office/hh185011(v=office.14).aspx */

function createListItem(ListName, SiteUrl, fieldNames, fieldValues) {
    var clientContext = new SP.ClientContext(SiteUrl);
    var oList = clientContext.get_web().get_lists().getByTitle(ListName);

    var itemCreateInfo = new SP.ListItemCreationInformation();
    this.oListItem = oList.addItem(itemCreateInfo);

    for(i=0; i<fieldNames.length; i++){
    oListItem.set_item(fieldNames[i], fieldValues[i]);}


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


<span name="POCUsernameError"></span>
<span class="fieldTitle">POC Username</span>
<input type="text" name="POCUsername" />
<input type="hidden" name="POCAccountName" />
<input type="hidden" name="POCID" />
<span class="fieldTitle">POC Email</span>
<input type="text" name="POCEmail" />
<a href="javascript:void(0);" name="submit">Submit</a>

You can get check the link for getting user info(ID, First Name, Last Name, Email, Username, Name) by User login Name. https://sharepoint.stackexchange.com/a/146788/40602

Use following way to set people or group field in javascript

var user = User_ID + ';#' + User_Name;
listItem.set_item(fieldName, users); 
| improve this answer | |
  • That was very helpful, but itm.ID was returning null, I had to use item.get_item('ID') Thank you ! – user17510 Jun 22 '15 at 13:17
var currentUser;
function init(){
    this.clientContext = new SP.ClientContext.get_current();
    this.oWeb = clientContext.get_web();
    currentUser = this.oWeb.get_currentUser();
    this.clientContext.executeQueryAsync(Function.createDelegate(this,this.onQuerySucceeded), Function.createDelegate(this,this.onQueryFailed));

function onQuerySucceeded() {
  userid = currentUser.get_id();
| improve this answer | |
  • I'm afraid that doesn't work for me, that gets the userid for the person currently on the page, but the user needs to enter someone else into the form, and I need to get the userid for that other person. – user17510 Jun 19 '15 at 20:42

@Yayati had a very helpful response, but I had to use "item.get_item('ID')" instead of item.ID in order to get it working for me. Here's my code:

function getUserInfo(AccountName) {
    var clientContext = new SP.ClientContext.get_current();
    var userInfoList = clientContext.get_web().get_siteUserInfoList();
    var query = new SP.CamlQuery();
    query.set_viewXml("<View><Query><Where><Eq><FieldRef Name='Name' /><Value Type='Text'>" + AccountName + "</Value></Eq></Where></Query><RowLimit>1</RowLimit></View>");
    items = userInfoList.getItems(query);
    clientContext.load(items, 'Include(Department,ID,EMail,Name,UserName,WorkPhone)');
            if(items.get_count() > 0){
                item = items.itemAt(0);
                console.log('User not found');
        }, function(){console.log('Error');});
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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