Surendra Sharma

Surendra Sharma

Search This Blog

Wednesday, July 22, 2015

How to create subscribers in ExactTarget using C# code



ExactTarget is a cloud service provided by Salesforce for running email campaign for email marketing. For this we need to create subscribers.
 
Here is a code for to create subscribers in ExactTarget via its web service.


Change necessary values in below code and call the method. That’s it.

static void AddSubscribers()
{
    //Account authentication information for an ExactTarget user. Access it from web.config
    SoapClient client = new SoapClient();
    client.ClientCredentials.UserName.UserName = "xyz";
    client.ClientCredentials.UserName.Password = "xyzpassword";

    //Create a new subscriber - person email id
    Subscriber newSub = new Subscriber();
    newSub.EmailAddress = "abc@test.com";
    newSub.SubscriberKey = "abc@test.com";

    //Create the subscriber attributes
    newSub.Attributes = new ExactTargetClient.Attribute[6];

    //Attribute - First Name
    newSub.Attributes[0] = new ExactTargetClient.Attribute();
    newSub.Attributes[0].Name = "First Name";
    newSub.Attributes[0].Value = "Enter your first Name Here";

    //Attribute - Last Name
    newSub.Attributes[1] = new ExactTargetClient.Attribute();
    newSub.Attributes[1].Name = "Last Name";
    newSub.Attributes[1].Value = "Enter your Last Name Here";

    //Attribute - Zip Code
    newSub.Attributes[2] = new ExactTargetClient.Attribute();
    newSub.Attributes[2].Name = "Zip Code";
    newSub.Attributes[2].Value = "110011";

    //Attribute - State
    newSub.Attributes[3] = new ExactTargetClient.Attribute();
    newSub.Attributes[3].Name = "State";
    newSub.Attributes[3].Value = "Johannesburg";

    //Attribute - City
    newSub.Attributes[4] = new ExactTargetClient.Attribute();
    newSub.Attributes[4].Name = "City";
    newSub.Attributes[4].Value = "Johannesburg";

    newSub.Attributes[5] = new ExactTargetClient.Attribute();
    newSub.Attributes[5].Name = "Country";
    newSub.Attributes[5].Value = "South Africa";

    //Specify the busiess unit ID
    newSub.Client = new ClientID();
       //Manage via a config file
    newSub.Client.ID = 1234567; 
    newSub.Client.IDSpecified = true;

    //Declare the list we want to add the subscriber to
    //Please manage these values via a configuration file
    SubscriberList mylist = new SubscriberList();
    mylist.ID = 1234;
    mylist.IDSpecified = true;

    //Relate the List to the Subscriber
    newSub.Lists = new SubscriberList[] { mylist };

    //Instantiate the CreateOptions object for the create call of TSD
    CreateOptions cOptionsTS = new CreateOptions();
    cOptionsTS.RequestType = RequestType.Asynchronous;
    cOptionsTS.RequestTypeSpecified = true;

    SaveOption saveOption = new SaveOption();
    saveOption.SaveAction = SaveAction.UpdateAdd;
    saveOption.PropertyName = "*";
    cOptionsTS.SaveOptions = new SaveOption[] { saveOption };

    try
    {
        //Create the request to add the subscriber
        string tsRequestID = "";
        string tsStatus = "";
        CreateResult[] tsResults = client.Create(cOptionsTS, new APIObject[] { newSub }, out tsRequestID, out tsStatus);

        Console.WriteLine("Overall Status ::: " + tsStatus);
        Console.WriteLine("Number of Results ::: " + tsStatus.Length);

        foreach (CreateResult result in tsResults)
        {
            Console.WriteLine("Status Code and Message ::: " + result.StatusCode + " " + result.StatusMessage);
        }

        Console.ReadLine();

        //Check for Error
        if (tsStatus != "OK") { throw new Exception(); }

    }
    catch (Exception exCreate)
    {
        Console.WriteLine(exCreate.Message);
        Console.ReadLine();
    }
}
 
Please leave your comments or share this code if it’s useful for you.

No comments:

Post a Comment