Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have a complex application that have several field of type UserMulti. These fields can accept either SPUser or SPGroup or a combination of both kind.

I have in my code the ID and the name of a SPPrincipal object. How can I get the corresponding SPPrincipal object?

I have this code :

    public static SPPrincipal GetPrincipal(SPWeb web, int principalID, string principalName)
    {
        return web.AllUsers.Cast<SPPrincipal>().SingleOrDefault(u => u.ID == principalID && u.Name == principalName) ??
            web.SiteGroups.Cast<SPPrincipal>().SingleOrDefault(g => g.ID == principalID && g.Name == principalName);
    }

But that looks a bit weird to my eyes.

PS: I have ids and names because my value are coming from SPMetal, where fields of type UserMulti are created as one IList<int?> userIds and IList<string> userImnName

[Edit] I just realized that both SPGroup and SPUser are stored in the UserInformationList. So I can rely on Ids, I don't have to test the name.

My code is then slightly simplified :

    public static SPPrincipal GetPrincipal(SPWeb web, int principalID, string principalName)
    {
        return web.AllUsers.Cast<SPPrincipal>().SingleOrDefault(u => u.ID == principalID) ??
            web.SiteGroups.Cast<SPPrincipal>().SingleOrDefault(g => g.ID == principalID);
    }
share|improve this question
add comment

2 Answers 2

I've done this by querying against the web.SiteGroups collection and the web.AllUsers collection, just as you have done, however I didn't cast. I instantiated an SPPrincipal to null and then set it equal to the SPGroup or SPUser before returning it.

Here's an example using iteration rather than Linq:

    private SPPrincipal GetPrincipal(SPSite spsSite, string strName)
    {
        SPPrincipal sppGroup = null;

        // Get user, if user is valid
        if (SPUtility.IsLoginValid(spsSite, strName))
        {
            sppGroup = spsSite.RootWeb.EnsureUser(strName); // <== Here I did not cast
        }
        else // Otherwise look for a group
        {
            foreach (SPGroup spgGroup in spsSite.RootWeb.SiteGroups)
            {
                if (spgGroup.Name.ToUpper(CultureInfo.InvariantCulture) ==
                    strName.ToUpper(CultureInfo.InvariantCulture))
                {
                    sppGroup = spgGroup; // <== and here I did not cast
                                         //  which is what I was talking about
                    break;
                }
            }
        }
        return sppGroup; // <== May be null here
    }
share|improve this answer
    
I'm sorry, but I don't understand your answer. –  Steve B Jul 10 '12 at 7:22
    
The code will probably help. –  RJ Cuthbertson Jul 10 '12 at 12:54
    
More clear now :) –  Steve B Jul 10 '12 at 13:00
add comment
up vote 1 down vote accepted

It looks like there is not simpler solution that this one :

public static SPPrincipal GetPrincipal(SPWeb web, int principalID, string principalName)
{
    return web.AllUsers.Cast<SPPrincipal>().SingleOrDefault(u => u.ID == principalID) ??
        web.SiteGroups.Cast<SPPrincipal>().SingleOrDefault(g => g.ID == principalID);
}

Feel free to suggest another approach if there is one.

share|improve this answer
add comment

Your Answer

 
discard

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.