SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have the following XML representation for a user:

  <pc:Person xmlns:pc="">
    <pc:DisplayName>John Dodo</pc:DisplayName>

How can I convert this to a SPUser object?

I don't want to parse the XML for this so I guess there is a standard way to do it but could not find how.

(I'm using SharePoint 2010 if that counts)

share|improve this question
up vote 3 down vote accepted

As far as I know you will have to parse the XML. Its very easy to do

        string xml = @"<pc:Person xmlns:pc=""""> 
                        <pc:DisplayName>John Dodo</pc:DisplayName> 

        var ns = "{}";

        var doc = XDocument.Parse(xml);

        var loginName = doc.Descendants(ns + "AccountId").FirstOrDefault().Value;

        using (SPSite site = new SPSite("http://SiteCollectionURL"))
            using (SPWeb web = site.OpenWeb())
                SPUser user = web.AllUsers[loginName];
share|improve this answer
I wanted to avoid this. The XML string is not visible (it comes from a variable) and didn't want to parse it because the code is not very clear on what's working. I would have to add a sample XML in some comments etc. I wanted something like SPUser.LoadFrom(string) but couldn't find a thing. – JohnDoDo Dec 8 '11 at 17:17
I don't think there is anything in the SharePoint API that will do that for you. Your going to have to write code to do it. The SPUser class doesn't have any constructors or static methods that return an SPUser. See the SPUser Class for your options to get SPUser objects. – MLF Dec 8 '11 at 18:46
Couldn't find a way to "load" the XML into an instance so I eventually parsed the XML for the data I was interested in. – JohnDoDo Dec 12 '11 at 14:53

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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