YAF Integration to Umbraco 4
For a website we are currently working on, we needed a Forum. The new Forum4Umbraco is still in early development so we turned to Yet Another Forum. This post describes how to integrate YAF into Umbraco 4 as a User Control.
This means you can create a template in Umbraco, and then insert the following line(s) of code to get an instant forum:
<form runat="server"> <yaf:Forum runat="server" ID="forum" /> </form>
This was also possible because we do not need to share Membership information between Umbraco and YAF. It may well be possible to share membership, and it’s something I’d like to look into if I ever get the time!
YAF Install Steps
1. Install Umbraco as usual, for instance into C:\Inetpub\wwwroot
2. If required, create a new database for YAF. Alternatively, YAF can share the same database as Umbraco.
3. Create a directory for YAF, e.g. wwwroot/yaf
4. Unzip YAF files into wwwroot/yaf
5. Temporarily rename umbraco web.config to _web.config
6. Rename wwwroot/yaf/default.config to wwwroot/yaf/web.config
7. Enter a DB connection string into yafnet.config, into the <connstr/> tag. If sharing the database with the Umbraco install, copy the DB connection string from the “umbracoDbDSN” element in web.config into yafnet.config. Note the “datalayer” keyword is not supported.
For example, if the Umbraco web.config contains the entry:
<add key="umbracoDbDSN" value="datalayer=SqlServer;Server=.\sql2005;Database=umbraco; User ID=umbraco_user;Password=password;Trusted_Connection=False" />
Then copy the connection string but do not include the datalayer section, e.g. yaf.config should contain:
<connstr>Server=.\sql2005;Database=umbraco;User ID=umbraco_user;Password=password; Trusted_Connection=False</connstr>
7. Still editing yaf.config, add a <root>/yaf</root> element.
8. In IIS create a new virtual application on the wwwroot/yaf directory:
- Right-click on the /yaf directory in IIS
- Choose “Properties” from the context menu
- Click the “Create” button on the Properties dialog
- Click OK
NOTE: at this point on the Umbraco website will stop functioning. It will begin working again once the YAF application is removed (later in this guide).
9. Open a web browser, and navigate to http://www.mywebsite.com/yaf/install. Follow the YAF installation wizard instructions.
10. Navigate to http://www.mywebsite.com/yaf and check that the forum is working.
Get Umbraco Working again
11. Right click on YAF, choose properties, click the “Remove” button to disable the YAF application.
Note: The Umbraco web application will now begin working again.
12. Move the yaf.config file to the root website directory e.g. wwwroot/
13. Rename wwwroot/_web.config back to web.config
14. Rename wwwroot/yaf/web.config back to wwwroot/yaf/default.config
15. Update web.config with config settings from the /yaf/default.config
<section name="yafnet" type="yaf.SectionHandler,yaf"/>
<authentication mode="Forms"> <forms name=".YAFNET_Authentication" timeout="525600" /> </authentication>
<customErrors defaultRedirect="error.aspx" mode="RemoteOnly"/>
Note that you will need to comment out the following line in the web.config file:
<forms name="yourAuthCookie" loginUrl="login.aspx" protection="All" path="/" />
16. Copy or move wwwroot/yaf/yaf.dll to wwwroot/bin/yaf.dll
17. Copy or move wwwroot/yaf/framehelper.aspx to wwwroot/framehelper.aspx
Add the forum to your website.
Create a document type and a template. The template will look something like this:
<%@ Register TagPrefix="yaf" Namespace="yaf" Assembly="yaf" %> <%@ Register TagPrefix="yc" Namespace="yaf.controls" Assembly="yaf" %> <asp:Content id="Forumcontent" ContentPlaceHolderID="ContentPlaceHolderDefault" runat="server"> <form runat="server">
<yaf:Forum runat="server" ID="forum" /> </form> </asp:Content>
Unfortunately there is a problem. The YAF User Control will not work properly – it will attempt to reference default.aspx which is ‘owned’ by Umbraco. This can be fixed using UrlRewriting.
To get around this:
1) Obtain the nodeId of the forum. One way to do this is to log into the Umbraco Backend, place your mouse pointer over the forum node, and look at the browser status bar. This will contain the node id.
For the purpose of this tutorial, we will assume the Node Id is 9999.
1) Update urlRewriting.config with the following (Note the Node Id in bold should be replaced with your node id):
<!-- Redirect any requests for the-forum.aspx to forum.aspx --> <add name="forum-redirect" virtualUrl="^~/the-forum.aspx" rewriteUrlParameter="ExcludeFromClientQueryString" destinationUrl="~/forum.aspx" ignoreCase="true" redirect="Application" redirectMode="Permanent" />
<!-- Internally rewrite the url --> <add name="forum-rewrite" virtualUrl="^~/forum.aspx" rewriteUrlParameter="ExcludeFromClientQueryString" destinationUrl="~/9999.aspx" ignoreCase="true" />
2) Ensure that the page (node) in Umbraco which contains the forum is called “Forum” and resolves to the url, “/forum.aspx”.
Note: Alternatively you could add a property to the Document Type called “umbracoUrlName” and set the value of that property to “the-forum”.
3) Ensure that there is NOT a page in Umbraco which resolves to “forum.aspx” (perhaps add to the list of reserved names?)
4) Ideally, any links (in templates, xslt etc) to the forum should link to “/forum.aspx” – NOT “the-forum.aspx”. This means when linking to the forum, avoid using umbraco.library:NiceUrl(…)
Note: This is a recommendation rather than a necessity. The “forum-redirect” rule above will ensure that all links to the forum will work.
Thats it – we’re done. The forum should now be visible at www.mywebsite.com/forum.aspx , embedded into an Umbraco page.
You will also need to add /framehelper.aspx to the list of reserved paths in web.config
<add key=”umbracoReservedPaths” value=”/umbraco,/install/,/bingo,/framehelper.aspx” />
Also, if you wish to place the forum in a directory other than the root of your website, you will need to add a url rewriting rule for framehelper.aspx.
E.g. if you place the forum in /subdir/forum.aspx, you will need the following rule in urlRewriting.config:
In fairness I should mention that most of the implementation details on how to embed YAF are actually covered on the YAF website. Also, once I had worked out how to integrate YAF into Umbraco, I stumbled across this post by Skiltz on .NET, Integrating YetAnotherForum.net into Umbraco. This post is concerned with Umbraco v3 but the steps are much the same so it’s great to see that others have been able to get this to work. In this case, YAF was recompiled to get around the default.aspx issue – I solved this problem using UrlRewriting. If you’d rather not use the UrlRewriting, you could try using the yaf.dll file provided by Skiltz instead.
Also thanks to Lars (see comments, below) for his tip on the use of the NodeId in the URL Re-write.