Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

Here when I am creating a new custom section it is showing an error as Adding a property definition requires Manage User Profiles rights in the code

CoreProperty cp = cpm.Create(true);

How can I solve this problem?

SPSite site = new SPSite("localhost");
SPServiceContext context = SPServiceContext.GetContext(site);
UserProfileConfigManager upcm = new UserProfileConfigManager(context);

ProfilePropertyManager ppm = upcm.ProfilePropertyManager;

CorePropertyManager cpm = ppm.GetCoreProperties();

if (cpm.GetSectionByName(propertyname) == null)
{
       **CoreProperty cp = cpm.Create(true);**
       cp.Name = propertyname;
       cp.DisplayName = displayname;
       cpm.Add(cp);

       ProfileTypePropertyManager ptpm = ppm.GetProfileTypeProperties(ProfileType.User);
       ProfileTypeProperty ptp = ptpm.Create(cp);
       ptpm.Add(ptp);

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

       ProfileSubtypePropertyManager pspm = ps.Properties;
       ProfileSubtypeProperty psp = pspm.Create(ptp);
       pspm.Add(psp);
}
share|improve this question
add comment

1 Answer

I think there is a pemission problem.

Run your code in elevated privileges, like this:

            SPSite site = properties.Feature.Parent as SPSite;
            SPUser AdminUser = sitefeature.RootWeb.AllUsers[@"SHAREPOINT\SYSTEM"];
            var superToken = AdminUser.UserToken;
            HttpContext con = HttpContext.Current;

            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite site = new SPSite(sitefeature.Url, superToken))
                {
                   // create custom property code here
                }
            });

            HttpContext.Current = con;
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.