Sample .NET code for consuming API
Question asked by Connie DeCinko - June 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?
 
 
 

4 Replies

Reply to Thread
1
Matt Petty Replied
Employee Post
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
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
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
Will the SOAP portion be discontinued with SM16?? Have i written SOAP for naught?

Reply to Thread