Search This Blog

Saturday, December 14, 2013

How to update BDC / BCS custom connector DLL programmatically in SharePoint 2010

using System.Management.Automation.Runspaces;
using System.Management.Automation;


public static void UpdateBDCDLL(string webappURL, string strModelName)
        {
            try
            {
// Mapping Assembly of BDC
                string DllPath = @"@"<DLL PHYSICAL PATH>";
                //create powershell runspace
                RunspaceConfiguration config = RunspaceConfiguration.Create();
                PSSnapInException OExSnapIn = null;
                PSSnapInInfo pssnap = config.AddPSSnapIn("Microsoft.SharePoint.PowerShell", out OExSnapIn);
                Runspace cmdlet = RunspaceFactory.CreateRunspace(config);                cmdlet.Open();
                RunspaceInvoke scriptInvoker = new RunspaceInvoke(cmdlet);

// set powershell execution policy to unrestricted

                scriptInvoker.Invoke("Set-ExecutionPolicy Unrestricted");  

// create a pipeline and load it with command object
                  Pipeline pipeline = cmdlet.CreatePipeline();
              StringBuilder sbPSCommand = new StringBuilder();
                sbPSCommand.AppendLine("$assemblyPath = '" + DllPath + "'");
                sbPSCommand.AppendLine("$lobSystem = '" + strModelName + "'");
                sbPSCommand.AppendLine("$url='" + webappURL + "'");
                sbPSCommand.AppendLine("$serviceContext = Get-SPServiceContext $url");
                sbPSCommand.AppendLine("$lobSystem = Get-SPBusinessDataCatalogMetadataObject -ServiceContext $serviceContext -BdcObjectType lobsystem -Name $lobSystem");
                sbPSCommand.AppendLine("Import-SPBusinessDataCatalogDotNetAssembly -LobSystem $lobSystem -Path $assemblyPath -Confirm:$false");
                Pipeline pipeline2 = cmdlet.CreatePipeline();
                pipeline2.Commands.AddScript(sbPSCommand.ToString());
                pipeline2.Invoke();
            }
            catch (Exception ex)
            {
                //exception handling goes here
            }
        }


No comments:

Post a Comment