public void Execute(IServiceProvider serviceProvider) { ITracingService tracer = (ITracingService)serviceProvider.GetService(typeof(ITracingService)); IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext)); IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory)); IOrganizationService service = factory.CreateOrganizationService(context.UserId); try { if (context.InputParameters["Target"] is Entity entity && entity.Contains("text")) { var text = (string)entity["text"]; if (text != null && text.StartsWith("{")) //JSON { var annotationDto = DataTransferObject.ParseJson(text); tracer.Trace(text); entity["createdon"] = annotationDto.createdon; entity["createdby"] = new EntityReference("systemuser", annotationDto.createdby); entity["modifiedon"] = annotationDto.modifiedon; entity["modifiedby"] = new EntityReference("systemuser", annotationDto.modifiedby); entity["text"] = annotationDto.originalfieldvalue; } } } catch (Exception exception) { throw new InvalidPluginExecutionException(exception.Message, exception); } }
public static DataTransferObject ParseJson(string json) { var annotationDto = new DataTransferObject(); var memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(json)); var jsonSerializer = new DataContractJsonSerializer(annotationDto.GetType()); annotationDto = jsonSerializer.ReadObject(memoryStream) as DataTransferObject; memoryStream.Close(); return(annotationDto); }