Showing posts with label Json. Show all posts
Showing posts with label Json. Show all posts

Wednesday, July 20, 2016

Easily construct Outlook Group Connector JSON messages in C#

Hi All,

If you are building an Outlook Group Connector that you are spending a lot of time writing JSON message & specifying different schema elements and attributes to be able to build a canvas that looks like this below figure, so i got good news for you!




I got your back and published an Outlook Group Connector SDK ver. 1.1 nuget package that includes tons of extension methods and features that helps your easily build your JSON payload message.


How to send a message in C# to a group:


                Message message = new Message()
                {
                    summary = "This is the subject for the sent message to an outlook group",
                    title = msg
                };
                message.AddSection(nSec1);
                message.AddFacts("Facts", facts);
                message.AddImages("Images", images);
                message.AddAction("check details here", "http://mostafaelzoghbi.com");


                var result = await message.Send(webhookUrl);


GitHub Code and Sample links:

1) GitHub Repo for SDK and Samples apps including console & web apps (link).
2) NuGet package that has been published to use it in your apps (link) or search for "Office365ConnectorSDK" in VS 2015.

Hope this helps.

Sunday, April 12, 2015

How to return JSON objects from Web API 2

Hi All,

I was trying to return an object from web api 2.0 and i was getting the following error:

The 'ObjectContent`1' type failed to serialize the response body for content type 'application/xml; charset=utf-8'.

The inner exception message was stating the following:

Type 'Newtonsoft.Json.Linq.JToken' is a recursive collection data contract which is not supported. Consider modifying the definition of collection 'Newtonsoft.Json.Linq.JToken' to remove references to itself.


Sample method:

[HttpGet]
[ResponseType(typeof(ChargifyTransaction))]      
public async Task<MyObject> GetTransactionDetails(string transactionId)

{

   MyObject obj = new MyObject();
   obj.Id=123222;
   obj.Name = "Mostafa dev";
   
   return obj;
}

The solution:

Add the following 2 lines in Global.asax file under App_Start method, This will force the web api to return Json formats and to ignore references to itself for Newtonsoft.Json dll.



 protected void Application_Start()
        {
            // Force to use Json formatter and ignore reference looping.
            GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
            GlobalConfiguration.Configuration.Formatters.Remove(GlobalConfiguration.Configuration.Formatters.XmlFormatter);


           }


Hope this helps.