LINQ TO UMBRACO

LINQ to Umbraco was developed to provide developers with an alternative way to work with Umbraco content nodes. It was designed from the start to provide an alternative to XSLT for developers who prefer working in a more traditional .NET environment using POCOs (Plain Old CLR Object) and C# or VB.NET syntax. As such, it's important to note that it's not meant to be a replacement for the NodeFactory or Document API (discussed earlier in this chapter). The default data provider for LINQ to Umbraco is the NodeDataProvider, which looks directly at the XML cache, much like the XSLT templates do. So, using LINQ to Umbraco out of the box means that you have access to the published nodes in a strongly typed read-only fashion.

image If all you're trying to do is output a document type property on a page, then sticking with standard Umbraco display fields, namely the <umbraco:item /> tag that is covered in Chapter 4, is best.

Getting Started with LINQ to Umbraco

To use LINQ to Umbraco in your project, you must export your document types to .NET. The process of exporting your document types is covered in Chapter 3. Doing this provides you with several data contexts from which to query the nodes you're looking for. Listings 12-18 goes back to the FAQ example and shows how to output nodes using LINQ method or query syntax.

Before you can dig into code samples, export your document types using the following settings, and add the generated class to your project in Visual Studio.

Use the following values for exporting the document types.

  • DataContext Name: FAQ
  • Namespace: UmbUsersGuide.Samples

LISTING 12-18: ListFaqs.ascx.cs

image
using System;
using System.Linq;
using System.Text;

namespace UmbUsersGuide.Samples.UserControls
{
    public partial class ListFaqs : System.Web.UI.UserControl
    {
        // Initialize a private FAQDataContext
        private FAQDataContext ctx;
        protected void Page_Load(object sender, EventArgs e)
        {
            // Tie the localized ctx variable to a new instatiation
            // of the Data Context
            ctx = new FAQDataContext();

            // Now, query the context using LINQ query syntax to
            // get some nodes. This returns an IEnumerable which
            // you can then use to loop over for display
            var techQuestionFaqs = from faq in ctx.FAQs
                                       orderby faq.UpdateDate descending
                                       where faq.Category == “1”
                                       select faq;

            var htmlOut = new StringBuilder();
            htmlOut.Append(“<ul>”);
            foreach (var faq in techQuestionFaqs)
            {
                htmlOut.AppendFormat(“<li>{0}</li>”, faq.NodeName);
            }
            htmlOut.Append(“</ul>”);

            // send the output to the view

            _faqList.Text = htmlOut.ToString();
        }
    }
}

Getting started using LINQ to Umbraco as an alternative to XSLT or the NodeFactory class is that simple.

Extending LINQ to Umbraco

LINQ to Umbraco was built with performance, flexibility, and testability in mind. So, although it's great as a potential replacement for XSLT or NodeFactory, it's also great for CRUD operations. Out of the box, it comes with a method called SubmitChanges, which are used to save changes to nodes in the database.

image Trying to execute this method using the default NodeDataProvider results in a System.NotSupportedException because this provider is read-only.

To learn more about how to extend LINQ to Umbraco, including how to create your own providers, take a look at Aaron Powell's blog at www.aaron-powell.com/. Aaron is the father of LINQ to Umbraco and large part of the Umbraco core team.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.227.161.225