locked
How to impersonate the user through plug-in code RRS feed

  • Question

  • Dear All,

    How to impersonate a user through plugin code in CRM 4.0 and CRM 2011?

    In CRM 4.0 we use 'CrmService' and in CRM 2011 we use 'OrganizationService'. Am I right?

    Please correct me if I am wrong.

    Please help with code snippets.


    Thanks & Regards, Sudhakar

    Thursday, July 5, 2012 10:10 AM

Answers

  • Hi,

    For a plugin you can change the run in context to run the plugin under that user's context. Following thread might help

    http://social.microsoft.com/Forums/en-US/crmdevelopment/thread/2ff0a565-c7d0-4f46-b1ad-b260947bcfa0/#762874ec-a7b0-45fe-b504-6ec5eac6b319 


    -Devashish
    http://thecrmworld.wordpress.com
    http://ebizartisans.com

    Thursday, July 5, 2012 11:44 AM
  • Hi Sudhkar,

    what to do you mean by another userid details(always a single user or may vary)?

    if you want pass a single userid you can register plugin in the plugin registration tool


    By Sanz. -- If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".

    Friday, July 6, 2012 6:27 AM
  • hi sudhkar,You have misunderstood me,

    in that dropdown you can select the user which you need,

    other wise you need to get the guid  of user and pass here

     IOrganizationService _service = _factory.CreateOrganizationService(anotehr user id);


    By Sanz. -- If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".

    Friday, July 6, 2012 9:53 AM

All replies

  • hi what do you mean of impersonate?

    check this for plugin walkthrough,

    http://msdn.microsoft.com/en-us/library/gg695782.aspx


    By Sanz. -- If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".

    Thursday, July 5, 2012 10:14 AM
  • Impersonate means "Executing the plugin on behalf of another user"

    Thanks & Regards, Sudhakar

    Thursday, July 5, 2012 11:14 AM
  • then you need to pass the user id when create the Service

    use this

     IOrganizationService _service = _factory.CreateOrganizationService(_context.UserId);


    By Sanz. -- If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".

    Thursday, July 5, 2012 11:32 AM
  • Hi,

    For a plugin you can change the run in context to run the plugin under that user's context. Following thread might help

    http://social.microsoft.com/Forums/en-US/crmdevelopment/thread/2ff0a565-c7d0-4f46-b1ad-b260947bcfa0/#762874ec-a7b0-45fe-b504-6ec5eac6b319 


    -Devashish
    http://thecrmworld.wordpress.com
    http://ebizartisans.com

    Thursday, July 5, 2012 11:44 AM
  • Hi,

    Check http://msdn.microsoft.com/en-us/library/gg334744.aspx


    Conatact Me
    Follow me on Twitter
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    Thursday, July 5, 2012 4:03 PM
    Moderator
  • Thanks for replying san Sanz.

    The above code is used for logged on user. But I want to pass another userid details. Please send me code snippet.


    Thanks & Regards, Sudhakar

    Friday, July 6, 2012 6:14 AM
  • Hi Sudhkar,

    what to do you mean by another userid details(always a single user or may vary)?

    if you want pass a single userid you can register plugin in the plugin registration tool


    By Sanz. -- If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".

    Friday, July 6, 2012 6:27 AM
  • Hi,

    As explained in my post above if you always wish to run a under a specific user context you can run the plugin under that user's context. For more information see the post below where in the plugin registration tool you can change the Run in user's context. Hope this helps.

    http://social.microsoft.com/Forums/en-US/crmdevelopment/thread/2ff0a565-c7d0-4f46-b1ad-b260947bcfa0/#762874ec-a7b0-45fe-b504-6ec5eac6b319


    -Devashish
    http://thecrmworld.wordpress.com
    http://ebizartisans.com

    Friday, July 6, 2012 6:41 AM
  • Hi Sanz,

    I know how to impersonate the user through plugin registration tool. But  I want to do the same thing through code. In that code i want to give the another user ( instead of 'Calling User') details, who is having sufficient admin privileges for executing the plugin.

    Please let me know if you have any clarifications.


    Thanks & Regards, Sudhakar

    Friday, July 6, 2012 8:52 AM
  • hi sudhkar,You have misunderstood me,

    in that dropdown you can select the user which you need,

    other wise you need to get the guid  of user and pass here

     IOrganizationService _service = _factory.CreateOrganizationService(anotehr user id);


    By Sanz. -- If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".

    Friday, July 6, 2012 9:53 AM