[SharePoint 2010] 自定义Action开发
需求:创建带Code-Behind的网站页面,并通过Action在sharepoint中显示。
实现:
1.创建带Code-Behind的sharepoint网站页面。
新建空白sharepoint项目,在项目中添加模块CustomPages。将Sample.txt修改为MyTemplate.aspx。添加以下内容。
<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %> <%@ Import Namespace="Microsoft.SharePoint.ApplicationPages" %> <%@ Register TagPrefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> <%@ Register TagPrefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> <%@ Register TagPrefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %> <%@ Import Namespace="Microsoft.SharePoint" %> <%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> <%@ Page Language="C#" CodeBehind="MyPageTemplate.aspx.cs" Inherits="SampleToDevelopAPage.MyPageTemplate, $SharePoint.Project.AssemblyFullName$" MasterPageFile="~masterurl/default.master" Title="Testing This Page" meta:progid="SharePoint.WebPartPage.Document" %> <asp:Content ID="Content1" runat="server" ContentPlaceHolderID="PlaceHolderMain"> <asp:Button runat="server" ID="btn" Text="TimeClick" OnClick="Button_Click" /> <asp:Label runat="server" ID="lbl" /> </asp:Content>
接下来需要创建Code-Behind代码,在CustomPages文件夹右键,添加类MyPageTemplate.aspx.cs。在类中添加以下内容。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web.UI.WebControls; using Microsoft.SharePoint.WebControls; using Microsoft.SharePoint.WebPartPages; using Microsoft.SharePoint; namespace SampleToDevelopAPage { public class MyPageTemplate:WebPartPage { protected Button btn; protected Label lbl; protected void Page_Load(object sender, EventArgs e) { } protected void Button_Click(object sender, EventArgs e) { lbl.Text = System.DateTime.Now.ToLongTimeString(); } } }
接着我们需要修改模块CustomPages下的Elements.xml文件。
<?xml version="1.0" encoding="utf-8"?> <Elements xmlns="http://schemas.microsoft.com/sharepoint/"> <Module Name="CustomPages"> <File Url="MyPageTemplate.aspx" Path="CustomPages\MyPageTemplate.aspx" Name="Sample1.aspx" Type="Ghostable" /> <File Url="MyPageTemplate.aspx" Path="CustomPages\MyPageTemplate.aspx" Name="Sample2.aspx" Type="Ghostable" /> <File Url="MyPageTemplate.aspx" Path="CustomPages\MyPageTemplate.aspx" Name="Sample3.aspx" Type="Ghostable" /> </Module> </Elements>
部署之后,可通过浏览器访问页面,我这里的访问地址是http://mydemo/sample1.aspx。
2.在sharepoint UI中添加链接。下面分三种方式添加。
参考资料:http://msdn.microsoft.com/zh-cn/library/bb802730.aspx
CustomAction:http://msdn.microsoft.com/zh-cn/library/ms460194.aspx
CustomActionGroup:http://msdn.microsoft.com/zh-cn/library/ms438085.aspx
A.在网站操作中添加,在Elements.xml中的Elements节点内添加以下内容。
<CustomAction Description="位于网站操作下的链接" GroupId="SiteActions" Id="MySiteActionLink1" Location="Microsoft.SharePoint.StandardMenu" Sequence="10" Title="MyCustomAction" ImageUrl="/_layouts/images/siteIcon.png"> <UrlAction Url="{SiteUrl}/Sample1.aspx" /> </CustomAction>
B.在网站设置中创建分组,并将链接放置在自定义的分组内。
CustomAction中的GroupId和CustiomActionGroup的Id相同。
<CustomActionGroup Description = "自定义的分组" Id = "MyCustomGroup" Location = "Microsoft.SharePoint.SiteSettings" Sequence = "35" Title = "My Custom Group" ImageUrl="/_layouts/images/siteIcon.png"> </CustomActionGroup> <CustomAction Description="自定义分组内的链接" GroupId="MyCustomGroup" Id="MyCustomGroup.MySiteAction1" Location="Microsoft.SharePoint.SiteSettings" Sequence="10" Title="MyCustomAction"> <UrlAction Url="{SiteUrl}/Sample2.aspx" /> </CustomAction>
C.在网站操作中创建分组。
通过B的方式,无法在SiteActions中添加新的分组,尝试采用下面的方式。
在项目中添加类MyCustomGroupMenu.cs,该类继承至System.Web.UI.WebControls.WebControl。在类中需要重写CreateChildControls。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.SharePoint; using Microsoft.SharePoint.WebControls; namespace SampleToDevelopAPage { public class MyCustomGroupMenu : System.Web.UI.WebControls.WebControl { protected override void CreateChildControls() { SubMenuTemplate subMenu = new SubMenuTemplate(); subMenu.Text = "My Custom Action Group"; subMenu.Description = "位于网站操作下的分组"; subMenu.ImageUrl = "/_layouts/images/siteIcon.png"; for (int i = 0; i < 2; i++) { MenuItemTemplate menuItem = new MenuItemTemplate(); menuItem.Text = "My Sample" + (i + 1); menuItem.Description = "My Custom page"; menuItem.ClientOnClickNavigateUrl = string.Format("/Sample{0}.aspx", (i + 1)); menuItem.ImageUrl = "/_layouts/images/siteIcon.png"; subMenu.Controls.Add(menuItem); } this.Controls.Add(subMenu); base.CreateChildControls(); } } }
接下来在Elements.xml中添加内容。
注意属性ControlAssmbly和ControlClass
<CustomAction Id="MyCustomGroupMenu" Description="位于网站操作下的分组" Location="Microsoft.SharePoint.StandardMenu" GroupId="SiteActions" Title="My Custom Group Menu" Sequence="45" ImageUrl="/_layouts/images/siteIcon.png" ControlAssembly="$SharePoint.Project.AssemblyFullName$" ControlClass="SampleToDevelopAPage.MyCustomGroupMenu" > </CustomAction>
现在部署之后,在网站操作里面还是没有显示自定义分组,我们需要将类注册为安全类型。
在模块CustomPages属性中,添加安全控制项。
部署解决方案。
结束。
本文版权归菜鸟和博客园共有,欢迎转载,但请注明出处。