locked
Get Current logged on user custom workflow activity RRS feed

  • Question

  • Hi All,

    I am using the following code to get the current logged on user through Custom Workflow. Can you correct the code? The below code is only giving me a lookup to select the user but not populating the lookup with current user.

    Thank you.

    Naresh.

    using System;
    using System.Collections.Generic;
    using System.Activities;
    using Microsoft.Xrm.Sdk;
    using Microsoft.Xrm.Sdk.Workflow;
    using System.Runtime.Serialization;

    namespace GetCurrentUser
    {
        public class GetUser : CodeActivity
        {
            [Input("User")]
            [Output("User EntityReference")]
            [ReferenceTarget("systemuser")]
            [Default("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx", "systemuser")]
            public InOutArgument<EntityReference> SystemUserReference { get; set; }
            protected override void Execute(CodeActivityContext context)
            {
                IWorkflowContext workflowContext = context.GetExtension<IWorkflowContext>();
                Guid id = workflowContext.InitiatingUserId;
                if (SystemUserReference.Get(context).Id != id)
                {
                    throw new InvalidPluginExecutionException("Unexpected Error");
                }
            }
        }
    }

    Friday, October 4, 2013 2:02 PM

All replies

  • Hi,
    you need to set your SystemUserReference variable with the InitiatingUserId value, I wrote a post in my blog with the correct code:

    Crm Answers - Get the current user inside a Workflow

    Hope it helps


    My blog: www.crmanswers.net

    Saturday, October 5, 2013 8:38 PM
  • Hi Guido Preite,

    Thanks for you reply.

    When I tried to execute the code suggested. I receive the below error while registering the custom workflow using plugin registeration tool.

    Kindly help..

    Thank you...

    Unhandled Exception: System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: The parameter name 'Current User' of property CurrentUser must be of type OutArgument or InOutArgument
    Detail: <OrganizationServiceFault xmlns="http://schemas.microsoft.com/xrm/2011/Contracts" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
      <ErrorCode>-2147200995</ErrorCode>
      <ErrorDetails xmlns:a="http://schemas.datacontract.org/2004/07/System.Collections.Generic" />
      <Message>The parameter name 'Current User' of property CurrentUser must be of type OutArgument or InOutArgument</Message>
      <Timestamp>2013-10-07T09:41:51.4484161Z</Timestamp>
      <InnerFault>
        <ErrorCode>-2147200995</ErrorCode>
        <ErrorDetails xmlns:a="http://schemas.datacontract.org/2004/07/System.Collections.Generic" />
        <Message>The parameter name 'Current User' of property CurrentUser must be of type OutArgument or InOutArgument</Message>
        <Timestamp>2013-10-07T09:41:51.4484161Z</Timestamp>
        <InnerFault i:nil="true" />
        <TraceText i:nil="true" />
      </InnerFault>
      <TraceText i:nil="true" />
    </OrganizationServiceFault>

    Server stack trace:
       at System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime operation, ProxyRpc& rpc)
       at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
       at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
       at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)

    Exception rethrown at [0]:
       at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
       at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
       at Microsoft.Xrm.Sdk.IOrganizationService.Update(Entity entity)
       at Microsoft.Xrm.Sdk.Client.OrganizationServiceProxy.UpdateCore(Entity entity)
       at Microsoft.Crm.Tools.PluginRegistration.RegistrationHelper.UpdateAssembly(CrmOrganization org, String pathToAssembly, CrmPluginAssembly assembly, PluginType[] type)
       at Microsoft.Crm.Tools.PluginRegistration.PluginRegistrationForm.btnRegister_Click(Object sender, EventArgs e)

    Monday, October 7, 2013 9:49 AM
  • Hi,
    did you copy exactly the code from my site? because looks like you left the decorator as [Input("...")] and not as Output[("...")]

    My blog: www.crmanswers.net

    Monday, October 7, 2013 10:57 AM
  • Hi Guido Preite,

    Yes, am using the decorator as [Input("...")] and not as Output[("...")]

    VS is not allowing to use the said correction.

    Am using the below code to get the current user. Please corrent.

    Thank you...

    public class GetUser : CodeActivity
        {
            [Output("Current User")]
            [ReferenceTarget("systemuser")]
            public OutArgument CurrentUser { get; set; }
            protected override void Execute(CodeActivityContext context)
            {
                IWorkflowContext workflowContext = context.GetExtension<IWorkflowContext>();
                CurrentUser.Set(context, new EntityReference("systemuser", workflowContext.InitiatingUserId));
            }
        }

    Monday, October 7, 2013 11:06 AM
  • The code is ok, try to increment the build number inside your project or change the class name, also remove the custom activity and register again instead updating your old one.

    My blog: www.crmanswers.net

    Monday, October 7, 2013 11:38 AM