Using the SharePoint Guidance 2010

The Patterns & Practices SharePoint 2010 Guidance is something rather for advanced developers and I was curious what exactly is in this stack. After the first glance I believe the stuff is really good and the included SharePoint Guidance Library looks like a huge benefit. Therefore I was tempted to try and use those libraries immediately at my own. Hence fore I have decided to retailor my previously published example Another walkthrough of enabling CRUD BCS for SP2010 in order to start from the scratch with those libraries.

Just to remind you, in the mentioned solution I was utilizing Business Connectivity Services and have created an external list accessing the Northwind database via Entity Framework. In that solution the connection string was written directly into the Web-Application’s web.config file (as matter of fact it is open for discussions whether such approach truly fits into the overall design requirements).

In this version of that former example I will put the connection string rather into the Web-Application’s setting using the Guidance Library’s Service Locator and the Application Setting Manager. Basically this new solution’s BDC model was slightly extended. Next to the Customer entity I have added the related Orders entity as well. Here the steps to follow in order to accomplish the plan.

Firstly of course you have to download and extract the above mentioned guidance. Next you could install the guidance’s two base libraries into the GAC and reference them from within all of your SharePoint 2010 development solutions:

  1. Microsoft.Practices.SharePoint.Common.dll
  2. Microsoft.Practices.ServiceLocation.dll

Also note (in my humble opinion) that it could make sense integrating these libraries into the overall SharePoint 2010 infrastructure prerequisite. This is however matter of another discussions.

The first change introducing the guidance libraries affects our event receiver (Picture 1) which was previously intended to inject the appropriate <connectionStrings> element into the web.config. In this version I’m going to replace that code details using the Application Setting Manager, thus the connection string will be a hardwired setting of the deployment package. Activating the feature, the Setting Manager will simply insert that prepared connections string into a SPWebApplication-level property bag (Code 1). The connection string is later retrievable using the predefined string Key NorthwindEntitiesConnectionString.

Also note, that here the connection string is concatenated rather only for more readability and it does NOT reflect any best practices in this regard. As best practice could be instead the utilization of the classes EntityConnection and the EntityConnectionStringBuilder like this msdn article describes.

 image

Picture 1

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
   IServiceLocator serviceLocator =
           SharePointServiceLocator.GetCurrent();
   IConfigManager configManager =
          serviceLocator.GetInstance<IConfigManager>();

   // Web-Application (SPWebApplication)
   SPWebApplication webApplication = properties.Feature.Parent as SPWebApplication;
   if (webApplication != null)
   {
      SPSite siteCollection = webApplication.Sites[0];
      if (siteCollection != null)
      {
         configManager.SetWeb(siteCollection.RootWeb);
         IPropertyBag bag = configManager.GetPropertyBag(ConfigLevel.CurrentSPWebApplication);
         configManager.SetInPropertyBag(
           “NorthwindEntitiesConnectionString”,
           “metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;”+
           “provider=System.Data.SqlClient;” +
           “provider connection string=’Data Source=.\\sqlexpress;” +
           “Initial Catalog=Northwind;” +
           “Integrated Security=True;MultipleActiveResultSets=True'”,
           bag);
      }
   }
}

Code 1

Furthermore this setting will be removed from the web application’s property bag while the feature is deactivating (Code 2).

The next challenging moment is to access that setting during runtime within the BDC model’s implementation classes (Picture 2). The code within the source-code files CustomerEntityService.cs and OrderEntityService.cs will not change too much (Code 3) as the hearth piece is relocated in the Utility.cs code-file (Code 4). So whenever the EntityConnection is needed, it is recreated based on the connection string set as application setting.

public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
   IServiceLocator serviceLocator =
           SharePointServiceLocator.GetCurrent();
   IConfigManager configManager =
           serviceLocator.GetInstance<IConfigManager>();

    SPWebApplication webApplication = properties.Feature.Parent as SPWebApplication;
    if (webApplication != null)
    {
       SPSite siteCollection = webApplication.Sites[0];
       if (siteCollection != null)
       {
          configManager.SetWeb(siteCollection.RootWeb);
          IPropertyBag bag = configManager.GetPropertyBag (ConfigLevel.CurrentSPWebApplication);
          configManager.RemoveKeyFromPropertyBag(
               “NorthwindEntitiesConnectionString”, bag);
      }
   }
}

 

Code 2

image

Picture 2

EntityConnection conn = UtilityClass.BuildUpEntityConnection();

Code 3

public static EntityConnection BuildUpEntityConnection()
{
   IServiceLocator serviceLocator =
       SharePointServiceLocator.GetCurrent();
   IHierarchicalConfig config =
       serviceLocator.GetInstance<IHierarchicalConfig>();

   string connectionString;
   if (config.ContainsKey(“NorthwindEntitiesConnectionString”))
   {
      connectionString = config.GetByKey<string>(“NorthwindEntitiesConnectionString”);
      return new EntityConnection(connectionString);
   }
   return null;
}

 

Code 4

Here is the complete solution download (do not forget to adjust your project’s Site URL):

http://cid-8d365142bc4869ab.office.live.com/self.aspx/.Documents/EntityFrameworkBdcNw2.zip 

Advertisements
This entry was posted in SharePoint 2010. Bookmark the permalink.

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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