# Update Subscriber Attributes using the Update Method

Updating subscriber attributes via the SOAP API allows you to better maintain your subscriber information while maintaining a tight integration with your system or development environment.

Use these code examples to construct your own API update calls. You must include the unique identifier (either an email address or subscriber key) and the name-value pairs for the attributes to be updated. You must create a new record if you want to create a new value for the subscriber's primary key.

## .NET Code Example

```csharp
//Create Subscriber object [Subscribers > My Subscribers > All Subscribers]
            Subscriber sub = new Subscriber();
            sub.SubscriberKey = "12345";//required //may not be active in all accounts //use EmailAddress instead
                sub.EmailAddress = "tester@example.com";//required
            sub.EmailTypePreference = EmailType.Text;//EmailType.HTML is the default this only needs to be set to override to Text
            sub.EmailTypePreferenceSpecified = true;
            //Create an Array of Lists
            sub.Lists = new SubscriberList[2];//If a list is not specified the Subscriber is added to the "All Subscribers" List
            sub.Lists[0] = new SubscriberList();
            sub.Lists[0].ID = 123;//Available in the UI via List Properties
            sub.Lists[0].IDSpecified = true;
            sub.Lists[1] = new SubscriberList();
            sub.Lists[1].ID = 1234;//Available in the UI via List Properties
            sub.Lists[1].IDSpecified = true;
            //Subscriber Attributes differ per account.  Some may be required to create a Subscriber
            sub.Attributes = new etAPI.Attribute[2];
            sub.Attributes[0] = new etAPI.Attribute();
            sub.Attributes[0].Name = "First Name";
            sub.Attributes[0].Value = "Updated";
            sub.Attributes[1] = new etAPI.Attribute();
            sub.Attributes[1].Name = "Last Name";
            sub.Attributes[1].Value = "ViaAPI";
            try
            {
                string uRequestID = String.Empty;
                string uStatus = String.Empty;
                //Call the Update method on the Subscriber object
                UpdateResult[] uResults = client.Update(new UpdateOptions(), new APIObject[] { sub }, out uRequestID, out uStatus);
                //Display Results
                lblMessage.Text += "Overall Status: " + uStatus;
                lblMessage.Text += "<br/>";
                lblMessage.Text += "Number of Results: " + uResults.Length;
                lblMessage.Text += "<br/>";
                //Loop through each object returned and display the StatusMessage
                foreach (UpdateResult ur in uResults)
                {
                    lblMessage.Text += "Status Message: " + ur.StatusMessage;
                    lblMessage.Text += "<br/>";
                }
            }
            catch (Exception ex)
            {
                //Set Message
                lblMessage.Text += "<br/><br/>UPDATE ERROR:<br/>" + ex.Message;
            }
        }
        catch (Exception exc)
        {
            //Set Message
            lblMessage.Text += "<br/><br/>###ERROR<br/>" + exc.Message;
        }
    }
}
```

```csharp
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using etAPI;
public partial class SubscriberUpdate : System.Web.UI.Page
{
    //Global Variables
    private SoapClient client = new SoapClient();
    protected void Page_Load(object sender, EventArgs e)
    {
        //Authenticate
        client.ClientCredentials.UserName.UserName = System.Configuration.ConfigurationSettings.AppSettings["wsUserName"];
        client.ClientCredentials.UserName.Password = System.Configuration.ConfigurationSettings.AppSettings["wsPassword"];
        if (!IsPostBack)
        {
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            //Create Subscriber object [Subscribers > My Subscribers > All Subscribers]
            Subscriber sub = new Subscriber();
            sub.SubscriberKey = "0613d278-888e-4825-b796-74a21a071391";//required //may not be active in all accounts //use EmailAddress instead
            sub.EmailTypePreference = EmailType.Text;//EmailType.HTML is the default this only needs to be set to override to Text
            sub.EmailTypePreferenceSpecified = true;
            //Create an Array of Lists
            sub.Lists = new SubscriberList[2];//If a list is not specified the Subscriber is added to the "All Subscribers" List
            sub.Lists[0] = new SubscriberList();
            sub.Lists[0].ID = 182857;//Available in the UI via List Properties
            sub.Lists[0].IDSpecified = true;
            sub.Lists[1] = new SubscriberList();
            sub.Lists[1].ID = 947244;//Available in the UI via List Properties
            sub.Lists[1].IDSpecified = true;
            //Subscriber Attributes differ per account.  Some may be required to create a Subscriber
            sub.Attributes = new etAPI.Attribute[2];
            sub.Attributes[0] = new etAPI.Attribute();
            sub.Attributes[0].Name = "First Name";
            sub.Attributes[0].Value = "Updated";
            sub.Attributes[1] = new etAPI.Attribute();
            sub.Attributes[1].Name = "Last Name";
            sub.Attributes[1].Value = "ViaAPI";
            try
            {
                string uRequestID = String.Empty;
                string uStatus = String.Empty;
                //Call the Update method on the Subscriber object
                UpdateResult[] uResults = client.Update(new UpdateOptions(), new APIObject[] { sub }, out uRequestID, out uStatus);
                //Display Results
                lblMessage.Text += "Overall Status: " + uStatus;
                lblMessage.Text += "<br/>";
                lblMessage.Text += "Number of Results: " + uResults.Length;
                lblMessage.Text += "<br/>";
                //Loop through each object returned and display the StatusMessage
                foreach (UpdateResult ur in uResults)
                {
                    lblMessage.Text += "Status Message: " + ur.StatusMessage;
                    lblMessage.Text += "<br/>";
                }
            }
            catch (Exception ex)
            {
                //Set Message
                lblMessage.Text += "<br/><br/>UPDATE ERROR:<br/>" + ex.Message;
            }
        }
        catch (Exception exc)
        {
            //Set Message
            lblMessage.Text += "<br/><br/>###ERROR<br/>" + exc.Message;
        }
    }
}
```

