Fetch SharePoint User ID of user

Jun 13, 2010 at 9:32 PM
Edited Jun 13, 2010 at 9:41 PM

Hi,

Do you have a function that can get the SharePoint ID of a user by providing the username.

If you go to /_layouts/userdisp.aspx?Force=True and hover your mouse over the "Edit Item" link, you can see that the URL is in the form: /_layouts/useredit.aspx?ID=XX , which tells you the ID of the user. Is it possible to obtain that value with SPServices?

Because what I actually need to do is update a 'User/Group' field, and http://pholpar.wordpress.com/2010/01/26/updating-sharepoint-lookup-and-user-field-values/ and http://pholpar.wordpress.com/2010/01/26/updating-multi-value-fields-using-web-service-call-and-batch-update/ explain that it needs to be done with the IDs.

Thanks in advance,

Khaled

Coordinator
Jun 14, 2010 at 5:05 AM

Khaled:

Actually, it depends on what you are trying to do. For instance, if you are trying to populate a People Picker on a form, you'd want to use the user's name rather than the ID.

You could certainly adapt the SPGetCurrentUser function to parse out the ID if you need it quickly. If you do so, please post your solution back here so that I can incorporate it for others to use!  However, I can also try to add this into the next release of SPServices. Since I've just done a release this weekend, I don't have a timetable for the next release yet, though.

M.

Jun 14, 2010 at 8:05 PM
Edited Jun 14, 2010 at 8:11 PM

Hi,

Thanks for your reply.

Here is the finished code, I made use of the GetUserInfo function. What the code below does it gets the User ID for each user specified in the array (the \\ is not a mistake and must be present after the domain name), then adds the separation ';#' and the Display name of the user. So you would end up with a string like this: 123;#Sample User;#234;#Demo User;#345;#Another User

You can use this in the UpdateListItems function to put these users into the Users field.

Example:

var users_array = new Array('DOMAIN\\sampleuser', 'DOMAIN\\demouser', 'DOMAIN\\anotheruser');
var users = '';
for(i=0; i<users_array.length; i++) { 
alert(users_array[i]); 
$().SPServices({
operation: "GetUserInfo",
async: false,
userLoginName: users_array[i],
completefunc: function(xData, Status) {
alert("Status=" + Status + " XML=" + xData.responseXML.xml); $(xData.responseXML).find("[nodeName=User]").each(function() {
users += $(this).attr("ID") + ";#" + $(this).attr("Name");
if(i != (users_array.length-1)) {
users += ";#";
}
});
}
});
}
alert(users); //Outputs '123;#Sample User;#234;#Demo User;#345;#Another User' (without quotes)

 

$().SPServices({
operation: "UpdateListItems",
async: false,
listName: "List",
batchCmd: "Update",
valuepairs: [["Users", users]],
ID: 123,
completefunc: function(xData, Status) {
 //alert("Status=" + Status + " XML=" + xData.responseXML.xml);
}
});

Khaled

Coordinator
Jun 20, 2010 at 7:32 PM

Khaled:

Nice solution. Sorry that GetUserInfo didn't occur to me in my first reply.

M.

Jun 22, 2010 at 10:04 AM

At some point of time there might be a requirement to access a SharePoint site programmatically and list the names of all the groups a user is associated to without using SharePoint APIs. Here is a small piece of code in C# which demonstrates how to achieve the above action using web service(s) available for a SharePoint site. First you need to add a reference of the UserGroup web service to your solution.

try {      UserGroup.UserGroup userGrp = new

TestApplication.UserGroup.UserGroup();      

userGrp.Url = "http://SPSite/_vti_bin/UserGroup.asmx";    

 userGrp.PreAuthenticate = false;      

userGrp.UseDefaultCredentials = true;

 

Hope you find this tips useful.

 

Thanks,

Bijayani

Mindfire Solutions

SharePoint Migration