Introduction

This blog is mainly focused on SharePoint
I will use this to store and share all the tips and tricks about SharePoint and the related products
List of all posts

SharePoint Retrieving UserProfiles (Claims authentication)


Topic Retrieving UserProfiles
OutputUserProfile

Desc : We need to get info in the UserProfile It seeems that the information is available to every logged user, but when you're in claim authentication mode You get an error so we have to use an elevated account
Step Description
1 We will create a function to get the Admin Token
        public static SPUserToken GetMainSiteAdminToken(SPWeb web)
        {
            if (web == null) throw new ArgumentNullException("web");
            SPUserToken userToken = null;
            SPSecurity.RunWithElevatedPrivileges(() =>
            {
                using (SPSite elevatedSite = new SPSite(web.Site.ID))
                {
                    using (SPWeb elevatedWeb = elevatedSite.OpenWeb(web.ID))
                    {
                            userToken = elevatedWeb.SiteAdministrators[0].UserToken;
                    }
                }
            });

            return userToken;
        }
2 Now that we have the SiteAdmnToken, we can get the user Profile
               public UserProfile GetUserProfile(SPWeb web, string UserLogin)
        {
   if (web == null) throw new ArgumentNullException("web");
            if (UserLogin == null) throw new ArgumentNullException("UserLogin");
            if (string.IsNullOrWhiteSpace(UserLogin)) throw new ArgumentException("argument cannot be an empty string", "UserLogin");
            UserProfile profile = null;
            using (SPSite elevatedSite = new SPSite(web.Site.ID, GetMainSiteAdminToken(web)))
                {
                    using (SPWeb elevatedWeb = elevatedSite.OpenWeb(web.ID))
                    {
                        bool bUnsafe = elevatedWeb.AllowUnsafeUpdates;

                        try
                        {
                            elevatedWeb.AllowUnsafeUpdates = true;
                            SPServiceContext serviceContext = SPServiceContext.GetContext(elevatedSite);
                            UserProfileManager upm = new UserProfileManager(serviceContext);
                            SPUser user = elevatedWeb.EnsureUser(UserLogin);
                            if (user != null)
                            {
                                try
                                {
                                    profile = upm.GetUserProfile(user.LoginName);
                                }
                                catch (Exception ex) { }
                             }
                        }
                        finally
                        {
                            elevatedWeb.AllowUnsafeUpdates = bUnsafe;
                        }
                    }
                }
             return profile;
        }
Made with Serge Lespagnard

No comments:

Post a Comment

by Category