WebFormsVisual Studio 2005.NET 2.0XMLAjaxC# 2.0IntermediateDevVisual StudioJavascriptWindows.NETASP.NETC#
Microsoft Office SharePoint 2007 和 ASP.NET 2.0 AJAX 1.0 Web 部件






4.58/5 (24投票s)
一个简单的 SharePoint 2007 Web 部件,用于计算两个数字,并支持 ASP.NET 2.0 AJAX 1.0

引言
本文描述了如何构建您的第一个支持 ASP.NET 2.0 AJAX 1.0 的 SharePoint 2007 Web 部件。
所需软件
- SharePoint Portal Server 2007 (已安装,并在端口 80 上创建了网站集)
- Visual Studio 2005
- Visual Studio 2005 的 SharePoint 2007 扩展
- ASP.NET 2.0 AJAX 1.0
使用代码
首先,您需要配置您的 SharePoint 门户以支持 AJAX。 为此,最好在 Visual Studio 中打开一个新的 AJAX 网站,以便从 web.config 中的配置节复制,或者您可以从此处复制,让我们开始吧
- 在以下位置添加以下部分:
<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>
- 在以下位置添加以下部分:
<pages>
<controls> <add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" /> </controls>
- 在以下位置添加以下部分:
<compilation><assemblies>
<add assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
- 在以下位置添加以下部分:
<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"/>
- 在以下位置添加以下部分:
<httpModules>
<add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
- 在 web.config 的末尾,在以下位置添加以下部分:
<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>
- 在关闭 web.config 之前,我们应该将 AJAX 控件 dll 添加到 SharePoint 安全控件,因此将以下部分复制到以下位置:
<SafeControls>
<SafeControl Assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" Namespace="System.Web.UI" TypeName="*" Safe="True" />
- 现在是将 AJAX 脚本管理器包含到母版页的时候了。 在我的例子中,我已将脚本管理器控件包含在位于以下路径的 default.master 中: C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\GLOBAL
因此,根据您的门户模板;找到正确的母版页文件,或者您可以从 SharePoint Designer 的 _catalogs 文件夹下打开母版页。找到母版页文件后,打开该文件,然后将以下行放在
<form>
标签的顶部。<asp:ScriptManager runat="server" ID="ScriptManager1"> </asp:ScriptManager>
如下图所示
<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%">
- 最后,是时候编写我们的代码了。 从 Visual Studio 2005 打开一个新的 Web 部件项目,然后将 System.Web.Extensions 的引用添加到项目中,并将以下代码写入 Web 部件代码文件
注意:包含了一个 SharePoint 脚本,用于更改可能停止表单提交的表单操作,因此我包含了
FixFormAction
方法,它将再次重置表单操作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 AjaxWebPart { [Guid("733ee261-6e34-49cf-ae29-e8aeb4df4563")] public class AjaxWebPart : System.Web.UI.WebControls.WebParts.WebPart { public AjaxWebPart() { this.ExportMode = WebPartExportMode.All; } // ASP.NET Controls declaration private Label label; private Label label2; private Label label3; private TextBox textBox1; private TextBox textBox2; // ASP.NET AJAX Controls declaration protected UpdatePanel udatePanel; protected UpdateProgress updateProgress; protected override void CreateChildControls() { base.CreateChildControls(); // Fix Form Action this.FixFormAction(); udatePanel = new UpdatePanel(); updateProgress = new UpdateProgress(); udatePanel.ID = "_UpdatePanel"; updateProgress.ID = "_UpdateProgress"; //Create Update Progress Template string templateHTML = "<div><img alt= \"Loading...\" src=\"/_layouts/images/loader.gif\"/> Loading...</div>"; updateProgress.ProgressTemplate = new ProgressTemplate(templateHTML); updateProgress.AssociatedUpdatePanelID = udatePanel.ClientID; udatePanel.UpdateMode = UpdatePanelUpdateMode.Conditional; this.Controls.Add(udatePanel); this.label = new Label(); this.label2 = new Label(); this.label3 = new Label(); this.label.Text = "Enter 1st Number: "; this.label2.Text = "Enter 2nd Number: "; this.textBox1 = new TextBox(); this.textBox1.ID = "TextBox1"; this.textBox2 = new TextBox(); this.textBox2.ID = "TextBox2"; //Adding Controls udatePanel.ContentTemplateContainer.Controls.Add(this.label); udatePanel.ContentTemplateContainer.Controls.Add (this.textBox1); udatePanel.ContentTemplateContainer.Controls.Add (new LiteralControl("<br />")); udatePanel.ContentTemplateContainer.Controls.Add(this.label2); udatePanel.ContentTemplateContainer.Controls.Add (this.textBox2); udatePanel.ContentTemplateContainer.Controls.Add (new LiteralControl("<br /><br />")); Button button = new Button(); button.Text = "Calculate"; button.Click += new EventHandler(HandleButtonClick); udatePanel.ContentTemplateContainer.Controls.Add(button); udatePanel.ContentTemplateContainer.Controls.Add (new LiteralControl(" ")); udatePanel.ContentTemplateContainer.Controls.Add(this.label3); udatePanel.ContentTemplateContainer.Controls.Add (updateProgress); } private void HandleButtonClick(object sender, EventArgs eventArgs) { //Just wait to see the progress loader working System.Threading.Thread.Sleep(1000); this.label3.Text = Convert.ToString(int.Parse(textBox1.Text) + int.Parse(textBox2.Text)); } //Fixing Form Action private void FixFormAction () { 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(AjaxWebPart), "UpdatePanelFixup", "_spOriginalFormAction = document.forms[0].action; _spSuppressFormOnSubmitWrapper=true;", true); } } //Class for Building progress tempales public class ProgressTemplate : ITemplate { private string template; public ProgressTemplate(string temp) { template = temp; } public void InstantiateIn(Control container) { LiteralControl ltr = new LiteralControl(this.template); container.Controls.Add(ltr); } } }
然后构建解决方案并将 Web 部件从 Visual Studio 部署到您的门户。 如您所见,这是一个简单的 Web 部件,用于计算两个整数的和。
请确保 Visual Studio 已重新启动您的 IIS,然后浏览 MOSS 门户并从 Web 部件库添加 AjaxWebPart
。