The code sample below demonstrates how to unsubscribe a subscriber. Note that StatusSpecified must be set to true in order to change the status.

```csharp
Subscriber subscriber = new Subscriber()
            {
                EmailAddress = emailAddress,
                // If your account does not have subscriber key enabled, ensure the following line is commented-out
                CustomerKey = subscriberKey,
                Status = SubscriberStatus.Unsubscribed,
                StatusSpecified = true
            };

            //Specifies this as an upsert call
            UpdateOptions options = new UpdateOptions
            {
                SaveOptions = new SaveOption[] { new SaveOption {SaveAction = SaveAction.UpdateAdd, PropertyName = '*'} }
            };
            string requestID, requestStatus;
            //Executes the update API call
            UpdateResult[] results = soapClient.Update(options, new APIObject[] { subscriber }, out requestID, out requestStatus);
```

### Sample Java Code (Axis 1.4)

This code example updates subscriber information and creates a new subscriber if the application can’t find the appropriate record:

```csharp
public void testUpdateDataExtension() throws RemoteException {
    Subscriber subscriber = new Subscriber();
    subscriber.setEmailAddress("aruiz@example.com"); // updating to new email address.
    subscriber.setSubscriberKey("aruiz_key"); //subscriber unique, can’t update
    /*subscriber.Status = SubscriberStatus.Unsubscribed;
        SubscriberList list = new SubscriberList();
        list.ID = 12345; //List in which subscriber is there
        subscriber.Lists = new SubscriberList[] { list };*/
    Attribute attribute = new Attribute();
    attribute.setName("username");
    attribute.setValue("Angel");
    subscriber.setAttributes(new Attribute[]{attribute});
    //upsert option, it adds if subscriber is not present or updates if subscriber exists in system
    CreateOptions co = new CreateOptions();
    SaveOption saveOption = new SaveOption();
    saveOption.setSaveAction(SaveAction.UpdateAdd);
    saveOption.setPropertyName("*");
    co.setSaveOptions(new SaveOption[]{saveOption});
    Soap stub = init();
    CreateRequest createRequest = new CreateRequest(co, new APIObject[]{subscriber});
    CreateResponse createResponse = stub.create(createRequest);
    System.out.println("CreateResposne :::: " + createResponse.getOverallStatus());
}
```

This code example updates a subscriber status to Unsubscribed.

```csharp
public void testUpdateSubcriberByKey() throws RemoteException {
    Soap stub = init();
    UpdateOptions updateOptions = new UpdateOptions();
    Subscriber sub = new Subscriber();
    sub.setSubscriberKey("aruiz@example.com");
    sub.setStatus(SubscriberStatus.Unsubscribed);
    UpdateRequest updateRequest = new UpdateRequest(updateOptions, new APIObject[]{sub});
    UpdateResponse updateResponse = stub.update(updateRequest);
    System.out.println("updateResponse :::: " + updateResponse.getOverallStatus());
}
```

## Java (CXF) Code Example

```java
// Updates a subscriber's information (status in this example).
public void testUpdateSubcriberByKey() throws RemoteException {
    try {
        Soap stub = init();

            UpdateOptions updateOptions = new UpdateOptions();

            Subscriber sub = new Subscriber();
            sub.setSubscriberKey("jdoe@example.com");
            sub.setStatus(SubscriberStatus.UNSUBSCRIBED);

            APIObject[] apiObjects = {sub};

            UpdateRequest updateRequest = new UpdateRequest();
            updateRequest.getObjects().addAll(Arrays.asList(apiObjects));
            updateRequest.setOptions(updateOptions);

            UpdateResponse updateResponse = stub.update(updateRequest);
            System.out.println("updateResponse :::: " + updateResponse.getOverallStatus());

            } catch (Exception e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.

    }
}
```

## SOAP Envelope Example

```xml
<soap-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://exacttarget.com/wsdl/partnerAPI" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <SOAP-ENV:Header>
      <wsse:Security SOAP-ENV:mustUnderstand="1" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
         <wsse:UsernameToken>
            <wsse:Username>xxx</wsse:Username>
            <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">xxx</wsse:Password>
         </wsse:UsernameToken>
      </wsse:Security>
   </SOAP-ENV:Header>
   <SOAP-ENV:Body>
      <ns1:UpdateRequest>
         <ns1:Options/>
         <ns1:Objects xsi:type="ns1:Subscriber">
            <ns1:Attributes>
               <ns1:Name>First Name</ns1:Name>
               <ns1:Value>Updated</ns1:Value>
            </ns1:Attributes>
            <ns1:Attributes>
               <ns1:Name>Last Name</ns1:Name>
               <ns1:Value>ViaAPI</ns1:Value>
            </ns1:Attributes>
            <ns1:SubscriberKey>012345</ns1:SubscriberKey>
            <ns1:Lists>
               <ns1:ID>12345</ns1:ID>
            </ns1:Lists>
            <ns1:Lists>
               <ns1:ID>12346</ns1:ID>
            </ns1:Lists>
         </ns1:Objects>
      </ns1:UpdateRequest>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>
```
