Can I Write a Custom Plugin to send Data to Azure Service Bus Topic, Where I Can see the OOB plugins like contact, account were there in internet.
My Question;
How I Can Register a custom plugin under Service endpoint created for Service bus
The same example for OOB Plugin Has registered to send data to Azure Service Bus
http://sachinkumar.strikingly.com/blog/registering-service-endpoint-for-azure-service-bus-topic-in-microsoft
I have written Custom Code but I am Confused how to register my custom plugin under Service endpoint??? Can Any one help on this
The code is below
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xrm.Sdk;
using System.IO;
using System.Xml;
using System.Runtime.Serialization;
using Microsoft.ServiceBus.Messaging;
using Newtonsoft.Json;
using System.Security;
namespace AccWildcatToSAPH2SFLCValuesTransfer
{
public class SAPH2SFLCValuesTransfer : IPlugin
{
Entity location = null;
Entity currentValues = null;
string flc = string.Empty;
//decimal higestH2S = 0;
string l_s_error_log = string.Empty;
string l_s_flc = string.Empty;
decimal l_s_higestH2S = 0m;
public void Execute(IServiceProvider serviceProvider)
{
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = factory.CreateOrganizationService(context.UserId);
ITracingService tracing = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
if (context.Depth > 1)
{
tracing.Trace("Depth is more than one ");
return;
}
if (context.PrimaryEntityName == "hss05_wellsite" && (context.MessageName == "Update"))
{
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
location = (Entity)context.InputParameters["Target"];
currentValues = service.Retrieve(context.PrimaryEntityName, context.PrimaryEntityId, new Microsoft.Xrm.Sdk.Query.ColumnSet("hss05_functionallocationcode","hss05_highesth2sreadingatlocation"));
if (location.Attributes.Contains("hss05_highesth2sreadingatlocation"))
{
if(currentValues.Attributes.Contains("hss05_functionallocationcode") && currentValues.Attributes["hss05_functionallocationcode"]!= null)
{
l_s_flc = location.GetAttributeValue<string>("hss05_functionallocationcode");
}
if(currentValues.Attributes.Contains("hss05_highesth2sreadingatlocation") && currentValues.Attributes["hss05_highesth2sreadingatlocation"]!= null)
{
l_s_higestH2S = location.GetAttributeValue<decimal>("hss05_highesth2sreadingatlocation");
}
var messageData = new
{
flc = l_s_flc,
higestH2S = l_s_higestH2S
};
string connectionString = "Endpoint=sb://notificationdetailqa.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=vicGNY8RwUWWtdaWi5U779/RmI+ino1mlRavPCECjBI=";
string senderTopicName = "notificationcreation";
//string connectionString = GetConfigDataString(_pluginConfiguration, "connectionString");
//string senderTopicName = GetConfigDataString(_pluginConfiguration, "senderTopicName");
var client = TopicClient.CreateFromConnectionString(connectionString, senderTopicName);
Stream stream = GenerateStreamFromString((JsonConvert.SerializeObject(new { NotificationDetails = messageData })));
Console.WriteLine(stream);
var xmlmessage = new BrokeredMessage(stream);
client.Send(xmlmessage);
throw new InvalidPluginExecutionException("Succesfully Executed");
//location["hss05_notificationstatusflag"] = "3 : Notification Details Sent to SAP";
}
}
}
}
public static string GetConfigDataString(XmlDocument doc, string label)
{
return GetValueNode(doc, label);
}
private static string GetValueNode(XmlDocument doc, string key)
{
XmlNode node = doc.SelectSingleNode(String.Format("Settings/setting[@name='{0}']", key));
if (node != null)
{
return node.SelectSingleNode("value").InnerText;
}
return string.Empty;
}
public void WriteLog(string strLog)
{
StreamWriter log;
FileStream fileStream = null;
DirectoryInfo logDirInfo = null;
FileInfo logFileInfo;
string logFilePath = "D:\\Wildcat_SAP_log\\";
logFilePath = logFilePath + "Log-" + System.DateTime.Today.ToString("MM-dd-yyyy") + "." + "txt";
logFileInfo = new FileInfo(logFilePath);
logDirInfo = new DirectoryInfo(logFileInfo.DirectoryName);
if (!logDirInfo.Exists) logDirInfo.Create();
if (!logFileInfo.Exists)
{
fileStream = logFileInfo.Create();
}
else
{
fileStream = new FileStream(logFilePath, FileMode.Append);
}
log = new StreamWriter(fileStream);
log.WriteLine(strLog);
log.Close();
}
private static Stream GenerateStreamFromString(string s)
{
MemoryStream stream = new MemoryStream();
StreamWriter writer = new StreamWriter(stream);
writer.Write(s);
writer.Flush();
stream.Position = 0;
return stream;
}
}
}