C# function not finding type

Topics: General, Troubleshooting, XSLT
Jun 15, 2011 at 9:28 AM

Hi all,

Evaluating Composite at the moment, so quite new to the system, and I've hit the following issue and any light thrown on it would be appreciated!

Under the Data view I've created a global datatype, XXX.Datatypes.Event, and added two bits of data '/bob/one' and 'bob/two', representing events. The Event type contains four fields - URL, Description, StartDate, EndDate.

Under the functions view I've created a Visual function, XXX.Datatypes.EventRendering to output the information in a definition list. So far so good, however I want to wrap the description in a link pointing to the URL. After discovering Visual functions don't play nicely with xsl:attribute, a bit of googling pointed me in the direction of using a C# function which I wrote. And thats where I hit a problem.

I've created the C# function, with a single input parameter (myevent) of type DataReference<Event> and passed a test value of '/bob/one' and a default value of NONE, ther source is as follows:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using Composite.Data;
using Composite.Data.Types;

using XXX.Datatypes;

namespace XXX.Datatypes{
public static class InlineMethodFunction{
public static string EventLink(XXX.Datatypes.Event myevent){
return (myevent.GetType().Name);//test output
}
}
}

And when I save and preview, I get the following error:

14 : CS0234 : The type or namespace name 'Event' does not exist in the namespace 'XXX.Datatypes' (are you missing an assembly reference?)

Yet everything in the function pane points to the XXX.Datatypes.Event being there! Including when previewing the Visual function!

Anyone have an idea?

Thanks in advance,

Dave

Jun 15, 2011 at 12:26 PM
eckersalld wrote:
...

Under the functions view I've created a Visual function, XXX.Datatypes.EventRendering to output the information in a definition list. So far so good, however I want to wrap the description in a link pointing to the URL. After discovering Visual functions don't play nicely with xsl:attribute, a bit of googling pointed me in the direction of using a C# function which I wrote.

Hello,

I am not sure how you will use this inline C# function in your XSLT to render "Description" filed  within link pointed to "URL" filed, but below is the example how you can do it in XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:in="http://www.composite.net/ns/transformation/input/1.0" xmlns:lang="http://www.composite.net/ns/localization/1.0" xmlns:f="http://www.composite.net/ns/function/1.0" xmlns="http://www.w3.org/1999/xhtml" exclude-result-prefixes="xsl in lang f">
  
  <xsl:variable name="events" select="/in:inputs/in:result[@name='GetEventXml']/Event" />
  <xsl:template match="/">
    <html>
      <head></head>
      <body>
        <ul>
          <xsl:apply-templates select="$events" />
        </ul>
      </body>
    </html>
  </xsl:template>
  <xsl:template match="Event">
    <li>
      <a href="{@URL}">
        <xsl:value-of select="@Description" />
      </a>
    </li>
  </xsl:template>
</xsl:stylesheet>


Also you can call C# method within XSLT in such way:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:in="http://www.composite.net/ns/transformation/input/1.0" xmlns:lang="http://www.composite.net/ns/localization/1.0" xmlns:f="http://www.composite.net/ns/function/1.0" xmlns="http://www.w3.org/1999/xhtml"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:csharp="http://c1.composite.net/sample/csharp"
exclude-result-prefixes="xsl in lang f csharp msxsl">
  
  <xsl:variable name="events" select="/in:inputs/in:result[@name='GetEventXml']/Event" />
  <xsl:template match="/">
    <html>
      <head></head>
      <body>
        <ul>
          <xsl:apply-templates select="$events" />
        </ul>
      </body>
    </html>
  </xsl:template>
  <xsl:template match="Event">
    <li>
      <a href="{@URL}">
        <xsl:value-of select="@Description" />
      </a>
      <!-- Example how to call C# method-->
      <xsl:value-of select="csharp:MyMethod(@URL, @Description)" />
    </li>
  </xsl:template>
   <msxsl:script implements-prefix="csharp" language="C#">
   <msxsl:assembly name="System.Web" />
   <msxsl:using namespace="System.Web" />
        public String MyMethod(string url, string description)
        {
         // your code here
            return description;
        }
    </msxsl:script>
</xsl:stylesheet>
 

Regarding to the C# inline error I think that  Tools(at the top panel)->Restart  Server will help. But then you will see another error like:

The parameter 'myevent' is expecting test value of type 'XXX.Datatypes.Event', got value of type 'Composite.Data.DataReference`1[XXX.Datatypes.Event]'.

So your inline function should be like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using Composite.Data;
using Composite.Data.Types;

namespace XXX.Datatypes
{
  public static class InlineMethodFunction
  {
    public static bool EventLink(DataReference<Event2> myevent)
    {
      return true;
    }
  }
}

Jun 16, 2011 at 9:28 AM

Hi Inna, thanks for the reply and help.

My aim was to use the C# function inside a visual function, by passing the Event datatype to it and returning a string of "<a href=\"" + <EventObject>.URL + "\"><EventObject>.Description</a>", so providing me with a single object I can just use to display events on a site.

I'll certainly have a look at the XSLT functions though, as in this case they may be the better solution.