Introduction
More than once I have request from Site Owner and Managers to add new Option and link without make any change in MasterPage or other Out of the Box page, for this example want to show how can use SharePoint Solution to add Jauot; in Feature to inject HTML code and add custom Links without change any file.
Global Link in a Web Part Form.
The first part of this task was to identify where I can add a Link and
The first thing we need to do is to identify where is the “Welcome Control” and get some identification of the control like a “Id”, since this controls generate dynamic ID, this type of ID are not trustable, to ensure a good attribute
that define the "Welcome Control" started to search in the Class and found CSS attribute “ms-welcomeMenu” as identifier, very suggestive indeed. :)
To identify the Html references in Internet Explorer we can use the Developer Tools accessed by (F12).
After identify the Class Name from ”Welcome Control”, was created a class to get the ClassName “ms-welcomeMenu” in the page and then inject html
to add the new Link.
This JavaScript will be executed after the page is load to ensure the Class already exists in the SharePoint Page.
Code example:
<script type=
"text/javascript"
>
ExecuteOrDelayUntilScriptLoaded(LoadLink,
"sp.js"
);
function
LoadLink() {
function
getByClass(className, parent) {
parent || (parent = document);
var
descendants = parent.getElementsByTagName(
'*'
), i = -1, e, result = [];
while
(e = descendants[++i]) {
((
' '
+ (e[
'class'
] || e.className) +
' '
).indexOf(
' '
+ className +
' '
) > -1) && result.push(e);
}
return
result;
}
var
myMenuContainer = getByClass(
'ms-welcomeMenu'
)[0].parentNode;
var
newSpan = document.createElement(
"span"
);
newSpan.innerHTML =
'<a class="ms-menu-a" style="cursor:pointer;white-space:nowrap;" href="#" onclick="window.location=\'http://www.sharepointpt.org/\';return false;"><span>SharePoint PT Web Part</span></a>'
;
newSpan.className =
'ms-SPLink ms-SpLinkButtonInActive ms-welcomeMenu'
;
myMenuContainer.insertBefore(newSpan, myMenuContainer.children[1]);
}
</script>
http://www.sharepointpt.org/\';return false;"><span>SharePoint PT Web Part</span></a>';
newSpan.className =
'ms-SPLink ms-SpLinkButtonInActive ms-welcomeMenu'
;
myMenuContainer.insertBefore(newSpan, myMenuContainer.children[1]);
& style="background-color:whitesmoke;"><noscript>
Your browser does not support JavaScript!
</noscript>
After the JavaScript code is created we can make test using a “Form” Web Part and inject the JavaScript in the current Page.
After inject the JavaScript code in the Web Part you can refresh the page and wait if the Link will appear in the top Right of the SharePoint Page.
If
this page shows the new link, then we can confirm the JavaScript is working and we can use in the following Methods to add the Link as Global.
Delegate Control
Delegates control gives the ability to Administrator to add custom Controls and change Layout and add custom controls without changing Themes or Master Pages in all SharePoint Site without administrator manage Features.
But for this example the attribute "AdditionalPageHead", this control allow add multiple user controls without affecting the existing Out of the Box Delegate Controls.
This control is more special then the Out of the box because one property “AllowMultipleControls". This property allows add multiple Controls
without affecting the existing ones.
Some Out of the Box Delegate Controls:
- AdditionalPageHead - Delegate control allocated in top site – AllowMultipleControls
- GlobalSiteLink0
- GlobalSiteLink1
- GlobalSiteLink2
- SmallSearchInputBox
- TopNavigationDataSource
- PublishingConsole
- QuickLaunchDataSource
Create a SharePoint Solution in the Visual Studio 2010 and create a Feature where is defined the Scope where the solution will be deployed and add the Mapping “ControlTemplates” for the User Control where the Delegate Control will
use to add the custom Controls and actions in the SharePoint Sites.
Add a new User Control “CustomHeader.ascx” where you can add controls, Html and inject JavaScript.
To customize your Solution needs to Add a Elements.XML file where you configuration of the Delegate Control ID as “AdditionalPageHead” and have the reference to the User Control where will support the additional Header of the SharePoint Masterpage.
<!--Add Global Link SharePoint PT using Custom Action-->
<
Control
Id
=
"AdditionalPageHead"
Sequence
=
"100"
ControlSrc
=
"~/_CONTROLTEMPLATES/CustomHeader/CustomHeader.ascx"
></
Control
>
</
Elements
>
After the Elements file is configured with the Delegate Control, the User control is created and the Feature is configured, we can install the SharePoint solution in the SharePoint Farm and activate Feature.
If we access to the Html code added in the SharePoint Page using Delegate Control ID “AdditionalPageHead”
Custom Action
The second method can be used to configure the global Link using Custom Actions, this option allow the IT Administrator to inject custom JavaScript code or files without changing MasterPage or other Out of the Box file.
To create this new Action needs to add a new mapping call “Layouts” where will store a JavaScript File where will be the custom Code.
After the Mapping for Layouts added, you can create a JS file where is stored the JavaScript actions to insert the global Link.
After the Mapping for Layouts added, you can create a JS file where is stored the JavaScript actions to insert the global Link.
To customize this Solution needs to Add in Elements.XML file the Custom Action with location “Location=’ScriptLink’"
this attribute will inject in the Masterpage the reference to JavaScript file where is stored.
<!--Add Global Link SharePoint PT using Custom Action-->
<
Control
Id
=
"AdditionalPageHead"
Sequence
=
"100"
ControlSrc
=
"~/_CONTROLTEMPLATES/CustomHeader/CustomHeader.ascx"
></
Control
>
<!--Add Global Link SharePoint PT 2 using Custom Action-->
<
CustomAction
ScriptSrc
=
"~SiteCollection/_Layouts/CustomHeader/GlobalLink.js"
Location
=
"ScriptLink"
Sequence
=
"10"
>
</
CustomAction
>
</
Elements
>
After the Elements file is configured with the Custom Action, the reference to the JavaScript File and Location ”ScriptLink” where makes reference to MasterPage, we can install the SharePoint solution in the SharePoint
Farm and activate Feature associated.
Example of the Html code added in the SharePoint Page using “Custom Action”, the MasterPage makes the reference to the JavaScript file that is executed every time the page is loaded.
Conclusion
This is some of the actions you can use to update you SharePoint Sites without changing MasterPage and without interaction of the Site Owners, turning into a very flexible option for to Manage SharePoint Sites for Global Operations.
Support links:
Project example:
Link
Delegate Control
http://msdn.microsoft.com/en-us/library/ms463169.aspx
How to: Customize a Delegate Control
http://msdn.microsoft.com/en-us/library/ms470880.aspx
AllowMultipleControls
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.webcontrols.delegatecontrol.aspx
CustomAction Element
Delegate Control
http://msdn.microsoft.com/en-us/library/ms463169.aspx
How to: Customize a Delegate Control
http://msdn.microsoft.com/en-us/library/ms470880.aspx
AllowMultipleControls
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.webcontrols.delegatecontrol.aspx
http://msdn.microsoft.com/en-us/library/ms460194.aspx
Custom Action Definition Schema
http://msdn.microsoft.com/en-us/library/ms465980.aspx
Default Custom Action Locations and IDs
http://msdn.microsoft.com/en-us/library/bb802730.aspx
How to: Modify the User Interface Using Custom Actions
http://msdn.microsoft.com/en-us/library/ms473643(v=office.14).aspx
HideCustomAction Element
http://msdn.microsoft.com/en-us/library/ms414790.aspx
André Lage
http://aaclage.blogspot.com/