For outbound EDI batching, we have to create Batch Configuration in Party Configuration and set Filter Criteria about what messages should constitute one batch. But what if Filter Criteria is dynamic or we have too many batches to configure
here is code snippet to create batch configuration through API
var builder = new SqlConnectionStringBuilder("DATA SOURCE="BTSSQLServer;Initial Catalog=BizTalkMgmtDb;
using (var tmpCtx = TpmContext.Create(builder))
var agreement = (from p in tmpCtx.Agreements where p.Name == AgreementName select p).FirstOrDefault();
if (agreement == null)//create agreement if does not exist.
string Sender = ConfigurationManager.AppSettings["Sender"] ?? "SenderPartyName";
string Receiver = ConfigurationManager.AppSettings["Receiver"] ?? "ReceiverPartyName";
var OneWayAgreement = agreement.GetOnewayAgreement(Sender, Receiver);
var firstBatch = OneWayAgreement.GetBatches(); // first batch config is used as template
var newBatch = OneWayAgreement.CreateBatch(BatchName);
FilterPredicate predicate = firstBatch.GetFilterPredicate();
predicate.Groups.Statements.Value = "FilterRHS";
// this SQL insert is required to Activate the batch config
using (var cmd = new SqlCommand(@" INSERT INTO [dbo].[PAM_Control]
,[SenderPartyName], ReceiverPartyName, AgreementName)
SELECT 0 as EDIMessageType
,'EdiBatchActivate' as 'ActionType'
,GetDate() as 'ActionDateTime'
,0 as 'UsedOnce'
," + newBatch.Id + @" as [BatchId]
,'" + BatchName + @"' as [BatchName], '"+ Sender +@"', '"+
Receiver +@"', '" + AgreementName + "'", new SqlConnection(builder.ConnectionString)))