|
using System; |
|
using System.Collections.Generic; |
|
using System.Linq; |
|
using System.Web; |
|
using Telerik.Sitefinity.Personalization.Impl; |
|
using Telerik.Sitefinity.Personalization.Impl.Model; |
|
|
|
namespace SitefinityWebApp.Documentation.Samples.Personalization |
|
{ |
|
public class PersonalizationApi_CreateOperations |
|
{ |
|
public void CreateSegment(string name, string description, bool isActive = true) |
|
{ |
|
var personalizationManager = PersonalizationManager.GetManager(); |
|
var segment = personalizationManager.CreateSegment(); |
|
segment.Name = name; |
|
segment.Description = description; |
|
segment.IsActive = isActive; |
|
|
|
var ipAddress = "209.190.163.105"; |
|
var ipCriterion = CreateIpAddressCriterion(ipAddress); |
|
var sampleLocation = "United States"; |
|
var locationCriterion = CreateLocationCriterion(sampleLocation); |
|
|
|
var criteriaGroup = CreateCriterionGroup(new Criterion[] { ipCriterion, locationCriterion }); |
|
|
|
segment.CriteriaGroups.Add(criteriaGroup); |
|
personalizationManager.SaveChanges(); |
|
} |
|
|
|
/// <summary> |
|
/// Creates an instance of CriterionGroup. |
|
/// </summary> |
|
/// <param name="criteria">The set of criterions in the criterion group.</param> |
|
/// <returns>An instance of CriterionGroup.</returns> |
|
public CriteriaGroup CreateCriterionGroup(params Criterion[] criteria) |
|
{ |
|
var group = new CriteriaGroup(); |
|
foreach (var criterion in criteria) |
|
{ |
|
group.Criteria.Add(criterion); |
|
} |
|
|
|
return group; |
|
} |
|
|
|
/// <summary> |
|
/// Creates a criterion for Location. |
|
/// </summary> |
|
/// <param name="location">The location. Examples: "Bulgaria", "Germany".</param> |
|
/// <param name="isNegated">Whether the criterion is negated.</param> |
|
/// <returns>A new criterion instance.</returns> |
|
public Criterion CreateLocationCriterion(string location, bool isNegated = false) |
|
{ |
|
string criterionValue = "[{{\"Location\":\"{0}\",\"Index\":0}}]".Arrange(location); |
|
Criterion criterion = new Criterion() |
|
{ |
|
CriterionName = "Location", |
|
CriterionValue = criterionValue, |
|
CriterionDisplayValue = location, |
|
CriterionTitle = "Location", |
|
IsNegated = isNegated |
|
}; |
|
return criterion; |
|
} |
|
|
|
/// <summary> |
|
/// Creates a criterion for IP address. |
|
/// </summary> |
|
/// <param name="ipAddress">The IP address.</param> |
|
/// <param name="isNegated">Whether the criterion is negated.</param> |
|
/// <returns>Criterion instance</returns> |
|
public Criterion CreateIpAddressCriterion(string ipAddress, bool isNegated = false) |
|
{ |
|
string criterionValue = String.Format("\"{0}\"".Arrange(ipAddress)); |
|
Criterion criterion = new Criterion() |
|
{ |
|
CriterionName = "IPAddress", |
|
CriterionValue = criterionValue, |
|
CriterionDisplayValue = ipAddress, |
|
CriterionTitle = "IP address", |
|
IsNegated = isNegated |
|
}; |
|
return criterion; |
|
} |
|
|
|
} |
|
} |