As you may know, the MySite host comes with some predefined webpart pages. The one, most users of MySites know, is person.aspx, which is shown, when the user selects “My profile” in the welcome menu. But there are other pages, e.g. OrganizationView.aspx, which is used to show the organization browser. To modify these pages, one very simple approach is, to use SharePoint Designer. This works, but if you have a straight forward application lifecycle, with a development-, a test- and a production environment, using SharePoint Designer for these tasks is not the right way. In this case you need a SharePoint solution, to apply these changes in each system.

This post will show an example, how you can replace the default person.aspx with a customized one. This customized person.aspx could contain a different layout or controls, than the original one.

So, what are the steps to customize this page?

  1. Create a new SharePoint 2010 project in Visual Studio. Use the template for an Empty SharePoint Project.
  2. Add a new module to the project. In this description the name of the module is “CustomPerson”.
  3. Add your own aspx-file to the module. For demonstration, I copied the original person.aspx into the project and renamed the file to CustomPerson.aspx.
  4. Edit the elements.xml of the module and remove the File-node with the sample.txt. As an example, the elements.xml could look like this

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Module Name="CustomPerson">
    <File Path="CustomPerson\CustomPerson.aspx" Url="CustomPerson.aspx">
      <AllUsersWebPart WebPartZoneID="MiddleLeftZone" WebPartOrder="1">
        <![CDATA[
                    <WebPart xmlns="http://schemas.microsoft.com/WebPart/v2">
                        <Assembly>Microsoft.SharePoint.Portal, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c</Assembly>
                        <TypeName>Microsoft.SharePoint.Portal.WebControls.AskMeAboutWebPart</TypeName>
                        <Title>$Resources:spscore,MySiteOnet_WebPart_AskMeAbout_Title</Title>
                        <Description>$Resources:spscore,MySiteOnet_WebPart_AskMeAbout_Description</Description>
                        <PartOrder>1</PartOrder>
                        <FrameType>TitleBarOnly</FrameType>
                        <AllowMinimize>true</AllowMinimize>
                        <AllowRemove>true</AllowRemove>
                        <IsVisible>true</IsVisible>
                    </WebPart>
                    ]]>
      </AllUsersWebPart>
    </File>
  </Module>
</Elements>

  1. From the CustomPerson module remove the file sample.txt. It’s not used anymore.
  2. When you add a new module, and your project contains no feature, a new feature is added to your project. Rename the feature to a name you can remember ( J ). The scope of the feature should be “Web”.
  3. Add a feature receiver to the feature and add this code to the feature receiver.

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
    SPWeb web = properties.Feature.Parent as SPWeb;

    try
    {
        SPFile originalFile = web.GetFile("person.aspx");

        originalFile.MoveTo("person.backup.aspx");

        SPFile newFile = web.GetFile("CustomPerson.aspx");

        newFile.CopyTo("person.aspx");
    }
    catch (Exception ex)
    {
    }
}

public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
    SPWeb web = properties.Feature.Parent as SPWeb;

    SPFile originalFile = web.GetFile("person.backup.aspx");

    originalFile.MoveTo("person.aspx", true);

    SPFile file = web.GetFile("CustomPerson.aspx");

    if (file != null)
    {
        file.Delete();
    }
}

That’s it. Compile the project and deploy the package to the site collection with the MySite host. On feature activation, the original person.aspx is renamed to person.backup.aspx, so we have a backup of the original file. Then our CustomPerson.aspx is copied to person.aspx, so we have replaced the file with our content. On feature deactivation, the person.aspx file is overwritten with the backup we made, when the feature is activated. Additionally the CustomPerson.aspx is removed from the web.

Advertisements