2

How to get user by id (server side SPUser.ID, client side PrincipalInfo.UserInfoID) using SharePoint web services?

How it works with SP2007? Do later versions make it easier? How does solution differ between Foundation and Server?

People.asmx can resolve account names (but not SharePoint user id). User information could be queried using web services but it seems to require administrative permissions.

3 Answers 3

3

You can query the User Information list within the site by ID. The code will depend on which version of visual studio you're using. In this case, I used the SoapClient by creating a Service Reference inside Visual Studio 2012.

Here is the code which queries for a user with an ID of 2:

string listName = "UserInfo";
string viewName = string.Empty;
string rowLimit = "1";
string webId = null;
ListsSoapClient client = new ListsSoapClient();
client.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;
client.ClientCredentials.Windows.ClientCredential = System.Net.CredentialCache.DefaultNetworkCredentials;

XElement ndQuery = new XElement("Query");
XElement ndViewFields = new XElement("ViewFields");
XElement ndQueryOptions = new XElement("QueryOptions");

ndQueryOptions.Add(new XElement("IncludeMandatoryColumns", "FALSE"));
ndQueryOptions.Add(new XElement("DateInUtc", "TRUE"));

string query = "<Where><Eq><FieldRef Name='ID'/><Value Type='Number'>2</Value></Eq></Where>";
//string query = "<Where><IsNotNull><FieldRef Name=\"ID\" /></IsNotNull></Where>";
ndQuery.Add(XElement.Parse(query));

XElement result = client.GetListItems(listName, viewName, ndQuery, ndViewFields, rowLimit, ndQueryOptions, webId);

Hopefully this should get you started. Let me know if you need help.

1
  • This seems to work (updated codebase from old Web References to these more recent Service References)
    – thomius
    Nov 9, 2013 at 17:52
1

I would recommend you to download the SPServices.js pack, which automates much of the webservice side of things for you, and has a bunch of extra goodies as well (including a function that does in fact retrieve the current user info for you). You'll also need to have jQuery deployed in order to make it work.

Otherwise, the good news is that the webservices remained basically the same for 2010 so in most cases you should be able to just leave the solution you create in place if your company migrates to that version. Unfortunately 2013 changed all of that around so it'll be necessary to recode when that time comes to pass.

4
  • Client-side JS libraries won't do because we need to add the reference from a server-side .NET solution (for example to _vti_bin/People.asmx).
    – thomius
    Oct 29, 2013 at 9:11
  • Okay, I need more clarification then. On the one hand you're saying that you can't deploy code onto the server. On the other hand you're saying that the code has to be server side. Can you explain your setup in a bit more detail, perhaps? Oct 29, 2013 at 16:15
  • @JohnCraven He is trying to call a web service from a client (example: using a console application).
    – Kit Menke
    Nov 1, 2013 at 20:46
  • I have a .NET MVC application running on a remote server. We have server-side code in this remote location which is discussing with SharePoint using web services - but we cannot deploy code on the destination SharePoint server (so this is why we cannot use SharePoint object model which is only available inside SharePoint servers in their internal memory).
    – thomius
    Nov 3, 2013 at 11:10
0

Check SPContext.Current.Web.CurrentUser it may have what you need.

See https://stackoverflow.com/questions/14510256/how-to-get-current-user-in-sharepoint-site-workflow-2010

3
  • Web services have to be used (code cannot be deployed on the server).
    – thomius
    Oct 29, 2013 at 0:14
  • Are you trying to get the current user id of the person in your .Net application or simply find and return any user in SharePoint?
    – ps_md
    Oct 29, 2013 at 12:37
  • I need to find and return a user where I know the user id without accountname.
    – thomius
    Nov 3, 2013 at 11:08

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.