Associate characteristics to segments
To associate characteristics with a specific segment, you first need to add each Criterion to a CriteriaGroup (Telerik.Sitefinity.Personalization.Impl.Model.CriteriaGroup). A CriteriaGroup represents a collection of one or more criteria that is defined for a specific segment. Each Segment object has a CriteriaGroups property (IList<CriteriaGroup>) that represents a collection of CriteriaGroup objects.
To associate your newly created CriteriaGroup to the segment, you include it in the CriteriaGroups collection of the specific segment. For example:
C#
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;
}
}
}
Want to learn more?
Enhance your Sitefinity skills by enrolling in free training sessions. Become Sitefinity certified through Progress Education Community to strengthen your professional credentials.
Get started with Integration Hub | Sitefinity Cloud
This free lesson teaches administrators, marketers, and other business professionals how to use Sitefinity Integration Hub to create automated workflows between Sitefinity and other business systems.
Web Security for Sitefinity Administrators
This free lesson teaches administrators the basics about protecting your Sitefinity instance and your sites from external threats. Configure HTTPS, SSL, allow lists for trusted sites, and cookie security, among others.
Foundations of Sitefinity ASP.NET Core Development
The free on-demand video course teaches developers how to use Sitefinity ASP.NET Core and take advantage of its decoupled architecture and modern development model.
