Profile Customization From A to Z

Target:
Customize SharePoint Profile properties.

Details:
Demo:
Create custom property with name "gender". By default it renders as textbox, in the following demo we will change this default behavior to make this property renders as radio button.
1-    Open Central Administration site and click on manage service application link.

2-    Then click on User Profile Service Application link.
3-    Click on Manage User Properties  

4-    Create new property by this values:
·         Name: Gender.
·         Display Name: CP-Gender.
·         Type: string (Single Value).
·         Length: 6.
·         Default User Profile Subtype: True.
·         User can override: True
·         Policy Settings Section: Choose anything.
·         Edit Settings Section: Allow users to edit values for this property.
·         Display Settings: Show on the Edit Details page or select all if you want.
5-    Now the property with name gender is created.
6-    Gender property appears as textbox in the profiling site.
7-    To change this behavior, we must answer to this question first :
How does the profiling control work?
·         The profiling control is simply server control which have a method called " LoadEditValueControl" it take 3 parameters and return control the parameters are :
                                          i.    Prop: property object.
                                         ii.    propValue: value for the property for the current user.
                                        iii.    sectionName: property section.


8-    Back to demo. Create SharePoint project "ProfileDemo" in visual studio and create class "CustomProfileEditor.cs" inherited from "Microsoft.SharePoint.Portal.WebControls.ProfileEditor" class.
9-    Override "LoadEditValueControl" method and return your control instead of the default textbox:
using System.Linq;
using System.Web.UI;
using Microsoft.Office.Server.UserProfiles;
using Microsoft.SharePoint.Portal.WebControls;
using System.Web.UI.WebControls;

namespace ProfileDemo
{
    public class CustomProfileEditor : ProfileEditor
    {
        protected override Control LoadEditValueControl(ProfileSubtypeProperty prop, object[] PropValue,
                                                                                            string sectionName)
        {
            Control control = base.LoadEditValueControl(prop, PropValue, sectionName);
            switch (prop.Name)
            {
                case "CP-Gender":
                    DropDownList ddlGender = new DropDownList {ID = control.ID};
                    ddlGender.Items.Add("Male");
                    ddlGender.Items.Add("Female");
                    if (PropValue.Any())
                    {
                        ddlGender.SelectedValue = PropValue[0].ToString();
                    }
                    control = ddlGender;
                    break;
            }
            return control;
        }
    }
}

10- Add page with name "editprofile.aspx" which in path "\15\TEMPLATE\LAYOUTS" in layouts mapped folder in ProfileDemo solution.

11-  Add registry for your control in this page .
<%@ Register TagPrefix="CPE" Namespace="ProfileDemo"
     Assembly="ProfileDemo, Version=1.0.0.0, Culture=neutral, PublicKeyToken=48a4d279fefe43cf" %>

12- Then replace this line:
<SPSWC:ProfileEditor id="ProfileEditor1" runat="server" />
By this
<CPE:CustomProfileEditor id="ProfileEditor1" runat="server" />

13- Deploy and see the result:


2 comments

Post a Comment