Use the following example to get contacts from contact list:
Guid contactListId = Guid.NewGuid(); //Contact list Id
int batchSize = 200;
var expandOptions =
new ContactExpandOptions(CollectionModel.FacetKeys.PersonalInformation);
var segmentationEngine = ServiceLocator.ServiceProvider
.GetService<ISegmentationEngine>();
var listSubscriptionConditions = new ListSubscriptionMatches()
{
ListId = contactListId
};
var contacts = segmentationEngine
.FindContacts(listSubscriptionConditions, batchSize, expandOptions);
In order to get contacts from segmented list, first you should get referenced segment definitions and retrieve conditions from them:
var contactListProvider = ServiceLocator.ServiceProvider.GetService<IContactListProvider>();
var contactProvider = ServiceLocator.ServiceProvider.GetService<IContactProvider>();
var contactList = contactListProvider.Get(contactListId, cultureInfo);
var segments = contactList.SegmentDefinitionIds
.Select(id => segmentManager.Get(id, cultureInfo));
var ruleConditions = segments.SelectMany(x => x.Rules)
.Select(x => x.Condition);
Then use conditions to build query and get contacts:
int batchSize = 200;
var expandOptions = new ContactExpandOptions(CollectionModel.FacetKeys.PersonalInformation);
var contactSearchExpressionBuilder = ServiceLocator.ServiceProvider
.GetService<IContactSearchExpressionBuilder>();
var segmentationEngine = ServiceLocator.ServiceProvider
.GetService<ISegmentationEngine>();
var contactSearchQuery = contactSearchExpressionBuilder.BuildQueryFactories(ruleConditions)
.CombineUsingOr();
var contacts = segmentationEngine.FindContacts(contactSearchQuery, batchSize, expandOptions);
Update:
You need to add reference to Microsoft.Extensions.DependencyInjection; to not get next error "a non generic method cannot be used with type arguments" in ServiceLocator.ServiceProvider.GetService<IContactSearchExpressionBuilder>()
;