Retrive Page Type metadata field value in function for specific page?

Topics: General, XSLT
Mar 10, 2011 at 5:58 PM

Hi!

Im kind of new to Composite C1..but Im starting to get the whole concept...but Im still a little rusty when it comes to the functions..especially when it comes to retriveing data(datatypes)

I have created a new Page metatype with the following setup:

Title: MyMetaData

Type name: MyMetaData

Type namespace: Custom

and then added a field called "Field" which takes a string..

I then have created a new Page Type called "MyCustomPageType" and then set the default layout to the "DefaultPage" layout..

I now add a metadata field to this newly created Page Type...

Metadata type: MyMetaData

Programmatic name: MetaData

Label: MetaData

 

Now here comes the "problem"...I whould like to be able to add a function in the "DefaultPage" layout that fetches the value of the current page metadata field that we just created..so for instance..

get it? :)

 

So for instance...as today the Composite C1 CMS has no built in field for the meta-keywords that some search engines uses...then it whould be great to let the user simply enter a value in the metedata field in the Metadata-tab ...and then use a function in the template to render the <meta name="keywords" content=""> tag and fill the content with keywords...

Another usefull thing whould be for customization of layouts..for instance you might want to change the color of the background of some pages by leting the user enter a HEX number or something in a field called "Background" in the metadata-tab of the page...well I guess you get my point by now..

So...how do I fetch the value of that metadata field?..

 

Thanks in advance!

Mar 10, 2011 at 10:26 PM

I, too, am trying to find some documentation on how to do this.

Coordinator
Mar 10, 2011 at 10:56 PM
Edited Mar 10, 2011 at 10:56 PM

In XSLT function

Add a function call which will get meta data type's xml.(http://docs.composite.net/C1/XSLT/GuideToXsltFunctions.aspx/Making-Function-Calls)

In "Filter" field choose "ActivePageReferenceFilter, so input xml will contain only meta data for the current page

 

In C# code

Guid pageId = Composite.Data.SitemapNavigator.CurrentPageId;
MyMetaDataType meta;
using(var conn = new Composite.Data.DataConnection()) {
  meta = conn.Get<MyMetaDataType>().FirstOrDefault(m => m.PageId == pageId);
}
...

Coordinator
Mar 10, 2011 at 11:04 PM
Edited Jun 28, 2012 at 3:48 PM

We will create an article on this - until that is published you can use these quick routes:

In this sample I will read data from a page meta data type named "Mawtex.PageMetaField".

The C# way:

Create an 'Inline C# Function' with code like below - and insert the function in yout html template head ( <f:function name=What.You.Named.Id" /> ):

 

using System.Xml.Linq;
using Composite.Core.Xml;
using Composite.Data;

namespace Mawtex
{
  public static class InlineMethodFunction
  {
    // quick and dirty code grabbing data using API and returning html element - written in notepad, not tested
    public static XElement Sample()
    {
      using (var dataConnection = new DataConnection() )
      {
        Guid pageId = SitemapNavigator.CurrentPageId; 

        string keywords = dataConnection.Get<Mawtex.PageMetaField>().Where( f=> f.PageId == pageId ).Select( f=> f.Keywords ).FirstOrDefault();
        if (keywords!=null)
          return new XElement( Namespaces.Xhtml + "meta", 
            new XAttribute("name", "keywords" ), 
            new XAttribute("content", keywords )
        else
          return null;
      }
    }
  }
}

 

The XSLT way:

  1. Create a new XSLT function
  2. On the Function calls tab, add a call to "Mawtex.PageMetaField.GetPageMetaFieldXml" (notice that there is a pattern using the type name here)
  3. Add a filter to this Get(Data)Xml call - use a page scoped filter. Default is 'current page' which is fine here. Also make sure your 'Keywords' field is selected.
  4. On the general tab, consider selecting a page that has your meta data field on it. Default is front page.
  5. Preview - you get XML with your data
  6. Write the XSLT that produce the desired result. Save.
  7. Insert the function in your template.
Mar 14, 2011 at 11:49 PM

Thanks!

Sep 11, 2011 at 7:29 PM

Hi,

Did you get round to writing an article on this. I was wondering if it is possible to cater for different meta ketwords for different languages for each page?

Thanks

McV

Jun 28, 2012 at 3:30 PM
Edited Jun 28, 2012 at 3:31 PM

This was really helpful... i was indeed looking for such functionality.

Is article ready on this.

One code change was required to make above code work:

Guid pageId = dataConnection.SitemapNavigator.CurrentPageId;  // This did not worked

change it to

Guid pageId = dataConnection.SitemapNavigator.CurrentPageNode.Id;


Also we need "==" instead of "=" in ( f=> f.PageId = pageId )

Is article ready on this.

Thanks & Regards
Pinal Bhatt | From the desk of Pinal Bhatt

Coordinator
Jun 28, 2012 at 3:49 PM
Edited Jun 28, 2012 at 3:50 PM

@pinalbhatt Thanks for pointing this out! I updated the code above - note that SitemapNavigator.CurrentPageId should also work. This is a static property that give you the id of the current page and this does not need an active DataConnection.

Also, Vitaly recently added related code samples to http://docs.composite.net/Data/AccessingDataWithCSharp/How-to-Query-Data-Using-LINQ