Blog site of founder, RealActivty – Paul J. Swider

A keystone to social computing using the Microsoft stack

The user profile database in SharePoint is a keystone to social computing using the Microsoft stack. We store information about users in our organization, communities or members of external sites in the user profile database.

Keystone to social using MS

Earlier today I was asked for sample code to access the user store. I often use the code below in presentations to demonstrate how we programmatically access information about users in the profile store. If we need to access user profiles from external applications we should use the SharePoint web services.

namespace UserProfilesSample 

{ 

    class Program 

    { 

        static void Main(string[] args) 

        { 

 

            using (SPSite site = new SPSite("http://SharePoint2007")) 

            { 

                ServerContext context = 

                    ServerContext.GetContext(site); 

                UserProfileManager profileManager = new UserProfileManager(context); 

 

                string userName = @"MyDomain\Administrator"; 

                Console.WriteLine("All Profiles:"); 

                foreach (UserProfile p in profileManager) 

                { 

                    Console.WriteLine("{0} : {1}", p.ID, p.MultiloginAccounts[0]); 

                } 

                Console.WriteLine("--"); 

                if (profileManager.UserExists(userName)) 

                { 

                    UserProfile profile = profileManager.GetUserProfile(userName);                 

                    Console.WriteLine("Found User Profile {0}", profile.ID); 

                    Console.WriteLine("\tPersonal Site: {0} ({1})", 

                        profile.PersonalSite.RootWeb.Title, 

                        profile.PersonalSite.Url);                  

                } 

                else 

                { 

                    Console.WriteLine("No account found for " + userName); 

                } 

 

                Console.ReadLine(); 

 

            } 

        } 

 

    } 

}

twitter-bird-2

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: