Adding BDC Columns to a Sharepoint List

I am trying to add BDC columns to a sharepoint list programatically.

In fact the application that I am working on has 140 lists each potentially with a different set of BDC extension columns.

This is of course impossible – at least no-one is saying how to do so on the blogs.

You cannot use the simple field constructor as business data is not in the enumeration.

The trick is to extract the XmlSchema from the field and remove the offending gumph (that is the specific guids – which I now think may be the cause of my problems).

There are two methods that I have found useful:

SPField.AddFieldAsXml is the key method on the list to allow the xml to be added.

You need to tell the client which bdc provider to use:

SqlSessionProvider.Instance().SetSharedResourceProviderToUse(“myssp_id”)

This almost works (the fields are added, you get a cryptic error about looking in the logs yet the bdc column does not work).

I suspect that the guids that I removed are the problem.

Normally I only post working examples – this is not one of them.

Loading data into a Sharepoint page from a webpart file

/*

using System;

using System.Xml;

using Microsoft.SharePoint;

using Microsoft.SharePoint.WebPartPages;

*/

 

            try

            {

 

                using (SPSite site = new SPSite(“http://mysite:123456/“))

                {

                    using (SPWeb web = site.OpenWeb(“MySubWeb”))

                    {

                        SPFile targetPage = web.GetFile(“default.aspx”);

 

                        SPLimitedWebPartManager webpartManager = targetPage.GetLimitedWebPartManager(

                            System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);

 

                        string errMsg;

 

                        // This is the file that can be exported from the webpart

                        XmlTextReader reader = new XmlTextReader(“summary.webpart”);

                        System.Web.UI.WebControls.WebParts.WebPart webPart = webpartManager.ImportWebPart(reader, out errMsg);

 

                        webpartManager.AddWebPart(webPart, “TopWebPartZone”, 1);

                        targetPage.Update();

 

                    }

                }

 

            }

            catch (Exception ex)

            {

                Console.WriteLine(ex);

            }

PowerShell

Recently I have been experimenting with PowerShell.

I have a need to configure Sharepoint in a repeatable manner and have considered Powershell as a possible means of doing so.

Here is a site that provides basic PowerShell documentation

Here is a site that has 5 useful Sharepoint PowerShell functions.
The code is missing a reference to the assembly but that can easily be fixed.

This is a site that proves a sample script to recycle the app pool – something that you need to do very frequently in sharepoint development.

How the BDC Columns actually work

This post explains how the BDC works under the hood.
It does however miss out on fully updating the underlying data – you need to update each BDC field explicitly.

The big flaw with the BDC is that the externally referenced data is cached in the list itself.  The user needs to update the list (which can be time consuming) or the host app must do it for the user.