Home > Best Practices, Development, SharePoint Server 2010 > Creating Custom Profile Property in SharePoint Server 2010

Creating Custom Profile Property in SharePoint Server 2010

if you have requirements to create Custom Profile Property in SharePoint 2010

below function do That

private
void CreateCustomProperty(string profilePropertyText,SPSite siteColl)

{


SPSecurity.RunWithElevatedPrivileges(delegate()

{

siteColl.AllowUnsafeUpdates = true;

siteColl.OpenWeb().AllowUnsafeUpdates = true;


//SPContext.Current.Web.AllowUnsafeUpdates = true;


SPServiceContext serviceContext = SPServiceContext.GetContext(siteColl);


UserProfileConfigManager profileConfigManager = new
UserProfileConfigManager(serviceContext);


CorePropertyManager coreProperties = profileConfigManager.ProfilePropertyManager.GetCoreProperties();


try

{


List<string> MyCustomProperties = new
List<string>();

MyCustomProperties.Add(profilePropertyText);


ProfilePropertyManager propertyManager = profileConfigManager.ProfilePropertyManager;


foreach (string profileProperty in MyCustomProperties)

{


if (coreProperties.GetPropertyByName(profileProperty) == null)

{


CoreProperty propertyInstance = coreProperties.Create(false);

propertyInstance.Name = profileProperty.Replace(” “, string.Empty);

propertyInstance.Type = PropertyDataType.String;

propertyInstance.Length = 2000;


//propertyInstance.Separator = MultiValueSeparator.Semicolon;

propertyInstance.DisplayName = profileProperty;

propertyInstance.Description = profileProperty;

propertyInstance.IsAlias = false;

propertyInstance.IsSearchable = true;

propertyInstance.Commit();


ProfileTypePropertyManager profileTypePropertyMgr = propertyManager.GetProfileTypeProperties(ProfileType.User);


ProfileTypeProperty profileTypeProperty = profileTypePropertyMgr.Create(propertyInstance);

profileTypeProperty.IsVisibleOnViewer = true;

profileTypeProperty.IsVisibleOnEditor = true;

profileTypePropertyMgr.Add(profileTypeProperty);


ProfileSubtypeManager psm = ProfileSubtypeManager.Get(serviceContext);


ProfileSubtype ps = psm.GetProfileSubtype(ProfileSubtypeManager.GetDefaultProfileName(ProfileType.User));


ProfileSubtypePropertyManager pspm = ps.Properties;


ProfileSubtypeProperty psp = pspm.Create(profileTypeProperty);

psp.IsUserEditable = true;


//psp.Commit();

pspm.Add(psp);

}

}

siteColl.AllowUnsafeUpdates = false;


//SPContext.Current.Web.AllowUnsafeUpdates = false;

siteColl.OpenWeb().AllowUnsafeUpdates = true;

}


catch (DuplicateEntryException exception)

{

siteColl.AllowUnsafeUpdates = false;


SPContext.Current.Web.AllowUnsafeUpdates = false;

logMessage(exception.Message, EventLogEntryType.Error);

}

});

}

to check if specific Profile Property exist or not

private
bool CheckCustomPropertyExistance(string profileProperty, SPSite siteColl)

{


bool exist = false;


SPSecurity.RunWithElevatedPrivileges(delegate()

{


SPServiceContext serviceContext = SPServiceContext.GetContext(siteColl);


UserProfileConfigManager profileConfigManager = new
UserProfileConfigManager(serviceContext);


CorePropertyManager coreProperties = profileConfigManager.ProfilePropertyManager.GetCoreProperties();


try

{


if (coreProperties.GetPropertyByName(profileProperty) != null)

{

exist = true;

}

}


catch (DuplicateEntryException exception)

{

logMessage(exception.Message, EventLogEntryType.Error);

}

});


return exist;

}

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: