1
Sample .NET code for consuming API
Question asked by Connie DeCinko - 6/7/2016 at 1:36 PM
Answered
Where can I find some clean sample code for consuming the SM web services in a C# MVC5 controller?  For example, to start with something simple, how would I use the svcMailListAdmin API to GetMailingListsByDomain?  For now, just return all the mailing lists and display in a table or grid.
 
I added svcMailListAdmin as a Web Reference in my project.  Where do I specify the AuthUserName and AuthPassword?  How do pass in the domain?
 
 
 

15 Replies

Reply to Thread
1
Matt Petty Replied
Employee Post Marked As Answer
I created this for you and anyone else that could find it useful.
I pulled code from http://www.diogonunes.com/blog/calling-a-web-method-in-c-without-a-service-reference/
Project: https://www.carbonitex.net/share/SmarterMailConsoleApp.zip
 
I just put one example of creating a user in it but hopefully it gives a nice clean example of using the API.
SmarterMail 16 will have a full REST driven API which is MUCH easier to use, until then SOAP.
 
Matt Petty Software Developer SmarterTools Inc. (877) 357-6278 www.smartertools.com
0
Connie DeCinko Replied
Come on SM16! I fear I'll do all this work for SM15 and before we can go live I'll have to rewrite it all for SM16.
0
Matt Petty Replied
Employee Post
We will have a beta period for SM16, could use that time for conversion. Depending on how you end up writing your calls switching to a REST api wouldn't be too crazy, depending on what all you plan on doing.
Matt Petty Software Developer SmarterTools Inc. (877) 357-6278 www.smartertools.com
0
Connie DeCinko Replied
Thanks. That worked pretty well for calling the web service. Now, how do I deal with the results? For example, calling GetMailingListsByDomain just returns <Result>true</Result>. How do I get the listNames? I see it in the SOAP response. Do I have to parse it out?
0
Matt Petty Replied
Employee Post
Switch svcUserAdmin.ResultString with svcUserAdmin.ResultXML
and it will return you XML (xdocument object) that you can scan through and pull the data you need.
Matt Petty Software Developer SmarterTools Inc. (877) 357-6278 www.smartertools.com
0
Connie DeCinko Replied
svcMailListAdmin.ResultXML still gives me <Result>true</Result>, at least when I cast to a string and dump it to my view.
0
Connie DeCinko Replied
Ok, I'm getting closer.  Here is my code so far.  It dumps a string of the Mailing Lists to the page.  The values look good.  Now to figure out how to correctly sort by list name and turn the XML into a list.
 
        public ActionResult MailingLists()
        {
            svcMailListAdmin.PreInvoke();
            svcMailListAdmin.AddParameter("AuthUserName", SM_AuthUserName);
            svcMailListAdmin.AddParameter("AuthPassword", SM_AuthPassword);
            svcMailListAdmin.AddParameter("DomainName", SM_DomainName);

            svcMailListAdmin.Invoke("GetMailingListsByDomain");
            svcMailListAdmin.PostInvoke();

            var resultStr = svcMailListAdmin.ResultXML;

            var types = from x in resultStr.Root.Descendants("listNames")
                        orderby (string) x.Attribute("string")
                        select x.Value;

            return View((object)types.Single());
        }
 
0
Connie DeCinko Replied
Figured out, you have to remove FirstNode from this code in WebServices.cs if you want all the nodes returned, not just the Result node.

ResultXML = XDocument.Parse(webMethodResult.FirstNode.ToString());
0
Connie DeCinko Replied
Got it, at least a simple display list of all MailingLists.
 
public ActionResult MailingLists()
{
    svcMailListAdmin.PreInvoke();
    svcMailListAdmin.AddParameter("AuthUserName", SM_AuthUserName);
    svcMailListAdmin.AddParameter("AuthPassword", SM_AuthPassword);
    svcMailListAdmin.AddParameter("DomainName", SM_DomainName);

    svcMailListAdmin.Invoke("GetMailingListsByDomain");
    svcMailListAdmin.PostInvoke();

    var resultXML = svcMailListAdmin.ResultXML;

    List<string> resultList = resultXML.Root.Elements("listNames")
                        .Elements("string")
                        .OrderBy(x => (string)x)
                        .Select(x => (string)x)
                        .ToList();

    return View((object)resultList);
}
 
1
Merle Wait Replied
Will the SOAP portion be discontinued with SM16?? Have i written SOAP for naught?
0
Matt Petty Replied
Employee Post
I will ask around and find out.
Matt Petty Software Developer SmarterTools Inc. (877) 357-6278 www.smartertools.com
0
Matt Petty Replied
Employee Post
Yes the SOAP API will still be included on SM16 for backwards compatibility.
Matt Petty Software Developer SmarterTools Inc. (877) 357-6278 www.smartertools.com
0
Merle Wait Replied
thanks.. appreciate that... and looking forward to using REST to update Contacts ( I have that on my list as an SM16 feature) <grin> Can't wait. When SM16 beta comes out.. I will be onboard
0
Matt Petty Replied
Employee Post
There will be a REST api for everything. Our new interface which does all the stuff, will be using our API. So sending email, moving email, settings, contacts, literally everything will have an API. :D
You get an api... you get an api... you all get api's!
Matt Petty Software Developer SmarterTools Inc. (877) 357-6278 www.smartertools.com
0
Connie DeCinko Replied
How much longer before the API docs are ready?

Reply to Thread