Custom Profile Type
Hi All,
How can I programmatically add a new custom Profile Type to Users?
Thanks!Hi Nathalia,
Adding a profile type programmatically is possible, but is not a simple operation, due to all the properties needed for the profile type fields. Also, it is not a common operation which is performed often. This is why you need to be careful if exposing it to frontend users, because you may end up with many profile types, and higher possibility of wrong or omitted properties.
I am sending you one code snippet which creates a new profile type and adds two fields (short text and choice field) to it.
Guid profileMetaTypeId = Guid.NewGuid();
var profileTypeDescription =
new
MetaTypeDescription(
"//"
, Guid.NewGuid())
UserFriendlyName =
"Test Profile"
;
MetaType profileMetatype =
new
MetaType(
"//"
, profileMetaTypeId)
ClassName =
"testprofile"
,
BaseClassName =
"Telerik.Sitefinity.Security.Model.UserProfile"
,
Namespace =
"Telerik.Sitefinity.Security.Model"
;
var profileData =
new
UserProfileTypeViewModel(profileTypeDescription, profileMetatype);
Guid profileTypeId = Guid.NewGuid();
profileData.Id = profileTypeId;
profileData.MembershipProvidersUsage = MembershipProvidersUsage.AllProviders;
profileData.ProfileProviderName =
"OpenAccessProfileProvider"
;
profileData.Title =
"TestProfileType"
;
profileData.AppliedTo =
"All user providers"
;
UserProfilesHelper.CreateUserProfileType(profileData,
string
.Empty);
var contentType = TypeResolutionService.ResolveType(
"Telerik.Sitefinity.Security.Model.testprofile"
);
var cfContext =
new
CustomFieldsContext(contentType);
var fieldsToAdd =
new
Dictionary<
string
, WcfField>();
fieldsToAdd.Add(
"Street"
,
new
WcfField()
Name =
"Street"
,
ContentType =
"Short text"
,
Definition =
new
WcfFieldDefinition()
FieldType =
"Telerik.Sitefinity.Web.UI.Fields.TextField"
,
Title =
"Street"
,
FieldName =
"Street"
,
DatabaseMapping =
new
WcfDatabaseMapping()
ClrType =
"System.String"
,
DbLength =
"255"
,
DbType =
"VARCHAR"
,
Nullable =
true
,
Indexed =
false
,
FieldTypeKey =
"ShortText"
,
IsCustom =
true
);
fieldsToAdd.Add(
"Area"
,
new
WcfField()
Name =
"Area"
,
ContentType =
"Multiple choice"
,
Definition =
new
WcfFieldDefinition()
Choices =
"[\"Text\":\"Area One\",\"Value\":\"Area One\",\"Selected\":false,\"Text\":\"Area Two\",\"Value\":\"Area Two\",\"Selected\":false,\"Text\":\"Area Three\",\"Value\":\"Area Three\",\"Selected\":false]"
,
FieldType =
"Telerik.Sitefinity.Web.UI.Fields.ChoiceField"
,
Title =
"Area"
,
FieldName =
"Area"
,
DatabaseMapping =
new
WcfDatabaseMapping()
ClrType =
"System.String"
,
DbType =
"VARCHAR"
,
Nullable =
true
,
Indexed =
false
,
FieldTypeKey =
"MultipleChoice"
,
IsCustom =
true
);
if
(fieldsToAdd.Count > 0)
cfContext.AddOrUpdateCustomFields(fieldsToAdd, contentType.Name);
var fieldsToRemove =
new
List<
string
>();
// In case you want to remove a field from existing profile type.
// fieldsToRemove.Add("Street");
if
(fieldsToRemove.Count > 0)
cfContext.RemoveCustomFields(fieldsToRemove, contentType.Name);
cfContext.SaveChanges();
This is a really useful thread, exactly what I needed. Thanks.
I have another question: how to declare the "fieldsToAdd" in case of classification instead of multiple choice? I would like to add an already existing hierarchical classification ..
Thank you