Programatically creating External Content type in SharePoint 2010

When delivering a SharePoint 2010 talk last Saturday I was asked whether it’s possible to save the external list as a template and the answer at the time of this Beta 2 timeframe is no. One of the reasons you’d want to save the list as a template is so you can export it to Visual Studio and make it a part of your package and be able to deploy it.

In this article I will show how you can create an external content type and generate few sample operation . Once we have that, we’ll go ahead and create a list based on the content type.

Open your Visual Studion 2010 Beta 2 and create a new BDC Model Project:

bdc_project

Open Entity.cs  – this is your Entity Definition class; by defining properties like these, you will expose columns in your future external list:

public string ID { get; set; }
public string Message { get; set; } 

Now, open Entity1Service.cs  – in here you will be implementing operation that will handle Delete, Edit, New, Read, Read List. By default there will be only read item and read list operation implemented; a minimum set of operation for you to see items in a list and be able to read individual item.

Your readitem operation will look something like this:

public static Entity1 ReadItem(string id)
        {
            Entity1 entity1 = new Entity1();
            entity1.ID= id;
            entity1.Message = “Hello !”;
            return entity1;
        }

In here you can really do anything you need to do to retrieve your item. You will probably be consuming parameters to select the correct item.

Once you’re ready to run your first test, build and deploy your project. To verify it’s been successfully deployed, access your SharePoint Administration -> Manage Service Applications -> Business Data Connectivity  and ensure the name of your newly created Entity is there.

Now you can create instances of list based on this entity. Keep in mind the entity will be identified as .NET assembly. If you had SharePoint designer opened while creating your entity, ensure you refreshed External Content Types window to ensure you can see your newly created entity.

Happy coding!

This entry was posted in sharepoint, sharepoint 2010 and tagged , , , , , . Bookmark the permalink.

Comments are closed.