public Guid GetEntityId(IPluginExecutionContext context, IMetadataService metaData) { // Get the metadata about the current entity. var req = new RetrieveEntityRequest { LogicalName = context.PrimaryEntityName }; var res = (RetrieveEntityResponse)metaData.Execute(req); return this.GetKeyValue(context, res.EntityMetadata.PrimaryKey); }
/// <summary> /// Retrieves EntityId from the Context /// Create,Update,Delete,SetState,Assign,DeliverIncoming /// </summary> /// <param name="context"></param> /// <returns></returns> public static Guid GetEntityId(IPluginExecutionContext context) { switch (context.MessageName) { case MessageName.Create: case MessageName.DeliverIncoming: if (context.Stage == MessageProcessingStage.BeforeMainOperationOutsideTransaction) { //throw new InvalidPluginExecutionException("EntityId is not available in PreCreate"); return(Guid.Empty); } else { //CreateResponse r; //r.id; if (context.OutputParameters.Contains(ParameterName.Id)) { return((Guid)context.OutputParameters[ParameterName.Id]); } //DeliverIncomingEmailResponse r; //r.EmailId; if (context.OutputParameters.Contains(ParameterName.EmailId)) { return((Guid)context.OutputParameters[ParameterName.EmailId]); } } break; case MessageName.Update: //context.InputParameters.Contains(ParameterName.Target) IMetadataService metadataService = context.CreateMetadataService(false); RetrieveEntityRequest rar = new RetrieveEntityRequest(); rar.LogicalName = context.PrimaryEntityName; rar.EntityItems = EntityItems.IncludeAttributes; RetrieveEntityResponse resp = (RetrieveEntityResponse)metadataService.Execute(rar); string keyName = resp.EntityMetadata.PrimaryKey; //UpdateRequest u; //TargetUpdateAccount a; //a.Account; // This s Dynamic entity //u.Target = a; // Update if (context.InputParameters[ParameterName.Target] is DynamicEntity) { Key key = (Key)((DynamicEntity)context.InputParameters[ParameterName.Target]).Properties[keyName]; return(key.Value); } break; case MessageName.Delete: case MessageName.Assign: case MessageName.GrantAccess: case MessageName.Handle: if (context.InputParameters[ParameterName.Target] is Moniker) { Moniker monikerId = (Moniker)context.InputParameters[ParameterName.Target]; return(monikerId.Id); } break; case MessageName.SetState: case MessageName.SetStateDynamicEntity: //SetStateAccountRequest r; //r.EntityId; // Guid === Moniker //r.AccountState; // State //r.AccountStatus; // Status return(((Moniker)context.InputParameters.Properties[ParameterName.EntityMoniker]).Id);; default: if (context.InputParameters.Contains(ParameterName.Target) && (context.InputParameters[ParameterName.Target] is Moniker)) { Moniker monikerId = (Moniker)context.InputParameters[ParameterName.Target]; return(monikerId.Id); } //Try by best route else fail //throw new InvalidPluginExecutionException("GetEntityId could not extract the Guid from Context"); return(Guid.Empty); } //throw new InvalidPluginExecutionException("GetEntityId could not extract the Guid from Context"); return(Guid.Empty); }