有時候咱們須要開發自定義的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.