In this article we will discuss how we can personalize MySite.
To customize the MySite we will use the Feature Stapling. We will do the following :
- Adding custom list instance to the MySite
- Changing navigation and MySite Theme
- Creating a new Page when MySite is being created.
- Deleting WebPart from the Default page of the MySite.
First of all we must create a feature Staple. In this article we will use the WSPBuilder instead of VSeWSS.
Creating the Feature Staple
- First of all we will create an WSP Project
After creating the WSP project we must create a new WSPBuilder item “Feature With Receiver”
Then we must create another feature without receiver (blank Feature) that we will call “BOSSMySiteStapling” .
Now we have to add some code and configurations to theses two features.
Setting the Scope of the Features:
Feature
| Scope
|
BOSSMySiteStaple
| Farm
|
FeatureStaplerMySite
| Web
|
In the BOSSMySiteStaple we must associate the MySite template with the FeatureStaplerMySite. For that we must add the following line in the “element.xml” of this feature.
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<FeatureSiteTemplateAssociation Id="GUID OF THE FEATUREMYSITE" TemplateName="SPSPERS#0"/>
</Elements>
The SPSPERS#0 => SPSPERS (which indicate that’s a MySite template) # 0 (indicate the default template of the MySite)
So if you are using different templates for the MySite , just add other lines with the corresponding template ID.
Now the Feature stapling is completed, we can add code we want in the FeatureActivated event.
Adding Code to the FeatureStaplerMySite.cs
Adding a custom list instance to our MySite.
SPWeb web = (SPWeb)properties.Feature.Parent;
SPFeatureCollection featureCollect = web.Site.Features;
featureCollect.Add(new Guid("GUID OF YOUR LIST INSTANCE"), true);
Changing navigation and MySite theme
Changing navigation
As a first step we will delete all the existing site navigation
List<SPNavigationNode> nodeQuicklanch= new List<SPNavigationNode>();
foreach (SPNavigationNode node in web.Navigation.QuickLaunch)
{
nodeQuicklanch.Add(node);
}
foreach (SPNavigationNode node in nodeQuicklanch)
{
web.Navigation.QuickLaunch.Delete(node);
}
To add a new navigation we can use the following
SPNavigationNode node_1 = new SPNavigationNode("NODE1","FormServerTemplates/NODE1Page.aspx");
Changing the Theme of MySite
That’s a very easy step; we have just to apply a new theme to the web.
web.ApplyTheme("Simple");
Creating a new Page programmatically when MySite is created.
We will use web service offered by WSS. Using the web.ProcessBatchData()
SPList list = web.Lists["Form Templates"]; // retrieving the Form Templates library
// Query to create a new page and store it in the Form Templates library.
string postInformation =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<Method>" +
"<SetList Scope=\"Request\">" + list.ID + "</SetList>" +
"<SetVar Name=\"ID\">New</SetVar>" +
"<SetVar Name=\"Cmd\">NewWebPage</SetVar>" +
"<SetVar Name=\"Type\">WebPartPage</SetVar>" +
"<SetVar Name=\"WebPartPageTemplate\">1</SetVar>" +
"<SetVar Name=\"Title\">TITLE OF YOUR PAGE</SetVar>" +
"<SetVar Name=\"Overwrite\">true</SetVar>" +
"</Method>";
// Processing Query
string processBatch = web.ProcessBatchData(postInformation);
Your page will be created and stored in the Form Templates library. You can should whatever list you want.
You can also choose the WebPartPageTemplate
<SetVar Name=\"WebPartPageTemplate\">1</SetVar
And you have the ability to overwrite the file if it’s already exists on the Library.
<SetVar Name=\"Overwrite\">true</SetVar>"
Removing existing WebPart in the default page of MySite.
The Default.aspx exists only if the MySite is fully provisioned. So we will create a method that will run in a separated thread and will test if the MySite has been provisioned or not. If it’s provisioned ,it will remove the existing webparts in the Default.aspx page.
public void removeWebPart(object web2) {
bool provisionned = false;
SPWeb web = null;
while (provisionned== false) {
SPSite site = new SPSite(((SPWeb)web2).Url);
web = site.OpenWeb();
provisionned = web.Provisioned;
Thread.Sleep(10000);
};
SPFile thePage = web.RootFolder.Files["default.aspx"];
SPLimitedWebPartManager manager = thePage.GetLimitedWebPartManager(PersonalizationScope.Shared);
List<Microsoft.SharePoint.WebPartPages.WebPart> webParts = new List<Microsoft.SharePoint.WebPartPages.WebPart>();
foreach (Microsoft.SharePoint.WebPartPages.WebPart webPart in manager.WebParts)
{
webParts.Add(webPart);
}
foreach (Microsoft.SharePoint.WebPartPages.WebPart webPart in webParts)
{
manager.DeleteWebPart(webPart);
manager.Web.Update();
}
manager.Dispose();
web.Close();
}
The thread.Sleep is used to wait a moment before testing again the status of the web.provisioned .
Now in the FeatureActivated event, we must call our method using a ParameterizedThreadStart.
So we will add the following lines:
ParameterizedThreadStart threadDelWebpart = new ParameterizedThreadStart(removeWebPart);
Thread thread = new Thread(threadDelWebpart);
thread.Start(web);