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) {
objErrorPlaceholder.text("");
if(Username.length == 0){
objEmail.val("");
}
else{
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> \
</ResolvePrincipals>\
</soap:Body> \
</soap:Envelope>";
$.ajax({
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'){
objUserID.val($(this).find("UserInfoID").text());
objAccountName.val($(this).find("AccountName").text());
objEmail.val($(this).find("Email").text());
}
else{
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\""
});
}
}
$(document).ready(function(){
$('input[name=POCUsername]').change(function(){
GetUserInfo($(this).val(), $('input[name=POCAccountName]'), $('input[name=POCID]'), $('input[name=POCEmail]'), $('span[name=POCUsernameError]'));
});
$('a[name=submit]').click(function(){
// Submit data
var ListName = 'MyList';
var SiteUrl = '/sites/MySite/';
var fieldNames = 'POC,POCEmail'.split(',');
var fieldValues = new Array($('input[name=POCID]').val(),
$('input[name=POCEmail]').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]);}
oListItem.update();
clientContext.load(oListItem);
clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}
</script>
<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>