有时候咱们须要开发自定义的SearchBox来代替Sharepoint网站的MasterPage默认的SearchBox,这就需html
要用到的Delegate Controls相关知识。Sharepoint的Delegate Control就像一个容器,它封装了各类默认的控件在里面。这些控件能够被用户开发的自定义控件进行替换,从而达到用户自定义的功能和效果。web
SharePoint 提供了很多的delegate controls:app
AdditionalPageHead
GlobalSiteLink0
GlobalSiteLink1
GlobalSiteLink2
PublishingConsole
QuickLaunchDataSource
SmallSearchInputBox
TopNavigationDataSourceide
下面是Sharepoint Fundation的MasterPage定义的一些Delegate Controlspost
<SharePoint:DelegateControl runat="server" ControlId="AdditionalPageHead"
AllowMultipleControls="true"/>
<SharePoint:DelegateControl runat="server" ControlId="GlobalNavigation" />
<SharePoint:DelegateControl runat="server" ID="GlobalDelegate0" ControlId="GlobalSiteLink0" />
<SharePoint:DelegateControl ControlId="GlobalSiteLink2" ID="GlobalDelegate2" Scope="Farm"
runat="server" />
<SharePoint:DelegateControl runat="server" ControlId="PublishingConsole"
Id="PublishingConsoleDelegate">
</SharePoint:DelegateControl><SharePoint:DelegateControl ControlId="GlobalSiteLink3" Scope="Farm"
runat="server" />
<SharePoint:DelegateControl runat="server" ControlId="SmallSearchInputBox" Version="4" />
<SharePoint:DelegateControl runat="server" ControlId="TopNavigationDataSource"
Id="topNavigationDelegate"/>测试
上面所列举的Delegate controls能够在运行时被用户自定义的控件或Feature动态替换。咱们这里所须要实现的建立自定义SearchBox功能就须要经过用咱们自行开发的MySearchBox用户控件代替 Small Search Input box这个delegate control来实现。网站
让咱们来看一看delegate control 的XML schemaui
<?xml version="1.0" encoding="utf-8" ?> this
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">spa
<Control Id="SmallSearchInputBox" Sequence="100" ControlSrc="/templates/mysearchcontrol.ascx"/>
</Elements>
从上面的schema,咱们能够看出,delegate control的重要属性包括 Control Id, Sequence 和 ControlSrc.
Control Id 是咱们用来识别Delegate control的
Sequence number 是咱们用来定位Delegate control的等级的,它的值越小则等级越高。
ControlSrc 则用来指明Delegate Control的控件资源的位置的。
关于SequenceNumber咱们能够进一步用图示说明
在上图的示例中,针对同一Delegate control咱们定义了3个用户控件并经过3个Feature(A,B,C)来启用它们,但注意,它们有不一样的Sequence(A>C>B),全部这3个user control都会被用来代替那个指定的Delegate Control,但因为Feature B的Sequence Number最小,因此,Feature B中的usercontrol在Runtime的时候会取得最终的代替权,并被Render到Page中呈现出来。可是,当Feature B被中止时,Feature C就会顶替上来,由于Feature C中的Sequence仅次于Feature B,当Feature B被中止后,Feature C就成了最小的了,因此,它就会取得最终的胜利。
下面咱们还列出了一些在Sharepoint的LAYOUTS目录下用到了Delegate Control页面。也就是说,当你修改了某些Delegate Control时,下面的某些用到了此Delegate Control的页面也会受到影响。
AddNavigationLinkDialog.aspx: ControlId=”AddNavigationLinkDialogPanel1″ Scope=”Site”
AddNavigationLinkDialog.aspx: ControlId=”AddNavigationLinkDialogPanel2″ Scope=”Web”
AreaNavigationSettings.aspx: ControlId=”NavigationSettingsPanel1″ Scope=”Site”
AreaNavigationSettings.aspx: ControlId=”NavigationSettingsPanel2″ Scope=”Web”
BackLinks.aspx: ControlId=”SmallSearchInputBox”
ExcelProfilePage.aspx: ControlId=”VariationsFlagControl”
ExcelProfilePage.aspx: ControlId=”GlobalSiteLink1″ Scope=”Farm”
ExcelProfilePage.aspx: ControlId=”GlobalSiteLink2″ Scope=”Farm”
groups.aspx: ControlId=”QuickLaunchDataSource”
listcontentsources.aspx: ControlId=”QuickLaunchDataSource”
listservernamemappings.aspx: ControlId=”QuickLaunchDataSource”
logsummary.aspx: ControlId=”QuickLaunchDataSource”
logviewer.aspx: ControlId=”QuickLaunchDataSource”
managecrawlrules.aspx: ControlId=”QuickLaunchDataSource”
managefiletypes.aspx: ControlId=”QuickLaunchDataSource”
manageprivacypolicy.aspx: ControlId=”QuickLaunchDataSource”
manageservicepermissions.aspx: ControlId=”QuickLaunchDataSource”
mycontactlinks.aspx: ControlId=”ColleaguesLink1″ Scope=”Farm”
newsbweb.aspx: ControlId=”CreateSitePanel1″ Scope=”Site”
people.aspx: ControlId=”QuickLaunchDataSource”
personalsites.aspx: ControlId=”QuickLaunchDataSource”
profmain.aspx: ControlId=”QuickLaunchDataSource”
quicklinks.aspx: ControlId=”AddColleaguesLink1″ Scope=”Farm”
regionalsetng.aspx: ControlId=”RegionalSettingsExtensions”
schema.aspx: ControlId=”QuickLaunchDataSource”
scsignup.aspx: ControlId=”CreateSiteCollectionPanel1″ Scope=”Farm” />
searchsspsettings.aspx: ControlId=”QuickLaunchDataSource”
SiteManager.aspx: ControlId=”GlobalSiteLink1″ Scope=”Farm”
SiteManager.aspx: ControlId=”GlobalSiteLink2″ Scope=”Farm”
user.aspx: ControlId=”QuickLaunchDataSource”
userdisp.aspx: ControlId=”ProfileRedirection” Scope=”Farm”
VersionDiff.aspx: ControlId=”SmallSearchInputBox”
viewlsts.aspx: ControlId=”QuickLaunchDataSource”
viewscopes.aspx: ControlId=”QuickLaunchDataSource”
XLViewer.aspx: ControlId=”GlobalSiteLink1″ Scope=”Farm”
XLViewer.aspx: ControlId=”GlobalSiteLink2″ Scope=”Farm”
有了上面的知识准备,回到本文的目标,建立一个用户自定义的SearchBox。
首先建立一个新的项目,在此项目里添加一个新的用户控件,在此用户控件中拖放一个日历控件,咱们就用此日历控件来示例如何代替默认的SearchBox控件。
项目以下图
而后,咱们须要在系统中添加一个Feature,命名此Feature以下
在此Feature中,咱们定义了Scope的范围是站点的级别的,那就意味着你整个站点页面中的搜索控件都会被替换为你本身定制的样式。
接下来,咱们须要新添加一个Elment.xml,以下图。
须要注意的是,Sequence属性的值必定要小于默认提供的,这个Sequence的值在wss中是100,在moss标准版中是50,企业版是25。
在MOSS的安装目录下搜索指定feature中对应的xml文件,打开它能够看到里面所定义的Sequence值(名字:SearchArea.xml)。
最后,编译部署,打开咱们的测试网站能够看到效果以下图.
原文连接:http://www.cnblogs.com/wsdj-ITtech/archive/2011/11/26/2263041.html
SharePoint2013新增长了几个Delegate Control,现介绍以下:
In this post we’ll take a quick look at some of the new DelegateControls I’ve discovered for SharePoint 2013 and how you can replace or add information to your new master pages using these new controls, without modifying the master pages. This is done exactly the same way as you would do it back in the 2010 projects (and 2007), the only addition in this case are a few new controls that we’ll investigate.
Searching through the main master page, Seattle.master, I’ve found these three new DelegateControls:
PromotedActions
SuiteBarBrandingDelegate
SuiteLinksDelegate
So let’s take a look at where these controls are placed on the Master page and how we can replace them.
The PromotedActions delegate control allows you to add your own content to the following area on a SharePoint site in the top-right section of the page:
An example of adding an additional link may look like this:
So what does the files look like for these parts of the project?
1
2
3
4
5
6
7
8
9
|
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
Elements
xmlns
=
"http://schemas.microsoft.com/sharepoint/"
>
<!-- DelegateControl reference to the PromotedActions Delegate Control -->
<
Control
ControlSrc
=
"/_controltemplates/15/Zimmergren.DelegateControls/PromotedAction.ascx"
Id
=
"PromotedActions"
Sequence
=
"1"
/>
</
Elements
>
|
1
2
3
4
5
6
7
|
<!-- Note: I've removed the actual Facebook-logic from this snippet for easier overview of the structure. -->
<
a
title
=
"Share on Facebook"
class
=
"ms-promotedActionButton"
style
=
"display: inline-block;"
href
=
"#"
>
<
span
class
=
"s4-clust ms-promotedActionButton-icon"
style
=
"width: 16px; height: 16px; overflow: hidden; display: inline-block; position: relative;"
>
<
img
style
=
"top: 0px; position: absolute;"
alt
=
"Share"
src
=
"/_layouts/15/p_w_picpaths/Zimmergren.DelegateControls/facebookshare.png"
/>
</
span
>
<
span
class
=
"ms-promotedActionButton-text"
>Post on Facebook</
span
>
</
a
>
|
This DelegateControl will allow you to override the content that is displayed in the top-left corner of every site. Normally, there’s a text reading "SharePoint" like this:
If we override this control we can easily replace the content here. For example, most people would probably like to add either a logo or at least make the link clickable so you can return to your Site Collection root web. Let’s take a look at what it can look like if we’ve customized it (this is also a clickable logo):
So what does the files look like for this project?
1
2
3
4
5
6
7
8
9
|
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
Elements
xmlns
=
"http://schemas.microsoft.com/sharepoint/"
>
<!-- SuiteBarBrandingDelegate (the top-left "SharePoint" text on a page) -->
<
Control
ControlSrc
=
"/_controltemplates/15/Zimmergren.DelegateControls/SuiteBarBrandingDelegate.ascx"
Id
=
"SuiteBarBrandingDelegate"
Sequence
=
"1"
/>
</
Elements
>
|
This is the only content in my User Control markup:
1
|
<
div
class
=
"ms-core-brandingText"
id
=
"BrandingTextControl"
runat
=
"server"
/>
|
1
2
3
4
5
6
7
8
9
10
|
protected
void
Page_Load(
object
sender, EventArgs e)
{
BrandingTextControl.Controls.Add(
new
Literal
{
Text =
string
.Format(
"<a href='{0}'><img src='{1}' alt='{2}' /></a>"
,
SPContext.Current.Site.Url,
"/_layouts/15/p_w_picpaths/Zimmergren.DelegateControls/tozit36light.png"
,
SPContext.Current.Site.RootWeb.Title)
});
}
|
The SuiteLinksDelegate control will allow us to modify the default links, and to add our own links, in the "suit links" section:
By adding a custom link to the collection of controls, it can perhaps look like this:
What does the project files look like for modifying the SuiteLinksDelegate? Well, here’s an example:
1
2
3
4
5
6
7
8
9
|
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
Elements
xmlns
=
"http://schemas.microsoft.com/sharepoint/"
>
<!-- DelegateControl reference to the SuiteLinksDelegate Delegate Control -->
<
Control
ControlSrc
=
"/_controltemplates/15/Zimmergren.DelegateControls/SuiteLinksDelegate.ascx"
Id
=
"SuiteLinksDelegate"
Sequence
=
"1"
/>
</
Elements
>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
public
partial
class
SuiteLinksDelegate : MySuiteLinksUserControl
{
protected
override
void
Render(HtmlTextWriter writer)
{
writer.RenderBeginTag(HtmlTextWriterTag.Style);
writer.Write(
".ms-core-suiteLinkList {display: inline-block;}"
);
writer.RenderEndTag();
writer.AddAttribute(HtmlTextWriterAttribute.Class,
"ms-core-suiteLinkList"
);
writer.RenderBeginTag(HtmlTextWriterTag.Ul);
// The true/false parameter means if it should be the active link or not - since I'm shooting off this to an external URL, it will never be active..
RenderSuiteLink(writer,
"http://timelog.tozit.com"
,
"Time Report"
,
"ReportYourTimeAwesomeness"
,
false
);
writer.RenderEndTag();
base
.Render(writer);
}
}
|
For reference: I’ve structured the project in a way where I’ve put all the changes into one single Elements.xml file and they’re activated through a Site Scoped feature called DelegateControls. The solution is a Farm solution and all artifacts required are deployed through this package.