Thursday, June 26, 2014

Create an External List Using C#

In my previous 2 articles (Create SharePoint 2013 External List using Visual Studio 2012Extending External List Capabilities to Add and Edit List Items) I described how to create an External Content Type and create a list using that content type. 

In this article the focus will be on creating the list using C# rather using the SharePoint UI. Given the BDC Model goes in a farm level feature and create the external content type, the best option would have been writing the list creation code in the FeatureActivated method of the feature receiver. There are some issues I faced when creating this list. Given the BDC model deployment also associated with a feature receiver that is not visible, this was not possible to create a feature receiver for the feature that included the BDC model. There were some possible scenarios explained in some articles, but I did not follow them since there is another cleaner approach.

Therefore I created another feature and added a feature receiver for the second feature. The feature activation procedure needs to be done in an order. The second feature needed to be activated after the deployment of the first feature as the External Content Type needed to be presented before creating the list.


I will be referring to the Visual Studio solution I created in previous article which can be found here. The step by step guide is as follows.

1. In the feature reciever I added following constants.

private const string ListName = "MyExternalList";
private const string ListDescription = "External list containing service types for each client";
private const string ListUrlFormat = "Lists/{0}";

private const string LobSystemInstanceValue = "VSDemoModel";
private const string EntityNamespaceValue = "BCSVSDemo.VSDemoModel";
private const string EntityValue = "Entity1";
private const string SpecificFinderValue = "ReadItem";

I will go through some explanation on what are the last 3 constants are. 

LobSystemInstanceValue --> Give the name you gave when creating the BDC model. It will appear in the solution explorer.
EntityNamespaceValue --> Namespace of the class that the entity class is available.
EntityValue --> Name of the entity which includes the properties.
SpecificFinderValue --> The method that maps for reading an item. The method name which is 'ReadItem' by default.

2. Then in the feature activation I added the following code.

SPListDataSource ds = new SPListDataSource();
ds.SetProperty(SPListDataSource.BDCProperties.LobSystemInstance, 
   Feature1FeatureEventReceiver.LobSystemInstanceValue);
ds.SetProperty(SPListDataSource.BDCProperties.EntityNamespace, 
   Feature1FeatureEventReceiver.EntityNamespaceValue);
ds.SetProperty(SPListDataSource.BDCProperties.Entity, 
   Feature1FeatureEventReceiver.EntityValue);
ds.SetProperty(SPListDataSource.BDCProperties.SpecificFinder, 
   Feature1FeatureEventReceiver.SpecificFinderValue);

using (SPSite site = new SPSite("http://uglwfe/sites/APT"))
{
   using (SPWeb web = site.RootWeb)
   {
      SPList externalList = web.Lists.TryGetList(Feature1FeatureEventReceiver.ListName);

      if (externalList == null)
      {
         string listUrl = string.Format(
         Feature1FeatureEventReceiver..ListUrlFormat, 
         Feature1FeatureEventReceiver..ListName);

         web.Lists.Add(
            Feature1FeatureEventReceiver..ListName, 
            Feature1FeatureEventReceiver..ListDescription, 
            listUrl, 
            ds);
      }
   }
}

Now activate the second feature (one with the event receiver) after activating the feature included the BDC model. Make sure the proper rights are set in the external content type so that can be accessed by necessary users.  

1 comment:

  1. I'm only vaguely familiar with C# but with your help I was able to figure this out! Thanks!

    Fred | IT Support Mansfield

    ReplyDelete