Adding fields to Profile

Posted by Community Admin on 03-Aug-2018 08:42

Adding fields to Profile

All Replies

Posted by Community Admin on 16-Jan-2011 00:00

Hi,
I am trying to add some additional fields (Company, Position, Country) to user's profile programmatically. I am migrating a website built using Sitefinity 3. 7 to the new released version 4. I made some changes to the Migration Tool to include the fields I needed from the source website but I can't get a valid profile instance.
My method is the following:

private static void UpdateUserInformationFromProfile(MigrationUser userContent, User newUser)
        
            newUser.FirstName = userContent.FirstName;
            newUser.LastName = userContent.LastName;
 
            try
            
                ProfileManager profileManager = ProfileManager.GetManager();
                //Profile profile = profileManager.GetProfiles().Where(prof => prof.User.UserName == newUser.UserName).FirstOrDefault();
                Profile profile = profileManager.CreateProfile(newUser);
 
                if (profile != null)
                
                    profile.SetString("FirstName", userContent.FirstName);
                    profile.SetString("LastName", userContent.LastName);
                    profile.SetString("Company", userContent.Company);
                    profile.SetString("Position", userContent.Position);
                    profile.SetString("Country", userContent.Country);
 
                    profileManager.SaveChanges();
                
            
            catch
        
The line 'Profile profile = profileManager.CreateProfile(newUser);' generates an InvalidOperationException: "Object references between two different object scopes are not allowed. The object 'Telerik.Sitefinity.Model.ManagerInfo' is already managed by 'ObjectScopeImpl 0x1cd6bdb Telerik.OpenAccess.RT.ObjectScope' and was tried to be managed again by 'ObjectScopeImpl 0xa8a0d5 Telerik.OpenAccess.RT.ObjectScope'."
I also tried to get the existing profile (if any) with the commented line, but no records are returned.
By checking at Administration > Settings > Security > Profile Providers, I see OpenAccessProfileProvider. What am I missing? Do I need any additional configuration for Profiles to work properly?
Thanks in advance

Posted by Community Admin on 19-Jan-2011 00:00

Hello Pablo,

Try using the code below

var profileManager = ProfileManager.GetManager();
Telerik.Sitefinity.Security.Model.Profile profileUser = profileManager.CreateProfile();
profileManager.SaveChanges();
var prof = profileManager.GetProfiles().Where(p => p.User.Email == "test@test.com").SingleOrDefault();
Telerik.Sitefinity.Model.DataExtensions.SetValue(prof, "test", "test");
profileManager.SaveChanges();


Greetings,
Ivan Dimitrov
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

Posted by Community Admin on 19-Jan-2011 00:00

Hi

On line

Telerik.Sitefinity.Security.Model.Profile profileUser = profileManager.CreateProfile();
I'm getting error;

No overload for method 'CreateProfile' takes 0 arguments

Thanks

Regards

Posted by Community Admin on 19-Jan-2011 00:00

Hi,

Please take a look at our API reference.

All the best,
Ivan Dimitrov
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

Posted by Community Admin on 19-Jan-2011 00:00

Hi

Sorry, not clear. When I changed

Telerik.Sitefinity.Security.Model.Profile profileUser = profileManager.CreateProfile();

to

Telerik.Sitefinity.Security.Model.Profile profileUser = profileManager.CreateProfile(User);

as per API reference, I get the error;

Object references between two different object scopes are not allowed. The object 'Telerik.Sitefinity.Model.ManagerInfo' is already managed by 'ObjectScopeImpl 0x21e65d7 Telerik.OpenAccess.RT.ObjectScope' and was tried to be managed again by 'ObjectScopeImpl 0x357e6ee Telerik.OpenAccess.RT.ObjectScope'.

Thanks

Regards

Posted by Community Admin on 20-Jan-2011 00:00

Hi Ivan,
As John01 points, there is no method CreateProfile receiving 0 arguments. Adding the user argument is the same I was doing, and John01 gets the same Exception I get. Please notice that in my code snippet the line where it searches for an existing profile is commented.
In general what I need to do is to get a user's existing profile if it exists or to create it if it does not.
Thanks and regards,
Pablo

Posted by Community Admin on 20-Jan-2011 00:00

Hi Pablo,

In the API reference I do not parameterless method.

You should use transaction name parameter of the managers

var userManager = UserManager.GetManager("Default", "test");
System.Web.Security.MembershipCreateStatus status;
var user = userManager.CreateUser("user1", "user111@", "user1@test.com", "Question", "Answer", true, null, out status);
user.FirstName = "FirstName";
user.LastName = "LastName";
 
