Getting started with NHibernate for SharePoint Part 3 – Show all lists and libraries from a web

« Previous post (Getting started)Next post (Working with a Links List) »


Listing available lists and their properties from a web always brings up some very interesting information. Since the Camelot .NET Connector follows the naming conventions of SQL the command we normally invoke is called “SHOW TABLES”. But to make this a bit more understandable for SharePoint developers we aliased this to “SHOW LISTS” and that’s the command we will use in this tutorial.

To do this using NHibernate for SharePoint we must create a few files in the same project we created earlier

  1. Lists.cs
  2. Lists.hbm.xml

Creating the structure

At this stage we will try to keep things as simple as possible. Simply create the files in your project root. Your project should now be looking something like this

Lists.cs

We will start by trying to fetch a small amount of the available properties of the lists, lets start with ID, Title, Description, Created and ItemCount

using System;

namespace NHibernateExample
{
    public class Lists
    {
        public virtual Guid ID { get; set; }
        public virtual string Title { get; set; }
        public virtual string Description { get; set; }
        public virtual DateTime Created { get; set; }
        public virtual int ItemCount { get; set; }
    }
}

Lists.hbm.xml

The properties of the lists class should be reflected in the hibernate mapping file

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="true">
  <class name="NHibernateExample.Lists, NHibernateExample" lazy="true">
    <id name="ID">
      <generator class="native" />
    </id>
    <property name="Title" />
    <property name="Description" />
    <property name="Created" />
    <property name="ItemCount" />
  </class>
</hibernate-mapping>

It is very important to set the build action of the hbm file to “Embedded Resource”. If not NHibernate will not be able to create the mapping since it will not be aware of the file.

Executing the command

So far we have created the essential files to handle the data coming from SharePoint and it’s time to move on to actually fetching the data. We must add a method that connects the previous, this is called a SessionFactory. Add the following method in the same class where you will execute your code, preferably the Program class if you are in a console application.

static ISessionFactory _sessionFactory;
static ISession OpenSession()
{
    if (_sessionFactory == null)
    {
        // Create configuration instance
        var configuration = new Configuration();

        // Fetch the first connectionProvider, if you have several configure them manually and disable this
        var connectionProvider = Assembly.GetExecutingAssembly().GetTypes().First(x => (typeof(IConnectionProvider)).IsAssignableFrom(x));

        if (connectionProvider != null)
        {
            // Register the connection probider
            configuration.SetProperty("connection.provider", connectionProvider.AssemblyQualifiedName);
        }

        configuration.AddAssembly(Assembly.GetCallingAssembly());
        _sessionFactory = configuration.BuildSessionFactory();
    }
    return _sessionFactory.OpenSession();
}

Creating a query

Any normal day we would now implement a CreateQuery command and execute our statement in that but since we are invoking a SHOW command we must instead execute a CreateSQLQuery and add its entity that are of type Lists.

This will look like this

ISQLQuery showListsQuery = session.CreateSQLQuery("SHOW LISTS").AddEntity(typeof(Lists));

What we are doing here is to send a SQL Query and forcing a type (Lists) on the result so NHibernate can know what kind of object we shall cast our result into later on.

Fill in the rest of the code, to start we must forst open a session, execute the query and then put the result in a list and render the result. My main method now looks like this

static void Main(string[] args)
{
    using (ISession session = OpenSession())
    {
        ISQLQuery showListsQuery = session.CreateSQLQuery("SHOW LISTS").AddEntity(typeof(Lists));
        IList<Lists> lists = showListsQuery.List<Lists>();

        if (lists.Any())
        {
            Console.WindowWidth = 90;
            const string header = "{0,-38} {1,-23} {2,-11} {3}";
            Console.WriteLine(header, "ID", "Title", "Created", "ItemCount");
            foreach (Lists list in lists)
            {
                Console.WriteLine(header, list.ID, list.Title, list.Created.ToShortDateString(), list.ItemCount);
            }
        }
        else
        {
            Console.WriteLine("Could not fetch any lists");
        }

    }
    Console.WriteLine("Done");
    Console.Read();
}

And the result of a normal teamsite will look something like this

This is a rather primitive way to show the result but this can easily be used in any .NET application. Supersimple, flexible and you don’t need much previous knowledge of SharePoint!


« Previous post (Getting started)Next post (Working with a Links List) »

This entry was posted in NHibernate for SharePoint, Tutorials and tagged , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>