Tuesday, 17 May 2011

Creating Custom Property in share point 2007 using object model

To create the custom property in share point 2007 using object model use the following code .

SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (SPSite elevatedSite = new SPSite(siteId))
                    {
                            elevatedSite.AllowUnsafeUpdates = true;

                            elevatedSite.RootWeb.AllowUnsafeUpdates = true;

Microsoft.Office.Server.ServerContext serverContext = Microsoft.Office.Server.ServerContext.GetContext(elevatedSite);
                            Microsoft.Office.Server.UserProfiles.UserProfileManager profileManager = new Microsoft.Office.Server.UserProfiles.UserProfileManager(serverContext);
                            Microsoft.Office.Server.UserProfiles.UserProfileConfigManager profileConfigManager = new Microsoft.Office.Server.UserProfiles.UserProfileConfigManager(serverContext);
                            Microsoft.Office.Server.UserProfiles.PropertyCollection propertyCollection = profileConfigManager.GetProperties();


Microsoft.Office.Server.UserProfiles.Property customProperty = propertyCollection.Create(false);
// Set the property seetings
                customProperty.Name = propertyName;
                customProperty.DisplayName = displayName;
                customProperty.Type = "string";
                customProperty.PrivacyPolicy = Microsoft.Office.Server.UserProfiles.PrivacyPolicy.OptIn;
                customProperty.DefaultPrivacy = Microsoft.Office.Server.UserProfiles.Privacy.Private;
                customProperty.Description = propertyName;
                customProperty.IsSearchable = false;
                customProperty.IsVisibleOnEditor = false;
                customProperty.IsAlias = false;
                customProperty.Length = 200;
                customProperty.IsUserEditable = true;
                propertyCollection.Add(customProperty);
}
}
                });