diff --git a/src/XrmMockupShared/Core.cs b/src/XrmMockupShared/Core.cs index 9c8833b9..8e5bfaf6 100644 --- a/src/XrmMockupShared/Core.cs +++ b/src/XrmMockupShared/Core.cs @@ -62,6 +62,8 @@ internal class Core public TimeSpan TimeOffset { get; private set; } public MockupServiceProviderAndFactory ServiceFactory { get; } + private List<string> systemAttributeNames; + public EntityReference AdminUserRef; public EntityReference RootBusinessUnitRef; @@ -100,6 +102,8 @@ public Core(XrmMockupSettings Settings, MetadataSkeleton metadata, List<Entity> this.pluginManager = new PluginManager(Settings.BasePluginTypes, metadata.EntityMetadata, metadata.Plugins); this.workflowManager = new WorkflowManager(Settings.CodeActivityInstanceTypes, Settings.IncludeAllWorkflows, Workflows, metadata.EntityMetadata); + this.systemAttributeNames = new List<string>() { "createdon", "createdby", "modifiedon", "modifiedby" }; + this.RequestHandlers = GetRequestHandlers(db); InitializeDB(); this.security.InitializeSecurityRoles(db); @@ -585,6 +589,9 @@ internal OrganizationResponse Execute(OrganizationRequest request, EntityReferen if (eventOp.HasValue) { + //copy the createon etc system attributes onto the target so they are available for postoperation processing + CopySystemAttributes(postImage, entityInfo.Item1 as Entity); + pluginManager.TriggerSystem(eventOp.Value, ExecutionStage.PostOperation, entityInfo.Item1, preImage, postImage, pluginContext, this); pluginManager.TriggerSync(eventOp.Value, ExecutionStage.PostOperation, entityInfo.Item1, preImage, postImage, pluginContext, this); pluginManager.StageAsync(eventOp.Value, ExecutionStage.PostOperation, entityInfo.Item1, preImage, postImage, pluginContext, this); @@ -604,6 +611,29 @@ internal OrganizationResponse Execute(OrganizationRequest request, EntityReferen return response; } + private void CopySystemAttributes(Entity postImage, Entity item1) + { + if (item1 == null) + { + return; + } + + foreach (var systemAttributeName in this.systemAttributeNames) + { + if (postImage.Contains(systemAttributeName)) + { + if (postImage[systemAttributeName] is EntityReference) + { + item1[systemAttributeName] = new EntityReference(postImage.GetAttributeValue<EntityReference>(systemAttributeName).LogicalName, postImage.GetAttributeValue<EntityReference>(systemAttributeName).Id); + } + else if (postImage[systemAttributeName] is DateTime) + { + item1[systemAttributeName] = postImage.GetAttributeValue<DateTime>(systemAttributeName); + } + } + } + } + internal void HandleInternalPreOperations(OrganizationRequest request, EntityReference userRef) { if (request.RequestName == "Create") diff --git a/tests/SharedPluginsAndCodeactivites/ContactPostPlugin.cs b/tests/SharedPluginsAndCodeactivites/ContactPostPlugin.cs new file mode 100644 index 00000000..1ac98917 --- /dev/null +++ b/tests/SharedPluginsAndCodeactivites/ContactPostPlugin.cs @@ -0,0 +1,35 @@ +namespace DG.Some.Namespace { + using System; + using Microsoft.Xrm.Sdk; + using DG.XrmFramework.BusinessDomain.ServiceContext; + + public class ContactPostPlugin : Plugin + { + public ContactPostPlugin() + : base(typeof(ContactPostPlugin)) + { + RegisterPluginStep<Contact>( + EventOperation.Create, + ExecutionStage.PostOperation, + Execute); + } + + protected void Execute(LocalPluginContext localContext) + { + if (localContext == null) { + throw new ArgumentNullException("localContext"); + } + + var service = localContext.OrganizationService; + + var con = localContext.PluginExecutionContext.InputParameters["Target"] as Contact; + + if (con.FirstName == "CheckSystemAttributes") + { + con.LastName = con.CreatedOn.ToString(); + con.FirstName = "updated"; + service.Update(con); + } + } + } +} diff --git a/tests/SharedPluginsAndCodeactivites/SharedPluginsAndCodeactivites.projitems b/tests/SharedPluginsAndCodeactivites/SharedPluginsAndCodeactivites.projitems index c9ef9e01..fb91da3e 100644 --- a/tests/SharedPluginsAndCodeactivites/SharedPluginsAndCodeactivites.projitems +++ b/tests/SharedPluginsAndCodeactivites/SharedPluginsAndCodeactivites.projitems @@ -11,6 +11,7 @@ <ItemGroup> <Compile Include="$(MSBuildThisFileDirectory)AccountCustomActivity.cs" /> <Compile Include="$(MSBuildThisFileDirectory)AccountBothImagePlugin.cs" /> + <Compile Include="$(MSBuildThisFileDirectory)ContactPostPlugin.cs" /> <Compile Include="$(MSBuildThisFileDirectory)SyncAsyncTest\Test5\Sync2WithExecutionOrder - Copy.cs" /> <Compile Include="$(MSBuildThisFileDirectory)SyncAsyncTest\Test5\AsyncWithExecutionOrder.cs" /> <Compile Include="$(MSBuildThisFileDirectory)SyncAsyncTest\Test5\Sync1WithExecutionOrder.cs" /> diff --git a/tests/SharedTests/TestPlugins.cs b/tests/SharedTests/TestPlugins.cs index f053e451..17139be4 100644 --- a/tests/SharedTests/TestPlugins.cs +++ b/tests/SharedTests/TestPlugins.cs @@ -35,6 +35,22 @@ public void TestImages() } #endif + [TestMethod] + public void TestSystemAttributesAddedToTargetForPostOperationStepPlugins() + { + using (var context = new Xrm(orgAdminUIService)) + { + var con = new Contact(); + con.FirstName = "CheckSystemAttributes"; + + con.Id = orgAdminUIService.Create(con); + + con = Contact.Retrieve(orgAdminService, con.Id, x => x.LastName,x=>x.CreatedOn); + Assert.IsTrue(!string.IsNullOrEmpty(con.LastName)); + Assert.AreEqual(con.CreatedOn.ToString(), con.LastName); + } + } + [TestMethod] public void TestPluginTrigger() {