locked
Meta tags in MOSS 2007 master pages RRS feed

  • Question

  • Does anyone know how I might go about inserting meta tags such as "description" and "keywords" into my master pages and page layouts in a MOSS 2007 external facing site?

    Ideally, when an author or editor is editing a page, I want field controls for the meta description and keyword tags, so the author can fill those in and save those with the page content.  The problem is that I can't put a server control like a contentplaceholder inside of the content="" attribute on a meta tag.  I've tried the following:

    <meta name="description" content="<asp:ContentPlcaHolder ID="PlaceHolderMetaDescription" runat="server"></asp:ContentPlaceHolder>" />

    The SharePoint designer doesn't like that at all, and the stuff in the content attribute just gets html encoded.  following don't work either:

    <meta name="description" content='<asp:ContentPlaceHolder ID="PlaceHolderMetaDescription" runat="server"></asp:ContentPlaceHolder>' />

    <meta name="description" content="<asp:ContentPlaceHolder ID='PlaceHolderMetaDescription' runat='server'></asp:ContentPlaceHolder>" />

    <meta name="description" ID="metaDescription" runat="server" content='<asp:ContentPlaceHolder ID='PlaceHolderMetaDescription' runat='server'></asp:ContentPlaceHolder>' />

    Anybody have any ideas how I can get this to work?

    Wednesday, January 31, 2007 4:46 PM

Answers

  • I havent yet tried anything similar in MOSS 2007, but are probably going to in the new 6 months.

    My approach would be that alle my pagelayouts were using content types that derived from a common base content type, that included columns fro meta-description and derived from page.

    Then i would build a webcontrol that checked if the page in context were using a content type that derived from my base content type.

    If that was the case i would simply output the meta data in the render method.

    Cheers

    Wednesday, January 31, 2007 5:20 PM
  • Hello Adam,

    My approach would be to create a special render control and generate the required HTML code via the control.

    1) Create a render control and override the RenderFieldForDisplay method. For an example, see http://msdn2.microsoft.com/en-us/library/aa981226.aspx. This article shows also how to create the custom edit mode but you don't need all of it.
    Deploy the custom control as mentioned in the article.

    2) Add the contentplaceholder in the master page (as you already tried), or use the default <asp:ContentPlaceHolder id="PlaceHolderAdditionalPageHead" runat="server"/>
    Place this in the <head> section, right after the <title> tag for example.

    3) Call the custom control to render the meta description field in the page layout:

    <asp:Content ContentPlaceholderID="PlaceHolderAdditionalPageHead" runat="server">

     <PublishingWebControls:EditModePanel runat="server" id="displayModeMetadata" PageDisplayMode="Display"  SuppressTag="true">
      <Mytag:HeadMetadataFieldControl FieldName="MyMetadatafieldname" runat="server"></Mytag:HeadMetadataFieldControl>
     </PublishingWebControls:EditModePanel>

    </asp:Content>

    Hope this is clear enough.

    Monday, February 12, 2007 9:31 AM

