Adding fields to Profile
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
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();
Hi
On line
I'm getting error;Telerik.Sitefinity.Security.Model.
Profile profileUser = profileManager.CreateProfile();
Hi,
Please take a look at our API reference.
All the best,
Ivan Dimitrov
the Telerik team
Hi
Sorry, not clear. When I changed
Telerik.Sitefinity.Security.Model.Profile profileUser =
profileManager.CreateProfile();
Telerik.Sitefinity.Security.Model.Profile profileUser =
profileManager.CreateProfile(User);
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
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
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"
);
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);
TransactionManager.CommitTransaction(TransactionName);
then a DataStoreException is thrown with Message: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"
);
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);
profile.SetValue(
"Company"
, userContent.Company);
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();
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);
Hi Jeroen and Ivan,
Thank you very much. Combining both solutions I could make it work.
Best regards,
Pablo
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
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'.
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
Hi,
How do I remove the email field for user profiles. I do not want to enter an email.
Thanks
Andrei
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
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
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