Try our conversational search powered by Generative AI!

Programmatically add recipients to List

Vote:
 

EPiServer 7

 

Following this sample link below I can add receipentsw but want to also add attributes is this possible via API?

http://world.episerver.com/Blogs/Per-Nergard/Dates/2013/1/Programmatically-add-recipients-to-an-EPiServer-Mail-recipient-list/

 

Would I need to call the store procedures

 

spEPiServerMailAddAttribute

spEPiServerMailAddAttributeValue

 

Or is there an API method?

 

or should I implement custom IRecipientSource, the standard gives me everything I need if I can add the Attributes

 

----------------------------------

Current source code so far:

----------------------------------

public class RecipientLists
{
/// <summary>
/// Subscribers email address
/// </summary>
public string Email { get; set; }
/// <summary>
/// Subscribers extended attributes
/// </summary>
public AttributeCollection AttributeCollection { get; set; }
/// <summary>
/// Recipient List Container name
/// </summary>
public string Container { get; set; }


public RecipientLists()
{
}


#region Public Methods
public void Subscribe()
{

// Instantiate a source object
Source store = new Source();

// From the source we can get the recipientcontainer by name.
RecipientContainer container = store.GetRecipientContainer(this.Container);

// If we don't do a dupe check it's possible to add the same address several times to the same recipientlist.
if (container != null)
{
RecipientCollection collection = container.GetRecipients();
var rcp = collection.FirstOrDefault<Recipient>(p => p.Email == this.Email);
if (rcp == null)
{
// Create a new recipent with the given email address.
Recipient subscriber = new Recipient(this.Email.Trim(), false, this.AttributeCollection);
AddRecipient(subscriber, container, subscriber.Attributes);
}
}

}
#endregion

#region Private Methods


/// <summary>
/// Add a Recipient
/// </summary>
/// <param name="recipient"></param>
/// <param name="recipientContainer"></param>
/// <param name="attributes"></param>
private void AddRecipient(Recipient recipient, RecipientContainer recipientContainer, AttributeCollection attributes)
{

var databaseHandler = ServiceLocator.Current.GetInstance<IDatabaseHandler>();

databaseHandler.ExecuteTransaction(() =>
{
Guid recipientId = (Guid)databaseHandler.GetScalar("spEPiServerMailAddRecipient", new object[] { recipient.Email, recipient.IsUnsubscribed, recipientContainer.ID });

// Add Attribute Values
AddAttributeValues(recipientId, recipient.Attributes, recipientContainer);
});

}


private void AddAttributeValues(Guid recipientId, AttributeCollection attributes, RecipientContainer recipientContainer)
{
Source store = new Source();
Recipient recipient = store.GetRecipient(recipientId);

throw new NotImplementedException();


//foreach (EPiServer.Mail.Core.Attribute attribute in attributes)
//{
// if(!recipient.Attributes.Contains(attribute))
// {
// recipient.Attributes.Add(attribute);
// }
//}

}

#endregion


}

 

 

#70786
Apr 30, 2013 11:29
Vote:
 

Have now got a working solution using EPiServer Store procedures

 

---------------------------------------------------------------------

public class RecipientLists
{
/// <summary>
/// Subscribers email address
/// </summary>
public string Email { get; set; }
/// <summary>
/// Subscribers extended attributes
/// </summary>
public AttributeCollection AttributeCollection { get; set; }
/// <summary>
/// Recipient List Container name
/// </summary>
public string Container { get; set; }


/// <summary>
/// Recipient Lists
/// </summary>
public RecipientLists()
{
}

#region Public Methods

public void Subscribe()
{

// Instantiate a source object
Source store = new Source();

// From the source we can get the recipientcontainer by name.
RecipientContainer container = store.GetRecipientContainer(this.Container);

// If we don't do a dupe check it's possible to add the same address several times to the same recipientlist.
if (container != null)
{
RecipientCollection collection = container.GetRecipients();
var rcp = collection.FirstOrDefault<Recipient>(p => p.Email == this.Email);
if (rcp == null)
{
// Create a new recipent with the given email address.
Recipient subscriber = new Recipient(this.Email.Trim(), false, this.AttributeCollection);
AddRecipient(subscriber, container, subscriber.Attributes);
}
}

}

#endregion

#region Private Methods
/// <summary>
/// Add a Recipient
/// </summary>
/// <param name="recipient"></param>
/// <param name="recipientContainer"></param>
/// <param name="attributes"></param>
private void AddRecipient(Recipient recipient, RecipientContainer recipientContainer, AttributeCollection attributes)
{

var databaseHandler = ServiceLocator.Current.GetInstance<IDatabaseHandler>();

databaseHandler.ExecuteTransaction(() =>
{
Guid recipientId = (Guid)databaseHandler.GetScalar("spEPiServerMailAddRecipient", new object[] { recipient.Email, recipient.IsUnsubscribed, recipientContainer.ID });

// Update Number Of Recipients
if (recipientId != null)
{
databaseHandler.GetScalar("spEPiServerMailUpdateRecipientContainer", new object[] { recipientContainer.ID, recipientContainer.Name, recipientContainer.NumOfRecipients + 1 });
}

// Add Attribute Values
AddAttributeValues(recipientId, recipient.Attributes, recipientContainer, databaseHandler);
});

}

/// <summary>
/// Add Attribute Values to recipient Container and add recipients value for an attribute
/// </summary>
/// <param name="recipientId"></param>
/// <param name="attributes"></param>
/// <param name="recipientContainer"></param>
/// <param name="databaseHandler"></param>
private void AddAttributeValues(Guid recipientId, AttributeCollection attributes, RecipientContainer recipientContainer, IDatabaseHandler databaseHandler)
{

List<ContainerAttribute> containerAttributes = new List<ContainerAttribute>();

// Populate container Attributes
using (IDataReader dr = databaseHandler.GetReader("spEPiServerMailGetAttributeNamesByContainer", new object[] { recipientContainer.ID }))
{
while (dr.Read())
{
ContainerAttribute containerAttribute = new ContainerAttribute();
containerAttribute.ID = dr.GetInt32(dr.GetOrdinal("intID"));
containerAttribute.Name = dr.GetString(dr.GetOrdinal("strName"));
containerAttributes.Add(containerAttribute);
}
}

var index = 1;
foreach (EPiServer.Mail.Core.Attribute attribute in attributes)
{
int attributeId;

// Check if attribute exists in Container List
var containerAttribute = containerAttributes.FirstOrDefault(x => x.Name == attribute.Name);
if (containerAttribute != null)
{
attributeId = containerAttribute.ID;
}
else
{
// Adds an attribute to a recipient container
attributeId = Convert.ToInt32(databaseHandler.GetScalar("spEPiServerMailAddAttribute", new object[] { attribute.Name, index, recipientContainer.ID }));
}

// Check if attributeId has been set
if (attributeId != 0)
{
// Adds a recipients value for an attribute
databaseHandler.GetScalar("spEPiServerMailAddAttributeValue", new object[] { attributeId, recipientId, attribute.Value });
}

index++;

}
}
#endregion


#region Private
/// <summary>
/// Container Attribute
/// </summary>
private class ContainerAttribute
{
public int ID { get; set; }
public string Name { get; set; }
}
#endregion
}

#70796
Apr 30, 2013 13:56
This thread is locked and should be used for reference only. Please use the Legacy add-ons forum to open new discussions.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.