หมายเหตุ
การเข้าถึงหน้านี้ต้องได้รับการอนุญาต คุณสามารถลอง ลงชื่อเข้าใช้หรือเปลี่ยนไดเรกทอรีได้
การเข้าถึงหน้านี้ต้องได้รับการอนุญาต คุณสามารถลองเปลี่ยนไดเรกทอรีได้
ตรรกะของหม้อแปลงหมายถึงการย้ายค่าของฟิลด์เฉพาะจากเทมเพลตบทความให้ความรู้ไปยังบทความให้ความรู้ ฟิลด์มาตรฐานได้รับการสนับสนุนทันทีโดยไม่ต้องปรับแต่งเพิ่มเติม องค์กรสามารถเขียนทับตรรกะหม้อแปลงแบบสําเร็จรูปได้โดยการสร้างปลั๊กอินและลงทะเบียนเป็น msdyn_GetKAObjectFromTemplate
ข้อความ
เมื่อต้องการขยายตรรกะของหม้อแปลงสําหรับเทมเพลตบทความให้ความรู้ของคุณ:
- สร้างปลั๊กอินโดยทําตามขั้นตอนที่กล่าวถึงใน สร้างโปรเจ็กต์ปลั๊กอิน
- คุณสามารถเขียนตรรกะการแมปของคุณเองด้วยการแปลงโดยใช้
kaobject
context.OutputParameters["Result"]
เป็นเอนทิตี ในตัวอย่างนี้ เราจะพูดถึงวิธีที่คุณสามารถสร้างตัวแปลงเทมเพลตบทความให้ความรู้ด้วยความช่วยเหลือของแอตทริบิวต์ใหม่สองอย่าง ได้แก่ ประเภทบทความ และ ผู้ชม ในปลั๊กอินที่คุณสร้างขึ้น ให้แทนที่ทั้งคลาสด้วยโค้ดตัวอย่างต่อไปนี้:
/// The plugin shows a sample of how to build a Knowledge Article Template Transformer
/// </summary>
/// <remarks>
/// To showcase the capabilities of the transformer, we have added 2 new attributes to the Knowledge Article Template entity. These attributes have also been added to the form.
/// The first is Article Type with the logical name new_articletype that is of type Option Set and points to a Global Option Set called Article Type
/// The second is Audience with the logical name new_audience, also of type Option Set. This is a local option set and has 3 values -
/// Employee with value 100000000, Managers with value 100000001 and Public with value 100000002
/// Similarly, we have 2 new attributes on the Knowledge Article entity which have been added to the Knowledge Article for Interactive Experience form.
/// The first is Article Type with the logical name new_kaarticletype that is of type Option Set and points to the same Global Option Set Article Type described earlier.
/// The second is the Disclaimer with the logical name new_disclaimer that is of type Single Line of Text. To ensure that it behaves like a disclaimer, we have ensured that it is
/// not editable on the form.
///
/// The core goal of the transformer is to copy over the Option Set value from the Article Type of the template record to the Article Type of the article record.
/// Having the same Global Option Set ensures that we don't need to worry about mapping the values of one to the other. But that is also possible as can be seen with the other attribute transform.
/// Based on the audience of the template record, the disclaimer on the article record is populated.
/// </remarks>
public class TemplatePlugin : IPlugin
{
#region Constants
private const string TemplateIdParam = "TemplateId";
private const string ResultParam = "Result";
private const string KATemplateLogicalName = "msdyn_knowledgearticletemplate";
private const string KA_ArticleTypeName = "new_kaarticletype";
private const string Tmpl_ArticleTypeName = "new_articletype";
private const string KA_DisclaimerName = "new_disclaimer";
private const string Tmpl_AudienceName = "new_audience";
private const int Audience_Employee = 100000000;
private const int Audience_Managers = 100000001;
private const int Audience_Public = 100000002;
#endregion
public void Execute(IServiceProvider serviceProvider)
{
// Get all required services
IOrganizationServiceFactory ServiceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IPluginExecutionContext Context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationService OrganizationService = ServiceFactory.CreateOrganizationService(Context.UserId);
ITracingService TracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
try
{
string templateIdStr = Context.InputParameters[TemplateIdParam] as string;
Guid TemplateId = new Guid(templateIdStr);
Entity template = new Entity(KATemplateLogicalName);
template = OrganizationService.Retrieve(template.LogicalName, TemplateId, new ColumnSet(GetColumns().ToArray()));
Entity kaobject = Context.OutputParameters[ResultParam] as Entity;
TransformKAObject(kaobject, template);
}
catch (Exception ex)
{
TracingService.Trace("Exception: {0}", ex.Message);
throw;
}
}
#region Private Methods
/// <summary>
/// Returns a list of all the columns to be retrieved from the template
/// </summary>
/// <returns>The list of columns to retrieve from the template</returns>
private List<string> GetColumns()
{
return new List<string> { Tmpl_ArticleTypeName, Tmpl_AudienceName };
}
/// <summary>
/// Transforms the <paramref name="templateEntity"/> template record to the <paramref name="kaEntity"/> entity record
/// </summary>
/// <param name="kaEntity">The knowledge article entity record to transform</param>
/// <param name="templateEntity">The template entity used to transform the article record</param>
private void TransformKAObject(Entity kaEntity, Entity templateEntity)
{
foreach (var attribute in templateEntity.Attributes)
{
if (!GetColumns().Contains(attribute.Key))
{
continue;
}
kaEntity[GetKAAttributeName(attribute.Key)] = GetKAConvertor(attribute.Key)(attribute.Value);
}
}
/// <summary>
/// Gets the mapped attribute name for the article entity for the corresponding template attribute
/// </summary>
/// <param name="templateAttributeName">The attribute name used for retrieving the mapping</param>
/// <returns>The attribute name of the knowledge article entity that maps to the <paramref name="templateAttributeName"/> template entity attribute</returns>
private string GetKAAttributeName(string templateAttributeName)
{
switch (templateAttributeName)
{
case Tmpl_ArticleTypeName:
return KA_ArticleTypeName;
case Tmpl_AudienceName:
return KA_DisclaimerName;
default:
return string.Empty;
}
}
/// <summary>
/// Gets the object convertor for the article entity for the corresponding template attribute
/// </summary>
/// <param name="templateAttributeName">The attribute name used for retrieving the mapping</param>
/// <returns>A function that can transform the value stored in the template entity <paramref name="templateAttributeName"/> attribute to the knowledge article entity</returns>
private Func<object, object> GetKAConvertor(string templateAttributeName)
{
switch (templateAttributeName)
{
case Tmpl_ArticleTypeName:
return (object templateValue) =>
{
return templateValue;
};
case Tmpl_AudienceName:
return (object templateValue) =>
{
var audienceVal = templateValue as OptionSetValue;
switch (audienceVal.Value)
{
case Audience_Employee:
return "Internal Use Only";
case Audience_Managers:
return "For Managers Only";
case Audience_Public:
default:
return "Generally Available";
}
};
default:
return (object templateValue) =>
{
return null;
};
}
}
#endregion
}
- ทําตามขั้นตอนที่กล่าวถึงใน ลงทะเบียนปลั๊กอิน เพื่อลงทะเบียนปลั๊กอินของคุณ
- ตอนนี้ เมื่อคุณสร้างบทความให้ความรู้จากเทมเพลต ค่าของชุดตัวเลือกจะถูกแปลงและแทรกในบทความ
ข้อมูลที่เกี่ยวข้อง
สร้างบทความให้ความรู้โดยใช้เทมเพลต
บทช่วยสอน: เขียนและลงทะเบียนปลั๊กอิน