ProfileManager profileM = new ProfileManager(ProfileManager.GetDefaultProviderName(), "test");
var profile = profileM.GetProfiles();
var prf = profileM.CreateProfile(user);
// other data goes here.
TransactionManager.CommitTransaction("test");


Greetings,
Ivan Dimitrov
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

Posted by Community Admin on 03-Feb-2011 00:00

Hi Ivan,
Still no luck migrating the Profiles. I modified my code and created the User and Profile managers with the same transaction name as follows:

01.const string TransactionName = "transactionName";
02....
03.string username = "someValidUsername";
04.var m_UserManager = UserManager.GetManager(“Default”, TransactionName);
05.var user = m_UserManager.GetUser(username);
06.var profileManager = new ProfileManager(ProfileManager.GetDefaultProviderName(), TransactionName);
07.Profile profile = profileManager.CreateProfile(user);
08.profile.SetString("FirstName", userContent.FirstName);
09.profile.SetString("LastName", userContent.LastName);
10.profile.SetString("Company", userContent.Company);
11.profile.SetString("Position", userContent.Position);
12.profile.SetString("Country", userContent.Country);
13.TransactionManager.CommitTransaction(TransactionName);

Line 08 generates a NullReferenceException (userContent and FirstName property are not null e.g "John") with the following stack trace:
   at Telerik.Sitefinity.Model.DataExtensions.SetString(IDynamicFieldsContainer dataItem, String fieldName, Lstring value, CultureInfo culture)
   at Telerik.Sitefinity.Model.DataExtensions.SetString(IDynamicFieldsContainer dataItem, String fieldName, Lstring value)
   at Migration.Classes.MigrationUsers.UpdateUserInformationFromProfile(MigrationUser userContent, User newUser) in C:\Users\(...)\MigrationUsers.cs:line 146


If after line 07 I insert TransactionManager.CommitTransaction(TransactionName); then a DataStoreException is thrown with Message:
Insert of '564597615-f7de2063-79fb-44b9-bd05-7c8d0111dcbf' failed: System.Data.SqlTypes.SqlTypeException: SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.
   at Telerik.OpenAccess.RT.Adonet2Generic.Impl.PreparedStatementImp.execute()
   at OpenAccessRuntime.Relational.conn.PooledPreparedStatement.execute()
   at OpenAccessRuntime.Relational.RelationalStorageManager.generateInserts(NewObjectOID oid, Int32 index, ClassMetaData cmd, PersistGraph graph, Int32[] fieldNos, CharBuf s, Object[] oidData, IntArray toUpdateIndexes)
INSERT INTO [sf_profile] ([content_id], [avatar], [can_inherit_permissions], [inherits_permissions], [membership_info], [preferred_culture], [user_id], [description_], [description_en], [description_ja], [title_], [title_en], [title_ja], [url_name_], [url_name_en], [url_name_ja], [allow_comments], [allow_track_backs], [app_name], [approve_comments], [content_state], [date_created], [default_page_id], [draft_culture], [email_author], [expiration_date], [last_modified], [last_modified_by], [original_content_id], [ownr], [post_rights], [publication_date], [source_key], [status], [vrsion], [views_count], [visible], [votes_count], [votes_sum], [voa_version]) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
(set event logging to all to see parameter values) System.Data.SqlTypes.SqlTypeException: SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.
   at Telerik.OpenAccess.RT.Adonet2Generic.Impl.PreparedStatementImp.execute()
   at OpenAccessRuntime.Relational.conn.PooledPreparedStatement.execute()
   at OpenAccessRuntime.Relational.RelationalStorageManager.generateInserts(NewObjectOID oid, Int32 index, ClassMetaData cmd, PersistGraph graph, Int32[] fieldNos, CharBuf s, Object[] oidData, IntArray toUpdateIndexes)


Thanks and regards,
Pablo

Posted by Community Admin on 03-Feb-2011 00:00

Hi Pablo,

Please modify the code as shown below.

var userManager = UserManager.GetManager("Default", "test");
System.Web.Security.MembershipCreateStatus status;
var user = userManager.CreateUser("abc", "user111@", "abc@test.com", "Question", "Answer", true, null, out status);
user.FirstName = "FirstName1";
user.LastName = "LastName1";
 
