扩展模板中的变压器逻辑

转换器逻辑是指将特定字段的值从知识文章模板移动到知识文章。 直接支持标准字段。 组织可以替代现成转换器逻辑,方法是创建插件,然后将其注册为 msdyn_GetKAObjectFromTemplate 消息。

扩展知识文章模板的转换器逻辑:

  1. 通过执行 创建插件项目 中提到的步骤创建插件。
  2. 您可以使用转换后的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
	}
  1. 按照 注册插件 中提到的步骤注册您的插件。
  2. 现在,当您从模板创建知识文章时,选项集的值将转换并插入到文章中。

使用模板创建知识文章
教程:编写和注册插件