Problem getting User from User Profile

Posted by Community Admin on 04-Aug-2018 02:24

Problem getting User from User Profile

All Replies

Posted by Community Admin on 29-Jun-2015 00:00

I have a function where I have to search profiles in the system based on last name.  When looking at the API docs here: docs.sitefinity.com/.../for-developers-user-profiles (note I am using 7.3) I see that User is a property of the Sitefinity Profile, yet I can't seem to reference it here:

        public static List<SupervisorListResponse> GetUsersByName(string input)
       
            var result = new List<SupervisorListResponse>();
            try
           
                var manager = UserProfileManager.GetManager();
                var profiles = manager.GetUserProfiles()
                    .OfType<SitefinityProfile>().Where(p => p.LastName.Contains(input));

                if (profiles.Any())
               
                    result = profiles.Select(
                        u => new SupervisorListResponse
                       
                            UserId = u.User.Id,
                            EmailAddress = u.User.Email,
                            LastName = u.LastName,
                            FirstName = u.FirstName
                       
                    ).ToList();
               
                

           
            catch (Exception ex)
           
                ErrorSignal.FromCurrentContext().Raise(ex);
                Log.Error("Error retreiving users with criteria " + input, ex);
           

           
            return result;
       

SupervisorResponse is a simple class - I can provide it if needed.

Now I am capturing the error in Elmah, and that's what has me very confused.  The error states:

Telerik.OpenAccess.OpenAccessException: Field 'User' not found on class 'Telerik.Sitefinity.Security.Model.SitefinityProfile'. at Telerik.OpenAccess.Query.ExpressionCompiler.PerformDatabaseQueryImpl(Type resultType, Int32 elementAt, Object[] groupResolutionParamValues, Boolean single, Boolean checkOid) at Telerik.OpenAccess.Query.ExpressionCompiler.PerformDatabaseQuery(Type type, Int32 elementAt, Object[] groupResolutionParamValues, Boolean single, Boolean checkOid) --- End of inner exception stack trace --- at Telerik.OpenAccess.Query.ExpressionCompiler.PerformDatabaseQuery(Type type, Int32 elementAt, Object[] groupResolutionParamValues, Boolean single, Boolean checkOid) at Telerik.OpenAccess.Query.ExpressionExecution.PerformDatabaseQueryMulti[T](Expression expr, ExecutionSettings settings, Object[] grpVals, Boolean checkOid, QueryOptions options) at Telerik.OpenAccess.Query.Piece`1.ExecuteMultiple() at Telerik.Sitefinity.Data.Linq.LinqQuery`2.GetEnumerator() at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)

And the code up top may not be matching to this error now, I rolled things back to try to get back to the original after a few hours of manipulating.  Any thoughts?

Posted by Community Admin on 09-Jul-2015 00:00

Hi George,

What I can suggest is to call ToList() to the profiles collection:

var profiles = manager.GetUserProfiles()
                .OfType<SitefinityProfile>().Where(p => p.LastName.Contains(input)).ToList();

This should resolve the issue and you will be able to access the User property of the profile and then get the Id and the Email of the User.

Can you please try this and let me know about the result?

Regards,
Sabrie Nedzhip
Telerik
 
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Sitefinity CMS Ideas&Feedback Portal and vote to affect the priority of the items
 

This thread is closed