ProfileManager profileM = new ProfileManager(ProfileManager.GetDefaultProviderName(), "test");
var profile = profileM.GetProfiles();
var prf = profileM.CreateProfile(user);
prf.DateCreated = DateTime.Now;
prf.PublicationDate = DateTime.Now;
prf.ExpirationDate = DateTime.Now;
prf.LastModified = DateTime.Now;
TransactionManager.CommitTransaction("test");

We are working on a new implementation for profiles and I believe that it will be finished for Q1.

Best wishes,
Ivan Dimitrov
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

Posted by Community Admin on 04-Feb-2011 00:00

Hi Ivan,
With the code modification now I am able to save the profile and also to get it back. What I am still unable to do is adding custom fields/properties to the profile:

profile.SetString("Company", userContent.Company);
generates the NullReferenceExeption I commented about in my previous post.
profile.SetValue("Company", userContent.Company);
generates a NotSupportedException with Message Cannot "SetValue", because Telerik.Sitefinity.Security.Model.Profile does not contain a public property named "Company".

Best regards,
Pablo

Posted by Community Admin on 04-Feb-2011 00:00

I also ran into this error and Ivan told me to create metatype for Profile using this code (run once):

var metaManager = Telerik.Sitefinity.Data.Metadata.MetadataManager.GetManager();
var type = metaManager.CreateMetaType(typeof(Telerik.Sitefinity.Security.Model.Profile));
metaManager.SaveChanges();

EDIT: sorry, the code above is when you receive error:
Specified type 'Telerik.Sitefinity.Security.Model.Profile' is not a dynamic type.

Posted by Community Admin on 04-Feb-2011 00:00

Hello Jeroen,

This creates metatype for the profile but Pablo need to create the field before trying to save it.

App.WorkWith().DynamicData().Type(typeof(Profile)).Field().CreateNew("Company", typeof(string)).SaveChanges(true);


Regards,
Ivan Dimitrov
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

Posted by Community Admin on 05-Feb-2011 00:00

Hi Jeroen and Ivan,
Thank you very much. Combining both solutions I could make it work.
Best regards,
Pablo

Posted by Community Admin on 23-Feb-2011 00:00

Hi,

I am still getting runtime error on this line:

  App.WorkWith().DynamicData().Type(typeof(Telerik.Sitefinity.Security.Model.Profile)).Field().CreateNew("Position", typeof(string)).SaveChanges(true);

System.InvalidOperationException: Specified type 'Telerik.Sitefinity.Security.Model.Profile' is not a dynamic type

Thanks
Johar Iqbal

Posted by Community Admin on 23-Feb-2011 00:00

Hi,

I am also getting this error:
Object references between two different object scopes are not allowed. The object 'Telerik.Sitefinity.Model.ManagerInfo' is already managed by 'ObjectScopeImpl 0xdd28cd Telerik.OpenAccess.RT.ObjectScope' and was tried to be managed again by 'ObjectScopeImpl 0x36b88df Telerik.OpenAccess.RT.ObjectScope'.

Posted by Community Admin on 23-Feb-2011 00:00

Hello Jeroen,

You need to use TransactionManager.CommitTransaction. If you use the fluent API you should create an instance of App.Prepare and use it.

Regards,
Ivan Dimitrov
the Telerik team

Posted by Community Admin on 07-Mar-2011 00:00

Hi,

How do I remove the email field for user profiles. I do not want to enter an email.

Thanks
Andrei

Posted by Community Admin on 07-Mar-2011 00:00

Hi Andrei,

The e-mail is part of the membership data. The field cannot be removed.UsersList is a complex  IScriptContorol control that wraps two RadWindow controls inside it - edit users and new user. The list view is represented by RadGrid control bound through RadGridBinder. The only way to modify the default behavior is creating a custom control that inherits from UsersList and replace it in the backend pages. This will allow you to remove the filed you want.


Kind regards,
Ivan Dimitrov
the Telerik team

Posted by Community Admin on 07-Mar-2011 00:00

Ivan,

Thanks, but no thanks. I am not going to mess around in that area. Seems a bit too risky for me. 

Can I at least make the field not-required? In our case the system is not linked up to any emailing
services and so it feels a bit silly having to enter a bad email address. 

However, it is not a big problem. If it can't be done then it can't be done.

Many thanks,
Andrei

Posted by Community Admin on 10-Mar-2011 00:00

Hi Andrei,

It is not possible to create a user without an email address. This is required by the provider and there are several places - controls which require this field to be set.

Kind regards,
Ivan Dimitrov
the Telerik team

This thread is closed