SharePoint User Profile

As noted in the UserProfile class documention on MSDN, it is necessary to validate that the Profile[PropertyName] exists, and that the Profile[PropertyName].Value exists, before actually retrieving the value.

References:
- UserProfile class - http://msdn.microsoft.com/en-us/library/microsoft.office.server.userprofiles.userprofile.aspx
- ported to PowerShell : http://www.sbrickey.com/Tech/Code/SharePoint_User_Profile_PowerShell_
Language:
C#
Keywords:
SharePoint
Code Snippet

string url = "http://ServerName";
using (Microsoft.SharePoint.SPSite tSite = new Microsoft.SharePoint.SPSite tSite(url))
{
    Microsoft.Office.Server.
ServerContext sc = Microsoft.Office.Server.ServerContext.GetContext(tSite);
    Microsoft.Office.Server.UserProfiles.
UserProfileManager upm = new Microsoft.Office.Server.UserProfiles.UserProfileManager(sc);

    string
accountName = "domain\\accountName";
    if (upm.UserExists(accountName))
    {
        Microsoft.Office.Server.UserProfiles.
UserProfile up = upm.GetUserProfile(accountName);

        // to grab a single property value
        if (up["WorkEmail"] != null &&
            up[
"WorkEmail"].Value != null)
            System.Diagnostics.
Debug.WriteLine("email: " + up["WorkEmail"].ToString());

        // to iterate all properties/values
        foreach (System.Collections.Generic.KeyValuePair<System.String, Microsoft.Office.Server.UserProfiles.UserProfileValueCollection> item in up)
        {
            System.Diagnostics.
Debug.WriteLine("s: " + item.Key);
            if (item.Value.Count == 1)
                System.Diagnostics.
Debug.WriteLine("v: " + item.Value[0]);
        }
    }
}

 


Created 2012-01-27
comments powered by Disqus
Login