How can I get a SharePoint user profile via the "Friendly Name" using the SP web service?

1.7k views Asked by At

I'm retrieving a list of names from a SharePoint list in my client program. The names are populated from a name picker in SharePoint. In the XML returned from the list query, the list looks like this:

"#10;#Some Name;#12;#Another Name;#15;#Yet AnotherName"

My program needs to get the account name (or the email address) of each user. I can use GetUserProfileByName("domain\\username"), but I don't have the account name. I can't do something like GetUserProfileByName("Some Name") because it has to be the account name.

The number before each name is the index or the ID, but I can't use GetUserProfileByIndex(10) because I have to be managing my own data or have administrator credentials.

So basically it is providing me with two important pieces of information, but I can't retrieve any further information using them.

2

There are 2 answers

0
CMPalmer On

I have a very messy solution working, but I'd love to hear of some ideas for a proper/elegant solution.

Right now I'm using GetListItems on the UserInfo list and creating a dictionary of ows_ImnName and ows_Name entries, then parsing the string from the list query into the names and using them as lookup values.

There has to be a better way.

0
djeeg On

Are you sure the number is an index, i think it might be the userid for the site collection. And it seems to be an odd sort of user list too, but anyway:

string result = "#10;#Some Name;#12;#Another Name;#15;#Yet AnotherName";
string[] users = result.Substring(1).Split(new string[2] { ";", "#" }, StringSplitOptions.RemoveEmptyEntries);
for (int j = 0; j < users.Length; j = j + 2) {
    using (ClientContext context = new ClientContext("http://yoursite")) {
        List list = context.Web.SiteUserInfoList;
        ListItem item = list.GetItemById(int.Parse(users[j]));
        context.Load(item, i => i.Id, i => i["Name"]);
        context.ExecuteQuery();
        object username = item["Name"];
    }
}