Edit User Profile Property policy Programmatically   

Tags: SharePoint 2010
Technorati Tags:

In my previous post (Edit User Profile Porpery policy) I have shown you how to edit the policy of a User Profile Property field through SharePoint Administrator page.

Now i am going to show you how can edit this by code.

You are probably thinking “I thought we are going to see code” well my answer is ‘Yes’. The first step is very important or else we can’t use are peace of code. You will get the error that your not authorized to edit the user profile property fields, because the primary administrator isn’t added to the user list.

Open visual studio and start a new project. Select SharePoint 2010 > Empty SharePoint Project.

By name fill in “DisableUserProfileField”.

 

NewProject

 

Select farm solution and click finish.

 

AddFeature

 

Right click on “Features” and click on “Add Feature”

 

Rename the feature to “DisableUserProfileFieldFeature” and the scope of the feature is site.

 

AddEventReceiver

 

Right click on DisableUserProfileFieldFeature and select “Add Event Receiver”.

 

In the event receiver uncomment “FeatureActivated” and “FeatureDeactivating”.

 

// Uncomment the method below to handle the event raised after a feature has been activated.
 
 public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
}
 
// Uncomment the method below to handle the event raised before a feature is deactivated.
 
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
}
 
When you activate the feature this code will run and sets the settings of Job-Title to required and only to view by his colleagues. 
 
 
 public override void FeatureActivated(SPFeatureReceiverProperties properties)
  {
      //Where editing in the administrator section so we have to use SPSecurity.RunWithElevatedPrivileges so we don't get user not autherized
      SPSecurity.RunWithElevatedPrivileges(delegate()
      {
          using (SPSite site = (SPSite)properties.Feature.Parent)
          {
 
             try
             {
 
               SPServiceContext serviceContext = SPServiceContext.GetContext(site);
 
                //collection of profilesubtypes
                ProfileSubtypeManager sm = ProfileSubtypeManager.Get(serviceContext);
 
                // get the user profile sub type with profiletype user(other types are group and organisation)
                ProfileSubtype ps = sm.GetProfileSubtype(ProfileSubtypeManager.GetDefaultProfileName(ProfileType.User));
 
                //Get the standard user properties 
                 ProfileSubtypePropertyManager userProperties = ps.Properties;
 
                //Gets the property jobtitle
                var jobTitleItem = userProperties.GetPropertyByName("SPS-JobTitle");
                        
                //Sets the property to required
                 jobTitleItem.PrivacyPolicy = PrivacyPolicy.Mandatory;
 
                //Sets the property yo my colleagues
                 jobTitleItem.DefaultPrivacy = Privacy.Organization;
 
                //updates the user profile
                jobTitleItem.Commit();
                }
                 catch (Exception ex)
                {
                    throw new Exception(ex.ToString());
                 }
 
                }
            });
        }
 
When you deactivate the feature this code will run and the settings of Job-Title will be set to optional
and public.
 
public override void FeatureDeactivating(SPFeatureReceiverProperties properties){
 //Where editing in the administrator section so we have to use SPSecurity.RunWithElevatedPrivileges so we don't get user not autherized
    SPSecurity.RunWithElevatedPrivileges(delegate()
    {
        using (SPSite site = (SPSite)properties.Feature.Parent)
        {
 
             try
             {
 
                 SPServiceContext serviceContext = SPServiceContext.GetContext(site);
 
                 //collection of profilesubtypes
                 ProfileSubtypeManager sm = ProfileSubtypeManager.Get(serviceContext);
 
                 // get the user profile sub type with profiletype user(other types are group and organisation)
                 ProfileSubtype ps = sm.GetProfileSubtype(ProfileSubtypeManager.GetDefaultProfileName(ProfileType.User));
 
                 //Get the standard user properties 
                 ProfileSubtypePropertyManager userProperties = ps.Properties;
 
                 //Gets the property jobtitle
                 var jobTitleItem = userProperties.GetPropertyByName("SPS-JobTitle");
                        
                 //Sets the property to optional
                 jobTitleItem.PrivacyPolicy = PrivacyPolicy.OptIn;
 
                 //Sets the property To everony
                 jobTitleItem.DefaultPrivacy = Privacy.Public;
 
                 //updates the user profile
                 jobTitleItem.Commit();
 
                 }
                    catch (Exception ex)
                    {
                        throw new Exception(ex.ToString());
                    }
 
                }
            });
        }
 
}

When you get an error that your not authorized please read this blog post:
Add Primary site collection admin to User Profile Service Application

 
Posted by  Gilissen Timmy  on  3/10/2011
0  Comments  |  Trackback Url  | 0  Links to this post | Bookmark this post with:        
 

Links to this post

Comments

Name *:
URL:
Email:
Comments:


CAPTCHA Image Validation