delegate control 是SharePoint 中一些可扩展控件,通常我们可以把这些控件当做占位符来使用,因为我们可以轻松通过custom Elements来扩展它。
本文以常用delegate control AdditionalPageHead 为例讲述在SharePoint 2013 中使用 delegate control 要注意的事项。
通常我们扩展AdditionalPageHead 的步骤是这样的:
1. 新建一个SharePoint 的farm solution
2. 新建一个User control,用于扩展AdditionalPageHead
3.在这个control里写写上要实现的代码, 比如用JavaScript 输出一个Hello World
<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %> <%@ Assembly Name="Microsoft.Web.CommandUI, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> <%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> <%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=15.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" %> <%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="AdditionalPageHead.ascx.cs" Inherits="SocialTest.ControlTemplates.SocialTest.AdditionalPageHead" %> <script type="text/javascript"> alert("Hello World"); </script>
4.新建一个Custom Element:
<?xml version="1.0" encoding="utf-8"?> <Elements xmlns="http://schemas.microsoft.com/sharepoint/"> <Control Id="AdditionalPageHead" Sequence="100" ControlSrc="~/_CONTROLTEMPLATES/15/SocialTest/AdditionalPageHead.ascx"> </Control> </Elements>
这里需要注意的是一定要带上15这个目录夹,否则SharePoint 2013 会找不到AdditionalPageHead.ascx。
也不知道微软是不是故意的,如果不带15, SharePoint 2013 默认还会去14目录搜索AdditionalPageHead.ascx
部署这个解决方案后,效果就是每个页面(只要对应的master page里面包含了delegate control AdditionalPageHead )都会弹出Hello World
SharePoint 2013 中使用 delegate control AdditionalPageHead 的注意事项