Sharepoint DAC Generator

After a lot of custom SharePoint projects you automatically find some recurrent code blocks. One of these blocks is the data access code to perform read/update actions against a SharePoint list. It’s always the same and maintaining the code is cumbersome especially when you modify while developing the list definition itself.
For these reasons I started developing a code generator for SharePoint lists as a feature.
How does it work (check screenshot below):
On every SharePoint site you active the Dac Generator feature. This feature creates for you two document libraries, one for the xml input file which describes all the DAC methods and the other library contains all the output: a C# classfile for the DAC and one classfile for the Entity (field description class of the SharePoint list).  It also activates a custom list actions for executing the generation on a list of your choice.
When you push the generate button, the tool analysis all the fields of the list and creates first an Entity class, this give you the advantage in development to talk with a typed data class against your SharePoint list. After creating the Entity class, the tool reads the whole xml input file (screenshot below) via Linq (pretty nice) and starts building his DAC class.
After creating you have two class files, one the holds Entity information and the other for talking with the SharePoint list. Copy those files into an assembly of your project and the job is finished. Each class is partial declared so custom modifications can be easily done…
Example of code reduction:
Old code to retrieve data

List<MyEntity> result = new List< MyEntity>();
SPQuery spQuery = new SPQuery();
spQuery.Query = “<query>…”;
SPListItemCollection spListItemCollection = spList.GetItems(spQuery);
foreach (SPListItem spListItem in spListItemCollection)
    MyEntity myEntity = new MyEntity();
    // Parsing the listitems
    myEntity.Title = spListItem.Title;
    if (spListItem[“something”] != null)
        myEntity.something = (cast)spListItem[“something”].ToString();
New code to retrieve data
MyDac myDac = new myDac(spSite.ID, spWeb.ID);
myDac.spListname = listname;
myDac.RunElevated = true; // true or false…
string camlQuery = “<query>…”;
List<MyEntity> result = myDac.GetAllItems(camlQuery);
This entry was posted in Sharepoint. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s