|
The ASP.NET AJAX Control Toolkit provides a set of sample controls and extenders that makes it a snap to spice up your web site with rich functionality, and I think it will be very interesting to embed this toolkit into MOSS 2007. In the following example I’ve started with ASP.NET Ajax Accordion control, and may be later I’ll get the opportunity to embed more controls.
Software Needed:
For all people who are enabling Ajax for the first time in SharePoint 2007, you need first to configure your SharePoint Portal to support AJAX. To do this, it is better to open a new AJAX web site in Visual Studio to pick a copy from configuration sections in web.config , and if you have configured it before you can start directly from step 9.
1– Add the following part under: <configSections>
<sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"> <sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"> <section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="MachineToApplication"/> <sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"> <section name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="Everywhere" /> <section name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="MachineToApplication" /> <section name="authenticationService" type="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="MachineToApplication" /> </sectionGroup> </sectionGroup> </sectionGroup>
2– Add the following part under: <pages >
<controls> <add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" /> </controls>
3– Add the following part under : <compilation><assemblies>
<add assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
4– Add the following part under: <httpHandlers>
<add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/> <add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/> <add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" validate="false"/>
5– Add the following part under: <httpModules>
<add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
6– At the end of web.config add the following part under: <configuration>
<system.web.extensions> <scripting> <webServices> <!-- Uncomment this line to enable the authentication service. Include requireSSL="true" if appropriate. --> <!-- <authenticationService enabled="true" requireSSL = "true|false"/> --> <!-- Uncomment these lines to enable the profile service. To allow profile properties to be retrieved and modified in ASP.NET AJAX applications, you need to add each property name to the readAccessProperties and writeAccessProperties attributes. --> <!-- <profileService enabled="true" readAccessProperties="propertyname1,propertyname2" writeAccessProperties="propertyname1,propertyname2" /> --> </webServices> <!-- <scriptResourceHandler enableCompression="true" enableCaching="true" /> --> </scripting> </system.web.extensions> <system.webServer> <validation validateIntegratedModeConfiguration="false"/> <modules> <add name="ScriptModule" preCondition="integratedMode" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/> </modules> <handlers> <remove name="WebServiceHandlerFactory-Integrated" /> <add name="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/> <add name="ScriptHandlerFactoryAppServices" verb="*" path="*_AppService.axd" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/> <add name="ScriptResource" preCondition="integratedMode" verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" /> </handlers> </system.webServer>
7– Before closing web.config we should add the AJAX controls dll to SharePoint Safe Controls, so copy the following part under: <SafeControls>
<SafeControl Assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" Namespace="System.Web.UI" TypeName="*" Safe="True" />
8– It is time to include the AJAX script Manager to the master page, in my case I’ve included the script manager control in the default.master located in the following path: C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\GLOBAL So, according to your portal template; locate the right master page file or you can open the master page from the SharePoint Designer under _catalogs folder. After you locate the master page file open the file then put the following line inside the top of <form> tag
<asp:ScriptManager runat="server" ID="ScriptManager1"></asp:ScriptManager>
As shown below:
<form runat="server" onsubmit="return _spFormOnSubmitWrapper();"> <WebPartPages:SPWebPartManager id="m" runat="Server" /> <asp:ScriptManager runat="server" ID="ScriptManager1"></asp:ScriptManager> <TABLE class="ms-main" CELLPADDING=0 CELLSPACING=0 BORDER=0 WIDTH="100%" HEIGHT="100%">
9– At this point you’ve configured your SharePoint to support AJAX, once more you need your SharePoint to support the Control Kit, so add the following line under <Controls> tag: <add namespace="AjaxControlToolkit" assembly="AjaxControlToolkit" tagPrefix="ajaxToolkit"/>
10 – Also you should copy the DLL “AjaxControlToolkit.dll” into your SharePoint Bin Folder: C:\Inetpub\wwwroot\wss\VirtualDirectories\80\bin
11– Now it is time to write our code, open a new web part project in Visual studio 2005 then add reference of System.Web.Extensions, AjaxControlToolkit to the project and write the following code to web part code file:
using System; using System.Runtime.InteropServices; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Serialization;
using Microsoft.SharePoint; using Microsoft.SharePoint.WebControls; using Microsoft.SharePoint.WebPartPages;
namespace Accordion { [Guid("6e6ad58b-7e84-46f4-a3f7-e4b276cc0b12")] public class _Accordion : System.Web.UI.WebControls.WebParts.WebPart { protected AjaxControlToolkit.Accordion acc;
public _Accordion() { this.ExportMode = WebPartExportMode.All; }
protected override void CreateChildControls() {
base.CreateChildControls();
// Fix Form Action this.FixingFormAction();
acc = new AjaxControlToolkit.Accordion(); acc.ID = "acc"; acc.FramesPerSecond = 40; acc.FadeTransitions = true; acc.TransitionDuration = 250; acc.SuppressHeaderPostbacks = true; AjaxControlToolkit.AccordionPane pane1 = new AjaxControlToolkit.AccordionPane(); AjaxControlToolkit.AccordionPane pane2 = new AjaxControlToolkit.AccordionPane();
PaneHTMLTemplate header = new PaneHTMLTemplate("<a href=\"\" style=\"border:1px solid #ccc;background-color:#e3eeef\"><b>1. Accordion</b></a>"); PaneHTMLTemplate header2 = new PaneHTMLTemplate("<a href=\"\" style=\"border:1px solid #ccc;background-color:#e3eeef\"><b>2. Accordion</b></a>");
CheckBoxList chk = new CheckBoxList(); CheckBoxList chk2 = new CheckBoxList();
chk.ID = "chk"; chk2.ID = "chk2";
chk.Items.Add(new ListItem("Check1.1")); chk.Items.Add(new ListItem("Check1.2"));
chk2.Items.Add(new ListItem("Check2.1")); chk2.Items.Add(new ListItem("Check2.2"));
Control[] myControls = { new LiteralControl("List No.1:<br />"), chk }; Control[] myControls2 = { new LiteralControl("List No.2:<br />"), chk2 };
PaneControlTemplate cont = new PaneControlTemplate(myControls); PaneControlTemplate cont2 = new PaneControlTemplate(myControls2); pane1.Header = header; pane1.Content = cont; pane2.Header = header2; pane2.Content = cont2; acc.Panes.Add(pane1); acc.Panes.Add(pane2); this.Controls.Add(acc); ; }
//Fixing Form Action private void FixingFormAction() { if (this.Page.Form != null) { string formOnSubmitAtt = this.Page.Form.Attributes["onsubmit"]; if (formOnSubmitAtt == "return _spFormOnSubmitWrapper();") { this.Page.Form.Attributes["onsubmit"] = "_spFormOnSubmitWrapper();"; } } ScriptManager.RegisterStartupScript(this, typeof(_Accordion), "UpdatePanelFixup", "_spOriginalFormAction = document.forms[0].action; _spSuppressFormOnSubmitWrapper=true;", true); } }
//Class for Building Pane HTML Template tempales public class PaneHTMLTemplate : ITemplate { private string template;
public PaneHTMLTemplate(string temp) { template = temp; }
public void InstantiateIn(Control container) { LiteralControl ltr = new LiteralControl(this.template); container.Controls.Add(ltr); } }
//Class for Building Pane Control Template tempales public class PaneControlTemplate : ITemplate { private Control[] tempControls;
public PaneControlTemplate(Control[] tempC) { tempControls = tempC; }
public void InstantiateIn(Control container) { foreach (Control ctl in this.tempControls) { container.Controls.Add(ctl); } } } }
Then deploy the web part to SharePoint and enjoy.
|