Archive

Posts Tagged ‘ECT’

Determining the NameSpace for an ECT in SharePoint WP

June 4, 2014 2 comments

So the other day, while working on a SharePoint project, I started writing a custom workflow in Visual Studio and I needed to access an ECT programmatically. To date in this project all my WebParts had the usual custom properties to define the NameSpace for the ECT that I needed to access. But suddenly I was in a situation where that information was not available.

So I had to try and find it a different way. After sitting in a debugging session for a bit and interrogating objects and properties, I eventually found it. So in the interest of sharing some code, here it is. Now I couldn’t find a solution online after googling & binging for a bit. And there might be other solutions out there much better than mine.

using (new SPServiceContextScope(SPServiceContext.GetContext(SPContext.Current.Site)))
{
 var service = Microsoft.SharePoint.Administration.SPFarm.Local.Services.GetValue<BdcService>(String.Empty);
 Microsoft.BusinessData.MetadataModel.IMetadataCatalog catalog = service.GetDatabaseBackedMetadataCatalog(SPServiceContext.Current);

 var tmp = catalog.GetEntities(“Agencies”);
 var _NameSpace = tmp[0].Key;
}

Now the “Agencies” entity is just an ECT that I know will be there and it is purely used to be able to extract the NameSpace. And there you have it, no more custom properties on my WebParts to configure the NameSpaces for the ECTs.