Adding fields to user profiles types in code
Hello,
I am starting to write a configuration program to configure a Sitefinity solution that is about to be deployed.
I am looking for information on how to add fields to the basic Sitefinity profile type through code. I need to add 6 new fields to the "Telerik.Sitefinity.Security.Model.SitefinityProfile" type only using code. Is there any way to accomplish this? Are there any code examples out there that manage profile type fields?
Thanks,
Clinton
Hi Clinton,
The way you can create user profiles is described in detail in our documentation. Please refer to the link for further clarification.
Regards,
Ivan D. Dimitrov
Telerik
Hello Ivan,
Thanks for the link, but there is no mention of adding new fields to existing user profiles. Is there any way to do this?
Thanks,
Clinton
Hello Clinton,
We do not have any API to achieve this. You will need to create your profiles from the UI.
Regards,
Ivan D. Dimitrov
Telerik
Hello Ivan,
I have figured it out, seems adding fields was only possible using the Fluent API, here is the code in case others want to do something similar:
var transaction = App.Prepare().SetTransactionName(
"CreateAttribute"
);
transaction.WorkWith().DynamicData().Type(
typeof
(SitefinityProfile)).Field()
.CreateNew(
"test"
,
typeof
(
string
)).Do(field =>
field.MetaAttributes.Add(
new
MetaFieldAttribute(Guid.NewGuid())
Name =
"UserFriendlyDataType"
,
Value =
"ShortText"
);
field.MetaAttributes.Add(
new
MetaFieldAttribute(Guid.NewGuid())
Name =
"IsCommonProperty"
,
Value =
"true"
,
);
field.DBType =
"VARCHAR"
;
field.DBLength =
"255"
;
).SaveChanges(
true
);
TransactionManager.CommitTransaction(
"CreateAttribute"
);
Regards,
Clinton
Old thread, but I wanted to add the following to the conversation.
When doing this, if I did not map the ColumnName property, then when I tried to retrieve the field it through a database error. My complex name (TestThisObjectId) mapped to Test_this_object_Id in the database and it had trouble resolving.
field.ColumnName =
"MatchToFieldName"
;
Also, a code example for querying against the user profiles that worked for me. Trying to query against GetProfiles<>() with a standard LINQ FirstOrDefault/Where caused a database exception, so hopefully this technique helps.
var profileManager = UserProfilesHelper.GetUserProfileManager<SitefinityProfile>();
var profiles = SitefinityQuery.Get<UserProfile>(
typeof
(SitefinityProfile), profileManager.Provider);
var apps = profiles.Where(p => p.ApplicationName == profileManager.Provider.ApplicationName);
var profile = apps.FirstOrDefault(x => x.GetValue<
string
>(
"ObjectNameHere"
)).
Equals(
"TestValue"
));