0

I have the following XML representation for a user:

  <pc:Person xmlns:pc="http://schemas.microsoft.com/office/infopath/2007/PartnerControls">
    <pc:DisplayName>John Dodo</pc:DisplayName>
    <pc:AccountId>DOMAIN\john.dodo</pc:AccountId>
    <pc:AccountType>User</pc:AccountType>
  </pc:Person>

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)

1 Answer 1

3

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

        string xml = @"<pc:Person xmlns:pc=""http://schemas.microsoft.com/office/infopath/2007/PartnerControls""> 
                        <pc:DisplayName>John Dodo</pc:DisplayName> 
                        <pc:AccountId>DOMAIN\john.dodo</pc:AccountId> 
                        <pc:AccountType>User</pc:AccountType> 
                       </pc:Person>";

        var ns = "{http://schemas.microsoft.com/office/infopath/2007/PartnerControls}";

        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];
            }
        }
3
  • 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
    Commented Dec 8, 2011 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
    Commented Dec 8, 2011 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
    Commented Dec 12, 2011 at 14:53

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.