Find herewith a list of examples useful to manage .NET Code
Parameters to login to Web_Service
First of all, you need to get the following credentials to login to the service:
- Web Service address (WSDL), for example https://ws-mn1.mag-news.it/ws/wsapi?wsdl
- An access_token (see OAuth 2.0 documentation)
For security reasons, MagNews users are not enabled by default to the use of Web Service.
You need to be enabled to use these functions.
If you change the credentials of the user, you must also update the same credentials on your software.
For safe use of MagNews API, it is recommended to use version 2010 of Microsoft Visual Studio
Modification of a contact
Creates/updates a contact. If the primaryKey is not in the database, it creates a new contact. If the contacts already exists, it updates the contact with the primaryKey specified.
class myClassName { static void Main(string[] args) { try { MagNewsAPIClient client = new MagNewsAPIClient("MagNewsAPIPort", "https://ws-mn1.mag-news.it/ws/wsapi"); credentials credential = new credentials(); credential.password = "My_Access_Token"; //see OAuth 2 section. mnContactValue[] merge = new mnContactValue[1]; { mnContactValue value = new mnContactValue(); value.field = "EMAIL"; value.value = "myEmailAddress"; merge.SetValue(value, 0); } mnContactOperation contactResult = client.mergeContact("myIdDatabase", merge, null, credential); if (contactResult.ok) { Console.WriteLine(contactResult.idContact); } else { Console.WriteLine(contactResult.debug); } } catch (Exception a) { Console.WriteLine(a); } } }
Subscription of a contact
Creates a new contact in active status. If a contact with the same primaryKey already exists, then an error occurs. If you need to update an existing contact, it is recommended to use the function mergeContact.
class myClassName { static void Main(string[] args) { try { MagNewsAPIClient client = new MagNewsAPIClient("MagNewsAPIPort", "https://ws-mn1.mag-news.it/ws/wsapi"); credentials credential = new credentials(); credential.password = "My_Access_Token"; //see OAuth 2 section. mnContactValue[] subscribe = new mnContactValue[1]; { mnContactValue value = new mnContactValue(); value.field = "EMAIL"; value.value = "myEmailAddress"; subscribe.SetValue(value, 0); } mnContactOperation contactResult = client.mergeContact("myIdDatabase", subscribe, null, credential); if (contactResult.ok) { Console.WriteLine(contactResult.idContact); } else { Console.WriteLine(contactResult.debug); } } catch (Exception a) { Console.WriteLine(a); } } }
Unsubscription of a contact
Unsubscribes a contact.
class myClassName { static void Main(string[] args) { try { MagNewsAPIClient client = new MagNewsAPIClient("MagNewsAPIPort", "https://ws-mn1.mag-news.it/ws/wsapi"); credentials credential = new credentials(); credential.password = "My_Access_Token"; //see OAuth 2 section. mnContactValue[] unsubscribe = new mnContactValue[1]; { mnContactValue value = new mnContactValue(); value.field = "EMAIL"; value.value = "myEmailAddress"; unsubscribe.SetValue(value, 0); } mnContactOperation contactResult = client.unsubscribeContact("myIdDatabase", unsubscribe, null, credential); if (contactResult.ok) { Console.WriteLine(contactResult.idContact); } else { Console.WriteLine(contactResult.debug); } } catch (Exception a) { Console.WriteLine(a); } } }
Modification of the contact's password field
class myClassName { static void Main(string[] args) { try { MagNewsAPIClient client = new MagNewsAPIClient("MagNewsAPIPort", "https://ws-mn1.mag-news.it/ws/wsapi"); credentials credential = new credentials(); credential.password = "My_Access_Token"; //see OAuth 2 section. mnContactValue[] merge = new mnContactValue[2]; { mnContactValue value = new mnContactValue(); value.field = "EMAIL"; value.value = "myEmailAddress"; merge.SetValue(value, 0); } { mnContactValue value = new mnContactValue(); value.field = "PASSWORD"; value.value = "myPassword"; merge.SetValue(value, 1); } mnContactOperation contactResult = client.mergeContact("myIdDatabase", merge, null, credential); if (contactResult.ok) { Console.WriteLine(contactResult.idContact); } else { Console.WriteLine(contactResult.debug); } } catch (Exception a) { Console.WriteLine(a); } } }
Modification of the field Email when it is the primaryKey
class myClassName { static void Main(string[] args) { try { MagNewsAPIClient client = new MagNewsAPIClient("MagNewsAPIPort", "https://ws-mn1.mag-news.it/ws/wsapi"); credentials credential = new credentials(); credential.password = "My_Access_Token"; //see OAuth 2 section. mnContactValue[] merge = new mnContactValue[2]; { mnContactValue value = new mnContactValue(); value.field = "IDCONTACT"; value.value = "idContact"; merge.SetValue(value, 0); } { mnContactValue value = new mnContactValue(); value.field = "EMAIL"; value.value = "newEmailAddress"; merge.SetValue(value, 1); } mnContactOperation contactResult = client.mergeContact("myIdDatabase", merge, null, credential); if (contactResult.ok) { Console.WriteLine(contactResult.idContact); } else { Console.WriteLine(contactResult.debug); } } catch (Exception a) { Console.WriteLine(a); } } }
Send email message and get status
class myClassName { static void Main(string[] args) { try { MagNewsAPIClient client = new MagNewsAPIClient("MagNewsAPIPort", "https://ws-mn1.mag-news.it/ws/wsapi"); credentials credential = new credentials(); credential.password = "My_Access_Token"; //see OAuth 2 section. emailMessage em = new emailMessage(); em.fromemail = "myfromemail"; em.fromname = "myfromname"; em.htmlbody = "myHtmlBody"; em.subject = "mySubject"; em.to = "myEmailAddress"; // use instead "body" param, if you want to use newsletter or notification template already present in your account as body of your notification message. You can set options "usenewsletterastemplate=true" and "idnewsletter=xx" where "xx" is the id of the nesletter or notification template. mnSimpleMessageStatus msms = client.sendEmailMessage(em, null, credential); Console.WriteLine("messageID: " + msms.messageId); String result2 = client.getSimpleMessageStatus(msms.messageId, credential).actualStatus; while (!result2.Equals("SENT")) { Console.WriteLine("status: " + result2); result2 = client.getSimpleMessageStatus(msms.messageId, credential).actualStatus; break; } } catch (Exception a) { Console.WriteLine("Exception: " + a); } } }
Get all journeys
class myClassName { static void Main(string[] args) { try { MagNewsAPIClient client = new MagNewsAPIClient("MagNewsAPIPort", " https://ws-mn1.mag-news.it/ws/wsapi?wsdl"); credentials credential = new credentials(); credential.password = "My_Access_Token"; //see OAuth 2 section. mnCampaign[] listcamp = client.getAllCampaigns(credential); for (int a = 0; a <listcamp.length; a++)="a++)" {="{" mncampaign="mnCampaign" myc="listcamp[a];" system.console.writeline(="System.Console.WriteLine(" idcampaign:="idCampaign:" +="+" myc.idcampaign);="myC.idCampaign);" name:="name:" myc.name);="myC.name);" }="}" catch="catch" (exception="(Exception" a)="a)" console.writeline(="Console.WriteLine(" exception:="Exception:" a);="a);"></listcamp.length;>
Add contact info to a rowSet from file and insert contact to database
class myClassName { static void Main(string[] args) { try { MagNewsAPIClient client = new MagNewsAPIClient("MagNewsAPIPort", " https://ws-mn1.mag-news.it/ws/wsapi?wsdl"); credentials credential = new credentials(); credential.password = "My_Access_Token"; //see OAuth 2 section. String idRowSet = client.createContactRowSet(credential); fileOptions fo = new fileOptions(); string[] map = new string[1]; map[0] = "EMAIL"; fo.columnMapping = map; option[] listOptions = new option[5]; option opt1 = new option(); opt1.key = "encoding"; opt1.value = "windows-1252"; listOptions[0] = opt1; option opt2 = new option(); opt2.key = "field_separator"; opt2.value = ","; listOptions[1] = opt2; option opt3 = new option(); opt3.key = "lines_separator"; opt3.value = "\r\n"; listOptions[2] = opt3; option opt4 = new option(); opt4.key = "quotation_mark"; opt4.value = "\""; listOptions[3] = opt4; option opt5 = new option(); opt5.key = "use_quotation_mark"; opt5.value = "true"; listOptions[4] = opt5; fo.options = listOptions; byte[] file = System.IO.File.ReadAllBytes("myfile.csv"); client.addDataToContactsRowsSetFromFile(idRowSet, file, fo, null, credential); //start batch string operationId = client.startBatchContactsUpdate(idRowSet, new mnBatchStartOptions(), null, credential); mnBatchStatusInfo bsi = client.getBatchStatusInfo(operationId, credential); //when mnBatchStatusInfo.status is "FINISHED" mnContactOperation[] result = client.fetchBatchContactsUpdateReport(bsi.reportRowsSetId, 0, 100, credential); for (int count = 0; count <result.length; count++)="count++)" {="{" console.writeline(="Console.WriteLine(" actionresult:="actionResult:" +="+" result[count].actionresult);="result[count].actionResult);" customid:="customId:" result[count].customid);="result[count].customId);" debug:="debug:" result[count].debug);="result[count].debug);" idcontact:="idContact:" result[count].idcontact);="result[count].idContact);" isok:="isOK:" result[count].ok);="result[count].ok);" primarykeyvalue:="primaryKeyValue:" result[count].primarykeyvalue);="result[count].primaryKeyValue);" requestedaction:="requestedAction:" result[count].requestedaction);="result[count].requestedAction);" sendemailresult:="sendEmailResult:" result[count].sendemailresult);="result[count].sendEmailResult);" if="if" (!result[count].ok)="(!result[count].ok)" mnerror[]="mnError[]" listerrors="result[count].errors;" for="for" (int="(int" counterror="countError"><listerrors.length; counterror++)="countError++)" {="{" console.writeline(="Console.WriteLine(" errortype:="errorType:" +="+" listerrors[counterror].errortype);="listErrors[countError].errorType);" }="}" catch="catch" (exception="(Exception" a)="a)" exception:="Exception:" a);="a);"></listerrors.length;></result.length;>
Create communication
class myClassName { static void Main(string[] args) { try { MagNewsAPIClient client = new MagNewsAPIClient("MagNewsAPIPort", "https://ws-mn1.mag-news.it/ws/wsapi"); credentials credential = new credentials(); credential.password = "My_Access_Token"; //see OAuth 2 section. option[] option = new option[0]; string idCampaign = "myIDCampaign"; content content = new content(); content.name = "myNewsletterName"; //Test from HTML Content content.contentType = "text/html"; content.text = "myHtml"; //Test from URL Content content.contentType = "text/url"; content.text = "http://www.mysite.it/example.html"; string idNewsletter = client.createNewsletter(idCampaign, content, option, credential); Console.WriteLine("idNewsletter: " + idNewsletter); } catch (Exception a) { Console.WriteLine(a); } } }