Go Back

Using OpenAccess Persistent Objects in Sitefinity

To use an OpenAccess persistent class in Sitefinity, first install OpenAccess and create a new Class project in Visual Studio.  Next, create the objects you want OpenAccess to persistNote: You cannot create your persistent objects in ~/App_Code.

Make your Persistent Class part of your Solution

Open your Sitefinity web site in Visual Studio by clicking File -> Open Web Site.  Navigate to the directory of your Sitefinity web site and click Open.

Sitefinity in Solution Explorer

Click File -> Add -> Existing Project to make your Persistent Class part of your current project.  (Note: alternately you can add a reference to your persistent class' DLL or simply copy the DLL file to your web site's ~/bin folder.)

Adding an Existing Project to Sitefinity

Navigate to the location of your persistent class' project file, select the file and click Open.

Select Existing Project to add to Sitefinity

Your persistent class should now become part of the Solution Explorer.

Existing Project is added to Sitefinity

Add a Reference to Telerik.OpenAccess and Telerik.OpenAccess.Query

Right-click on the root of your Sitefinity web-site and click Add Reference.

Add a Reference to Sitefinity

Locate Telerik.OpenAccess in the .NET tab, select it and click OK.

Add OpenAccess Reference to Sitefinity

Repeat the step above and select Telerik.OpenAccess.Query as well.

Add a Reference to your Persistent Class

Right-click on the root of your Sitefinity web-site and click Add Reference.

website-add-reference

Select your project in the Projects tab and click OK.

add-project-reference 

Making use of the Persistent Class

We're now ready to use our persistent class inside Sitefinity.  Let's look at some code:

using System;
using System.Web;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        MyClass.Customer cust = new MyClass.Customer();
        cust.FirstName = "Gabe";
        cust.LastName = "Sumner";

        Telerik.OpenAccess.IObjectScope scope = MyClass.ObjectScopeProvider1.GetNewObjectScope();
        scope.Transaction.Begin();
        scope.Add(cust);
        scope.Transaction.Commit();
    }
}

That's it!  With 7 lines of code OpenAccess will create the customer database table and generate/execute the SQL needed to store this new customer object.  Check the database and you'll see a new customer table with 1 row.

OpenAccess created table in Server Explorer

We could then fetch and display all Customer objects using LINQ with the following code:

using System;
using System.Web;
using System.Linq;
using Telerik.OpenAccess.Query;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Telerik.OpenAccess.IObjectScope scope = MyClass.ObjectScopeProvider1.GetNewObjectScope();
        var result = from c in scope.Extent<MyClass.Customer>() select c;

        foreach (MyClass.Customer customer in result)
        {
            Response.Write(customer.FirstName + " " + customer.LastName + "<br />");
        }
    }
}

When using LINQ, be sure to add "using Telerik.OpenAccess.Query"

In the examples above I've placed the code in an ASP.NET page code-behind file.  However, I could also place this code in a UserControl.  This control could then be dragged & dropped onto your Sitefinity pages.

Where to Now?

We've barely scratched the surface of what you can do with OpenAccess.  I'll publish more articles in the future.  For now, please check out the support page and the product forums.

Comments  3

  • Brook 25 Dec

    I love this tool!!!
  • Matt 16 Jan

    Hi, very helpful.. Can you get a collection of some sort you can bind to a control or can you only retieve single objects? Sorry, finding my way!
  • goxel 20 Jan

    how i can update an openaccess object i use below code, but i doesnt work, why? can you help me? scope = Database.Get("DatabaseConnection1").GetObjectScope(); scope.Transaction.Begin(); role.Definition = "aaa"; scope.Transaction.Commit();
Post a comment!
  1. Formatting options
       
     
     
     
     
       
  2. I'm sorry for the CAPTCHA. You have spammers to thank for this: