I'm using SharePoint 2007 and I would like to add the JS code for Google Analytics into the master page. I think a Delegate Control is the way to go, as we already have a Delegate Control in the Head section of the master page.
However, in this case there is a requirement to enable and disable the code per site, so would this still be the best approach?
I have put following code in .ascx file and writing in body tag instead of head tag.
protected override void Render(HtmlTextWriter writer) {
//base.Render(writer);
if (SPContext.Current != null) {
SPFile file = SPContext.Current.File;
if (file.Item != null) {
if (file.Item.Fields.ContainsField(KeywordKey) &&
file.Item[KeywordKey] != null &&
file.Item[KeywordKey].ToString() != string.Empty) {
writer.Write(string.Format(@"<META name=""keywords"" content=""{0}"">", file.Item[KeywordKey].ToString()));
}
if (file.Item.Fields.ContainsField(DescriptionKey) &&
file.Item[DescriptionKey] != null &&
file.Item[DescriptionKey].ToString() != string.Empty) {
writer.Write(string.Format(@"<META name=""description"" content=""{0}"">", file.Item[DescriptionKey].ToString()));
}
}
}
}
i would like to write meta keywords on head section instead of body tags.
Thanks Ronak
Here's the Elements.xml
<?xml version="1.0" encoding="utf-8" ?>
<Elements xmlns="schemas.microsoft.com/sharepoint/">;
<!-- Use a sequence number lower than default of 100, so the custom control gets loaded before SharePoint controls -->
<Control Id="AdditionalPageHead" Sequence="50" ControlSrc="~/_ControlTemplates/PhilaGov/MetaTagsKeyWords.ascx" />
</Elements>
Here's the Head section of Master Page
<HEAD runat="server">
<META Name="GENERATOR" Content="Microsoft SharePoint">
<META Name="progid" Content="SharePoint.WebPartPage.Document">
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8">
<META HTTP-EQUIV="Expires" content="0">
<SharePoint:RobotsMetaTag runat="server"/>
<Title ID=onetidTitle><asp:ContentPlaceHolder id=PlaceHolderPageTitle runat="server"/></Title>
<SharePoint:CssLink runat="server"/>
<SharePoint:Theme runat="server"/>
<SharePoint:ScriptLink language="javascript" name="core.js" Defer="true" runat="server"/>
<SharePoint:CustomJSUrl runat="server"/>
<SharePoint:SoapDiscoveryLink runat="server"/>
<asp:ContentPlaceHolder id="PlaceHolderAdditionalPageHead" runat="server"/>
<SharePoint:DelegateControl runat="server" ControlId="AdditionalPageHead" AllowMultipleControls="true"/>
</HEAD>
UPDATE: Example page layout:
<%@ Page language="C#" Inherits="Microsoft.SharePoint.Publishing.PublishingLayoutPage,Microsoft.SharePoint.Publishing,Version=12.0.0.0,Culture=neutral,PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="SharePointWebControls" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="PublishingWebControls" Namespace="Microsoft.SharePoint.Publishing.WebControls" Assembly="Microsoft.SharePoint.Publishing, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="PublishingNavigation" Namespace="Microsoft.SharePoint.Publishing.Navigation" Assembly="Microsoft.SharePoint.Publishing, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<asp:Content ContentPlaceholderID="PlaceHolderAdditionalPageHead" runat="server">
<PublishingWebControls:editmodepanel runat="server" id="editmodestyles">
<!-- Styles for edit mode only-->
<SharePointWebControls:CssRegistration name="<% $SPUrl:~sitecollection/Style Library/~language/Core Styles/zz2_editMode.css %>" runat="server"/>
</PublishingWebControls:editmodepanel>
<SharePointWebControls:CssRegistration name="<% $SPUrl:~sitecollection/Style Library/~language/Core Styles/rca.css %>" runat="server"/>
<PublishingWebControls:editmodepanel runat="server" id="displaymodejs" PageDisplayMode="Display">
<!-- JavaScript for to remove OverViewbox if its empty-->
<script type="text/javascript" language="javascript">
$(document).ready(function () {
$(".overviewBox>div").filter(function (index) { return $(this).children().length < 1; }).parent().remove();
});
</script>
</PublishingWebControls:editmodepanel>
</asp:Content>
<asp:Content ContentPlaceholderID="PlaceHolderPageTitle" runat="server">
<SharePointWebControls:FieldValue id="PageTitle" FieldName="Title" runat="server"/>
</asp:Content>
<asp:Content ContentPlaceholderID="PlaceHolderMain" runat="server">
<div class="widget breadcrumb">
<asp:SiteMapPath runat="server" id="SiteMapPath1" SkipLinkText="" SiteMapProvider="CurrentNavSiteMapProviderNoEncode"></asp:SiteMapPath>
</div>
<div class="widget pageTitle">
<h1>
<SharePointWebControls:FieldValue id="HomePageTitleInTitleArea" FieldName="Title" runat="server"/></h1>
</div>
<div class="grid_15 colRight">
<div class="widget overviewBox">
<PublishingWebControls:RichHtmlField FieldName="OverViewContent1" runat="server"
InputFieldLabel="Content">
</PublishingWebControls:RichHtmlField>
</div>
<div class="widget WebPartEdit">
<PublishingWebControls:RichHtmlField FieldName="PublishingPageContent1" runat="server"
InputFieldLabel="Content">
</PublishingWebControls:RichHtmlField>
</div>
<div class="widget overviewBox">
<PublishingWebControls:RichHtmlField FieldName="OverViewContent2" runat="server"
InputFieldLabel="Content">
</PublishingWebControls:RichHtmlField>
</div>
<div class="widget WebPartEdit">
<PublishingWebControls:RichHtmlField FieldName="PublishingPageContent2" runat="server"
InputFieldLabel="Content">
</PublishingWebControls:RichHtmlField>
</div>
<WebPartPages:WebPartZone runat="server" Title="BottomZone" ID="BottomZone"><ZoneTemplate></ZoneTemplate></WebPartPages:WebPartZone>
<PublishingWebControls:editmodepanel runat="server" id="editmodepanel1">
<!-- Add field controls here to bind custom metadata viewable and editable in edit mode only.-->
<SharePointWebControls:NoteField FieldName="MetaKeywords" runat="server"></SharePointWebControls:NoteField>
<SharePointWebControls:NoteField FieldName="MetaDescription" runat="server"></SharePointWebControls:NoteField>
</PublishingWebControls:editmodepanel>
</div>
<div class="grid_5 colLeft">
<WebPartPages:WebPartZone runat="server" Title="LeftZone" ID="LeftZone"><ZoneTemplate></ZoneTemplate></WebPartPages:WebPartZone>
</div>
</asp:Content>