Thursday, December 09, 2010

Using Delegation Control (such as AdditionalPageHead or SmallSearchInputBox) to add headers (scripts, styles etc) in MOSS 2007 sites

This is an easy way to add control across all the sites/pages in MOSS (whether system or other templates provided by Microsoft).

These can be used in your custom layouts/ Master Pages as well.

More on DelegationControl is here. Note that WSS 3.0 and SharePoint 2010, both provide this useful control.

Scenario: Let’s say you have to add a JavaScript on all the pages in all SharePoint sites. The easiest way to do it would be using DelegateControl.

AdditionalPageHead is one of the controls provided by Microsoft in the SharePoint Master Pages. Let’s see how we can use this.

1. Open Visual Studio 2008 and select “SharePoint” in the group (this should be there if you have VSeWSS installed) and create “Empty” project named “MyProject”.

2. Add a class MyControl and inherit it from “System.Web.UI.WebControls.WebControl”.

3. Implement the method “OnLoad” like the following:

using System.Web.UI.WebControls;

namespace MyProject
{
    public class MyControl: WebControl
    {
        protected override void OnLoad(EventArgs e)
        {
            this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "popup", "<script>alert('hello man!');</script>");
        }
    }
}

4. Open the WSP View (if you don’t find it, go to “View>Other Windows>WSP View" in Visual Studio)

5. Add new Feature there, and select “WebApplication” when it asks for its scope. You can change the scope later as well but since in this case we want the script be available all across the application we selected “WebApplication”. You can select “Web” if you need it only on one site and “Site” if you need it on only one site collection. If you select “Farm” it will be applied to all the applications and sites on the server.

6. In the WSP View, open the featuer.xml and edit as follows:

<Feature Id="306e3fa9-8876-4778-9e01-17757c362f36" Title="My Control Title" Scope="WebApplication" Version="12.0.0.0" Hidden="FALSE" DefaultResourceFile="core" xmlns="http://schemas.microsoft.com/sharepoint/">
  <ElementManifests>
    <ElementManifest Location="Feature1\Elements.xml" />
  </ElementManifests>
</Feature>

(change the folder name as you see it in the “Solution Explorer” that contains Element1.xml. I renamed the elements file name)

7. Edit the elements.xml file as follows:

<?xml version="1.0" encoding="utf-8"?>
<Elements Id="b749dad4-3e1d-489b-a21b-bcab3258736b" xmlns="http://schemas.microsoft.com/sharepoint/">
  <Control Id="AdditionalPageHead"
           ControlAssembly="MyProject, Version=1.0.0.0, Culture=neutral, PublicKeyToken=7a16335555e20c79"
           ControlClass="MyProject.MyControl" Sequence="1"/>
</Elements>

- specify Sequence number under 100
- Use .NET Reflector to use get the assembly full name
- ControlClass should contain full “namespace.classname”

8. Now open the web.app of the application you need to deploy this control to. It should be in C:\Inetpub\wwwroot\wss\VirtualDirectories folder.

9. Add entry as below:

<SafeControl Assembly="MyProject, Version=1.0.0.0, Culture=neutral, PublicKeyToken=7a16335555e20c79" Namespace="MyProject" TypeName="MyControl" Safe="True" />

- Make sure you give full assembly information in “Assembly” property
- Make sure Namespace ONLY contains namespace (and not the class name). You would say, of course, but Yea, a little care would save you some time.
- TypeName should contain ONLY the class name of control OR “*”. Adding asterisk would make all the controls in the assembly “safe” for the web application which some administrators may not like.

Just right click solution and hit “Deploy”. Watch closely to ensure it compiled successfully. Because even if it fails, VSeWSS will deploy the old assemblies, if available.

We are all set!

No comments:

Post a Comment

Feel free to give constructive feedback or let me know if it was helpful.