Archive

Posts Tagged ‘MySite branding’

Set Content Types of default list and libraries

July 6th, 2011 Comments off

We discussed the branding of the MySite through feature stapling in previous post. Now, what if you have to change the content types of the personal and shared document libraries in the personal MySites.

Unfortunately you cannot do it from feature stapling. As your feature stapler executed before the creation of document libraries and it cannot find the document libraries to set the content type. In simple words you need to set content type after site provisioning, so your code can see the document libraries. I thought we can have a control on the master page which should run on the first time user open the site. But need to run just once.

So the solution is to use the power of SharePoint delegate controls. I created a custom control and then loaded that control in a SharePoint delegate control through a feature. This control set the content types for libraries and after completing the job it deactivated the feature. This deactivation actually removes the custom user control from SharePoint Delegate control. So it executed just once and I got the desired results from an elegant solution.

Lets start the work now……

Use the same project we created last time for MySite branding. Create a custom user control in the MySiteBranding project. Your project will look like this at this stage.

Now add a new feature to hook up your custom user control in a delegate control. Add an empty element to the project and add it to the new ContentTypeFeature. Remember to set the feature scope to Site.

Now load your custom user control to the AdditionalPageHead delegate control. Change your elements.xml file as below.

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Control Id="AdditionalPageHead"
           Sequence="80"
           ControlSrc="~/_controltemplates/MysiteBranding/ContentTypeHook.ascx"></Control>
</Elements>

Here is the code for custom user control. I’m assuming that you already added the content types to the site. You can add it through feature stapler.

/// <summary>
/// Defines the MySiteHook class.
/// </summary>
public partial class MySiteHook : UserControl
{
    /// <summary>
    /// Page load method.
    /// </summary>
    /// <param name="sender">object for sender</param>
    /// <param name="e">object for events args </param>
    protected void Page_Load(object sender, EventArgs e)
    {
        SPWeb web = SPContext.Current.Web;
        SPSite site = web.Site;
        string contentTypeFeatureGuid = "6369c736-eff8-4c72-9397-b56fbc456a3b";
        string personalDocLibName = "Personal Documents";
        string sharedDocLibName = "Shared Documents";
        try
        {
            web.AllowUnsafeUpdates = true;

            // Get the SPList objects
            SPList sharedList = web.Lists[sharedDocLibName];
            SPList personalList = web.Lists[personalDocLibName];

            // Your content types has to be added to the site.
            SPContentType customContentType = web.ContentTypes["Salman Docs"];
            AddAsDefaultContentType(sharedList, customContentType);
            AddAsDefaultContentType(personalList, customContentType);
            web.Update();

            // Deactivate the feature that this control is linked to so that this code will not run again.
            bool hasFeature = false;
            foreach (SPFeature feature in site.Features)
            {
                if (feature.DefinitionId.ToString().Equals(contentTypeFeatureGuid, StringComparison.CurrentCulture))
                {
                    hasFeature = true;
                    break;
                }
            }

            if (hasFeature)
            {
                site.Features.Remove(new Guid(contentTypeFeatureGuid));
            }
        }
        finally
        {
            web.AllowUnsafeUpdates = false;
        }
    }

    /// <summary>
    /// This method add the content type as default content type.
    /// </summary>
    /// <param name="list">The SPList object.</param>
    /// <param name="customContentType">The SPContentType object.</param>
    private static void AddAsDefaultContentType(SPList list, SPContentType customContentType)
    {
        // Enable content type management on this list
        list.ContentTypesEnabled = true;
        list.Update();

        // Add the custom content type
        list.ContentTypes.Add(customContentType);
        list.Update();

        // Set it as default content type
        foreach (SPContentType contentType in list.ContentTypes)
        {
            if (contentType.Name != customContentType.Name)
            {
                list.ContentTypes.Delete(contentType.Id);
                list.Update();
            }
        }
    }
}

You have no need to make any change in the master page. If you look in the default master page, you can see that AdditionalPageHead delegate control exists there.

<SharePoint:DelegateControl runat=”server” ControlId=”AdditionalPageHead” AllowMultipleControls=”true”/>

Ok so the last thing you have to do is to add your feature to site definition. Use the existing feature stapling concept and just add this feature as well in the elements file for MySiteStapler. Your feature file will look like this now.

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <!-- Feature to staple with MySite host site collection -->
  <FeatureSiteTemplateAssociation Id="13719e8d-0a3d-4a1a-97a9-3e6aa9adf4c5" TemplateName="SPSMSITEHOST#0" ></FeatureSiteTemplateAssociation>
  <!-- Feature to staple with MySite personal site collection -->
  <FeatureSiteTemplateAssociation Id="13719e8d-0a3d-4a1a-97a9-3e6aa9adf4c5" TemplateName="SPSPERS#0" ></FeatureSiteTemplateAssociation>
  <!--MySiteDocs: Feature to replace content types in document libraries for MySite personal site collection-->
  <FeatureSiteTemplateAssociation Id="6369c736-eff8-4c72-9397-b56fbc456a3b" TemplateName="SPSPERS#0" ></FeatureSiteTemplateAssociation>
