Re: How can I resolve the Party Name

Home Page Forums BizTalk 2004 – BizTalk 2010 How can I resolve the Party Name Re: How can I resolve the Party Name

#15654

I have a small .Net helper class that allows you to cross reference the party properties.

You can use the static method GetAliasFromSID to retrieve the OrganisationName:

strPartyName = BTSParty.CrossReference.GetAliasFromSID(Message_1(BTS.SourcePartyID),"OrganisationName");

Source:

using System;
using System.Data;
using System.Data.SqlClient;
using System.Management;
using System.Xml;
using Microsoft.BizTalk.ExplorerOM;
using Microsoft.Win32;

namespace BTSParty
{
 /// <summary>
 /// Summary description for PartyAliasMapper.
 /// </summary>
 public class CrossReference
 {
  static private object syncRoot = new object();
  static private string MgmtDBConnString = String.Empty;

  public struct PartyDetails
  {
   public string SID;
   public string PartyName;
  }

  public static string GetMgmtDBConnectionString()
  {
   string BTSMgmtDBName = String.Empty, BTSMgmtDBServerName = String.Empty;
   //check to see if the string has already been retrieved.  If not, get it and store it.
   if( MgmtDBConnString.Length == 0 )
   {
    lock( syncRoot )
    {
     if( MgmtDBConnString.Length == 0)
     {
      ManagementObjectSearcher searcher =
       new ManagementObjectSearcher(@"root\MicrosoftBizTalkServer", "SELECT * FROM MSBTS_GroupSetting");
      foreach (ManagementObject Group in searcher.Get())
      {
       if(Group != null)
       {
        Group.Get();
        BTSMgmtDBName = Group["MgmtDbName"].ToString();
        BTSMgmtDBServerName = Group["MgmtDbServerName"].ToString();
       }      
      }
      if( BTSMgmtDBName.Length == 0 || BTSMgmtDBServerName.Length == 0 )
       throw new ApplicationException("Unable to find Management Database Name or Management Database Server Name");

      // Assuming Integrated Security is being used for database connection.
      MgmtDBConnString = string.Format("SERVER={0};DATABASE={1};Integrated Security=SSPI", BTSMgmtDBServerName, BTSMgmtDBName);
     }
    }
   }
   return MgmtDBConnString;
  }

  public static PartyDetails GetPartyFromAlias(string Alias, string Qualifier)
  {
   PartyDetails ThisParty;
   const string GetnvcSIDQueryString = "SELECT bts_party.nvcName, nvcSID " +
       "FROM bts_party_alias INNER JOIN bts_party " +
       "     ON bts_party_alias.nPartyID = bts_party.nID " +
       "   WHERE nvcValue='{0}' AND nvcQualifier='{1}'";

   ThisParty.SID = "s-1-5-7";   // set default SID to "Guest"
   ThisParty.PartyName = "";

   SqlConnection BTSMgmtDBconn = new SqlConnection(GetMgmtDBConnectionString());
   
   //Build the querystring by populating the Alias and Qualifier into the base query
   SqlCommand PartyIdCMD = new SqlCommand(string.Format(GetnvcSIDQueryString, Alias, Qualifier), BTSMgmtDBconn);

   // any exceptions raised here will be reported to the event log by the messaging engine.
   BTSMgmtDBconn.Open();
   SqlDataReader PartyReader = PartyIdCMD.ExecuteReader(CommandBehavior.CloseConnection);
   if(PartyReader.HasRows)
   {
    PartyReader.Read();
    ThisParty.PartyName = PartyReader.GetString(0);
    ThisParty.SID = PartyReader.GetString(1);
   }
   PartyReader.Close();
   BTSMgmtDBconn.Close();

   return ThisParty;
  }
  public static string GetSIDFromAlias(string Alias, string Qualifier)
  {
   string ThisSID = string.Empty;
   const string GetnvcSIDQueryString = "SELECT  nvcSID " +
       "FROM bts_party_alias INNER JOIN bts_party " +
       "     ON bts_party_alias.nPartyID = bts_party.nID " +
       "   WHERE nvcValue='{0}' AND nvcQualifier='{1}'";

   SqlConnection BTSMgmtDBconn = new SqlConnection(GetMgmtDBConnectionString());
   
   //Build the querystring by populating the Alias and Qualifier into the base query
   SqlCommand PartyIdCMD = new SqlCommand(string.Format(GetnvcSIDQueryString, Alias, Qualifier), BTSMgmtDBconn);

   // any exceptions raised here will be reported to the event log by the messaging engine.
   BTSMgmtDBconn.Open();
   SqlDataReader PartyReader = PartyIdCMD.ExecuteReader(CommandBehavior.CloseConnection);
   if(PartyReader.HasRows)
   {
    PartyReader.Read();
    ThisSID = PartyReader.GetString(0);
   }
   PartyReader.Close();
   BTSMgmtDBconn.Close();

   return ThisSID;
  }
  public static string GetAliasFromSID(string SID, string Qualifier)
  {
   string ThisAlias = string.Empty;

   const string GetnvcSIDQueryString = "SELECT bts_party_alias.nvcValue " +
       "FROM bts_party_alias INNER JOIN bts_party " +
       "     ON bts_party_alias.nPartyID = bts_party.nID " +
       "   WHERE bts_party.nvcSID='{0}' AND bts_party_alias.nvcQualifier='{1}'";

   SqlConnection BTSMgmtDBconn = new SqlConnection(GetMgmtDBConnectionString());
   
   //Build the querystring by populating the SID and Qualifier into the base query
   SqlCommand PartyIdCMD = new SqlCommand(string.Format(GetnvcSIDQueryString, SID, Qualifier), BTSMgmtDBconn);

   // any exceptions raised here will be reported to the event log by the messaging engine.
   BTSMgmtDBconn.Open();
   SqlDataReader PartyReader = PartyIdCMD.ExecuteReader(CommandBehavior.CloseConnection);
   if(PartyReader.HasRows)
   {
    PartyReader.Read();
    ThisAlias = PartyReader.GetString(0);
   }
   PartyReader.Close();
   BTSMgmtDBconn.Close();

   return ThisAlias;
  }

  public static string GetCustomPropertyFromSID(string SID, string customPropertyName)
  {
   string ThisProperty = string.Empty;

   const string GetnvcSIDQueryString = "SELECT bts_party.nvcCustomData " +
       "FROM bts_party" +
       "   WHERE bts_party.nvcSID='{0}'";

   SqlConnection BTSMgmtDBconn = new SqlConnection(GetMgmtDBConnectionString());
   
   //Build the querystring by populating the SID and Qualifier into the base query
   SqlCommand PartyIdCMD = new SqlCommand(string.Format(GetnvcSIDQueryString, SID), BTSMgmtDBconn);

   // any exceptions raised here will be reported to the event log by the messaging engine.
   BTSMgmtDBconn.Open();
   SqlDataReader PartyReader = PartyIdCMD.ExecuteReader(CommandBehavior.CloseConnection);
   if(PartyReader.HasRows)
   {
    try
    {
     PartyReader.Read();
     XmlDocument customData = new XmlDocument();
     customData.LoadXml( PartyReader.GetString(0));
     XmlNode propertyNode = customData.SelectSingleNode("/Properties/" + customPropertyName);
     ThisProperty = propertyNode.InnerText;
    }
    catch(Exception)
    { }
   }
   PartyReader.Close();
   BTSMgmtDBconn.Close();

   return ThisProperty;
  }
 }
}