All replies

  • I havent yet tried anything similar in MOSS 2007, but are probably going to in the new 6 months.

    My approach would be that alle my pagelayouts were using content types that derived from a common base content type, that included columns fro meta-description and derived from page.

    Then i would build a webcontrol that checked if the page in context were using a content type that derived from my base content type.

    If that was the case i would simply output the meta data in the render method.

    Cheers

    Wednesday, January 31, 2007 5:20 PM
  • Hi

    You can create columns (for metadata) add these to a (existing) publishing content type. Create a new page layout based on this contenttype and add the fields for example in the Edit Mode Panel (the description in code is - add field controls here to bind custom metadata viewable and editable in edit mode only)

    hope this is what you're looking after

    cheers, Joost

    Friday, February 2, 2007 9:06 AM
  • The edit mode panel is what I will use to house the editing controls that the author will use to input the content.

    My problem is more of an issue of how do I get the content physically inside of an html <meta> tag's "content=" attribute.  I don't want to make the author have to type the html for the meta tag in the edit mode panel, I would rather them type just the content, but have my page layout insert that content into the meta tag's "content=" attribute.

    Looks like a server control is the only way to go.

    Friday, February 2, 2007 3:24 PM
  • Hi Adam,

    What is your intention with using meta tags like "description" or "keywords" on an external facing site?
    If you plan to help search engines, this is probably not the best solution.

    As far as I know, commercial search engines do not really rely on and use these tags.

    Death Of A Meta Tag http://searchenginewatch.com/showPage.html?page=2165061
    Kereső optimalizálás - meta HTML elemmel? (Hungarian) http://weblabor.hu/blog/20050211/seometa

    Regards,
    Peter

    Friday, February 9, 2007 10:25 AM
  • Hello Adam,

    My approach would be to create a special render control and generate the required HTML code via the control.

    1) Create a render control and override the RenderFieldForDisplay method. For an example, see http://msdn2.microsoft.com/en-us/library/aa981226.aspx. This article shows also how to create the custom edit mode but you don't need all of it.
    Deploy the custom control as mentioned in the article.

    2) Add the contentplaceholder in the master page (as you already tried), or use the default <asp:ContentPlaceHolder id="PlaceHolderAdditionalPageHead" runat="server"/>
    Place this in the <head> section, right after the <title> tag for example.

    3) Call the custom control to render the meta description field in the page layout:

    <asp:Content ContentPlaceholderID="PlaceHolderAdditionalPageHead" runat="server">

     <PublishingWebControls:EditModePanel runat="server" id="displayModeMetadata" PageDisplayMode="Display"  SuppressTag="true">
      <Mytag:HeadMetadataFieldControl FieldName="MyMetadatafieldname" runat="server"></Mytag:HeadMetadataFieldControl>
     </PublishingWebControls:EditModePanel>

    </asp:Content>

    Hope this is clear enough.

    Monday, February 12, 2007 9:31 AM
  • Thanks for the great answer.  I'll give that a try.

    Adam

    Tuesday, February 13, 2007 12:25 AM
  • Hello Adam,

    I too am very interested in adding content for meta tags in my master pages. Could you please suggest me any ways of doing it. Am stuck with it for a very long time any help would be great.

    Lokanand
    Tuesday, March 6, 2007 2:00 PM
  • It seems that MOSS has no Tag for that.

    Create a UserControl Library. This user control library has to be registered in GAC and added as a safe control in the web.config ( see  http://blogs.tamtam.nl/mart/PermaLink,guid,a34071a0-ad27-44c7-aa78-3f956d6f920e.aspx)

    In the Library add a class (mine is KeywordsTag) and override the RenderControl Method as below:

    using System;

    using System.Web;

    using System.Web.UI;

    using System.Web.UI.WebControls;

    using System.Collections.Generic;

    using System.Text;

    using Microsoft.SharePoint;

    using System.Data;

    namespace MyControls

    {

    public class KeywordsTag: UserControl

    {

    public override void RenderControl(HtmlTextWriter writer)

    {

    //Get the sp file context and retrieve corresponding field

    try

    {

    SPFile file = SPContext.Current.File;

    String keywords = file.Item["Keywords"].ToString();

    //read the keywords field and render

    writer.Write("<meta name=\"keywords\" content=\"" + keywords + "\" />");

    }

    catch { }

    base.RenderControl(writer);

    }

    }

    }

     

    Build your user control library and add a Tag prefix in your master page.

    <%@ Register Tagprefix="MyTag" Namespace="MyControls" Assembly="AssemblyName, Version=1.0.0.0, Culture=neutral, PublicKeyToken=1b25d240d1396f63" %>

    Note: The Assembly Name , Version, Culture and PublicToken have to be changed to your Assembly Name , Version, Culture and PublicToken

    On your master page add the control before the title tag as below:

    <MyTag:KeywordsTag runat="server" />

    Your pages derived from the modified master page will show the meta keywords tag.

    This assumes that you are using the Field Named "Keywords" to store your keywords. If you are using a field like "mykeywords" change your user control accordingly.

     

     


    Tuesday, March 6, 2007 8:47 PM
  • Thanks a lot for that. Will try it out and keep you posted on it.

    Lokanand
    Wednesday, March 7, 2007 4:59 AM
  • Metatags can be more easily rendered using a method like this:

    In your master page:

    <asp:ContentPlaceHolder id="phMetaDescription" runat="server"></asp:ContentPlaceHolder>

    and this in the layout page:

    <asp:Content ContentPlaceholderID="phMetaDescription" runat="server">
    <asp:literal ID="meta1" runat="server" Text="&lt;meta name=&quot;description&quot; content=&quot;"/><SharePointWebControls:FieldValue id="MetaDescription" FieldName="Description" runat="server"/><asp:literal id="meta2" runat="server" Text="&quot;&gt;" />
    </asp:Content>

    Technically you do not need to use &lt; or &gt; for this solution, but it keeps it consistent - and will prevent Sharepoint Designer from highlighting the occurences of these characters as potential errors.

    Praggers
    • Proposed as answer by Praggers Monday, December 8, 2008 6:33 PM
    Monday, December 8, 2008 5:58 PM
  • See CodePlex WCM Utilities... contains the MetaTagsGenerator.
    -AC [MVP Office SharePoint Server] <> http://www.andrewconnell.com/blog
    Monday, December 22, 2008 5:20 AM
  • Hi Praggers,

    i try your method but the page show the error: Unknown server tag 'SharePointWebControls:FieldValue'. Can you explain more details about your method?. I newbie with sharepoint and I do not know about Asp.net, Asp, Visual or C#.

    thanks in advance.

    Camilo Andres
    Thursday, February 11, 2010 9:21 PM
  • Hi Camilo,

    'SharePointWebControls:FieldValue' mentioned by Praggers is the Page Fields coming under Sharepoint Controls. You can drag and drop the required field value from the Sharepoint designer, which is more error safe.

    Thanks,

    Sudhish

    Pathuppilliath
    Tuesday, April 20, 2010 8:22 PM
  • Thanks Praggers

    This is by far the most efficient approach - also works fine with Sharepoint 2010 and any custom meta tag you might add to your page layout.

    PS! Please note that the internal name for 'Description' in SP2010 is: 'Comments', thus FieldName attribute should equal "Comments". (FieldName="Comments").


    Kind regards


    • Edited by Erik Bo Friday, April 27, 2012 2:01 PM
    Friday, April 27, 2012 2:00 PM