</Elements>

Ok all is done now just create the site with new site definition. So when you create your new MySite, it will change the master page as we discuss in the last post. Also it will load your custom control to the AdditionalPageHead delegate control. When user opens the MySite first time your control’s code will change the content type of the shared and personal document libraries. So all is done. Your project finally looks like this.

You can use this approach for anything which needs to be done after site provisioning.

MySite Branding for SharePoint 2010

July 4th, 2011 Comments off

randing of MySites is always a challenging task. It was complicated in SharePoint 2007 and still confusing in SharePoint 2010. Recently we had a requirement to applying cooperate branding for MySites. I tried to find a reasonable solution but nothing was fitting well in the required scenario. So after completion I decided to share my idea with the world.

Before start applying new branding for MySites. We need to understand that MySites are actually use two different types of site collections. First you need a MySite host site collection and then each personal site is a separate site collection. So we have to apply branding on both sites.

Now come to the main task. There could be different ways to customise the MySites. But the best possible solution would be using feature stapling. If you are new to feature stapler you can see this good post from Chris (http://www.sharepointnutsandbolts.com/2007/05/feature-stapling.html).

So we need to create first feature with Site level scope. This feature will be called MySiteStaplee. This feature will deploy master pages to the site and also change the master page for the site. Right first step would be to create a new project in Visual Studio 2010 and create a feature with Site level scope. It will look like this.

MySite Stapler Project View
Now we need to attach an elements.xml to include the master pages to deploy through this feature. Now it is bit different in Visual Studio 2010. We need to add a module. Add master page in the module, and make changes in the elements.xml file as below.

<?xml version=”1.0″ encoding=”utf-8″?>
<Elements xmlns=”http://schemas.microsoft.com/sharepoint/”>
<Module Name=”MasterPages” List=”116″ Url=”_catalogs/masterpage”>
<File Path=”MasterPages\salman.master” Url=”salman.master” Type=”GhostableInLibrary” />
</Module>
</Elements>

Your project will look like this now.

Now add a property to the master with a value as master name.

<?xml version=”1.0″ encoding=”utf-8″ ?>
<Feature xmlns=”http://schemas.microsoft.com/sharepoint/“>
<Properties>
<Property Key=”MySiteMasterPage” Value=”/_catalogs/masterpage/salman.master”/>
</Properties>
</Feature>

Next thing we need to add a feature event receiver to replace the master page for mysite. For this add feature event receiver with the MySiteStaplee. Your code for featureactivated will look like this.

/// <summary>
/// Key for MySite master.
/// </summary>
private const string MySiteMasterPage = “MySiteMasterPage”;

/// <summary>
/// Set master page on feature activation.
/// </summary>
/// <param>The SPFeatureReceiverProperties object.</param>
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPSite site = properties.Feature.Parent as SPSite;

if (site != null)
{
SPWeb web = site.RootWeb;
string oldMasterUrl = web.MasterUrl;

web = site.RootWeb;
string newMySiteMasterUrl = properties.Feature.Properties[MySiteMasterPage].Value;
if (!string.IsNullOrEmpty(newMySiteMasterUrl))
{
web.CustomMasterUrl = SPUrlUtility.CombineUrl(web.ServerRelativeUrl, newMySiteMasterUrl);
web.MasterUrl = SPUrlUtility.CombineUrl(web.ServerRelativeUrl, newMySiteMasterUrl);
}

web.Update();
}
}

Ok, so your staple feature is ready. Now you need a stapler to staple your feature with mysite site definition.
In order to achieve this create a new project. Create a new feature in this project with Farm level scope. It will look like this.

Now we need to staple the MySiteStaple with MySite host and MySite personal site definitions. So we need elements file here. Add a new Empty Element from Visual Studio 2010. Remember mysite host site definition’s template name is SPSMSITEHOST and MySite personal site template name is SPSPERS. Your elements file will look like this.

<?xml version=”1.0″ encoding=”utf-8″?>
<Elements xmlns=”http://schemas.microsoft.com/sharepoint/“>
<!– Feature to staple with MySite host site collection –>
<FeatureSiteTemplateAssociation TemplateName=”SPSMSITEHOST#0″ >
</FeatureSiteTemplateAssociation>
<!– Feature to staple with MySite personal site collection –>
<FeatureSiteTemplateAssociation TemplateName=”SPSPERS#0″ >
</FeatureSiteTemplateAssociation>
</Elements>

Ok so all is done. Now you have to deploy these wsp solutions to the server. This will apply your master page with MySite host and MySite personal site definitions. So whenever you create a new MySite host or whenever a user creates his/her MySite, this feature will replace the master page for the new site. Your final solution structure will look like this in visual studio 2010.

I hope it will help you start the branding. Please wait for my next post with the advance solution required for some more branding issues with MySites.

Cheers.