2

I am trying create Activity record in Dynamics 2011 with owner name set as "test_user". Instead below code is taking credential which is used to access Dynamics API. Is there any way to impersonate "test_user" user without passing his password? Thank you.

string TargetCrmService = ConfigurationManager.AppSettings["TargetCrmService"];
string UserName = ConfigurationManager.AppSettings["UserName"];
string Domain = ConfigurationManager.AppSettings["Domain"];
string Password = ConfigurationManager.AppSettings["Password"];

Uri organizationUri = new Uri("http://CRMDEV/XRMServices/2011/Organization.svc");
Uri homeRealmUri = null;
ClientCredentials credentials = new ClientCredentials();

credentials.UserName.UserName = Domain + "\\" + UserName;
credentials.UserName.Password = Password;

OrganizationServiceProxy orgProxy = new OrganizationServiceProxy(organizationUri, homeRealmUri, credentials, null);

var _userId = (from u in orgProxy.CreateQuery<SystemUser>()
           where u.FullName == "Kevin Cook"
           select u.SystemUserId.Value).FirstOrDefault();

IOrganizationService _service = (IOrganizationService)orgProxy;
_service.CallerId = _userId;


try
{
    //Entity activity = new Entity("activitypointer");

    Entity activity = new Entity("appointment");
    activity["subject"] = "Test Meeting 1";
    activity["description"] = "Test Description";
    activity["scheduledstart"] = DateTime.Now;
    activity["scheduledend"] = DateTime.Now.AddMinutes(30);
    activity["createdbyname"] = "test_user";
    activity["modifiedbyname"] = "test_user";
    activity["createdbyname"] = "test_user";
    activity["owneridname"] = "test_user";

    Guid id = _service.Create(activity);
    Console.WriteLine("id: " + id);
}
catch (Exception ex)
{
    //MessageBox.Show(ex.Message);
}

Modified Code

Based on example on http://msdn.microsoft.com/en-us/library/gg309629.aspx

var _userId = (from u in orgProxy.CreateQuery<SystemUser>()
           where u.FullName == "Kevin Cook"
           select u.SystemUserId.Value).FirstOrDefault();
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Firoz Ansari
  • 2,505
  • 1
  • 23
  • 36

1 Answers1

2

You have two methods of setting the owenr id of an entity in CRM.

  1. Use the AssignRequest to update the record after it is created.
  2. Use impersonation with the OrganizationServiceProxy, CallerId to use the particular user you'd like to be the owner when it's created. You don't need their password to do impersonation, just their CRM SystemUserId
Community
  • 1
  • 1
Daryl
  • 18,592
  • 9
  • 78
  • 145
  • Thanks Daryl, I tried impersonation with the OrganizationServiceProxy, CallerId but its throwing this compilation error: Error 1 'Microsoft.Xrm.Sdk.Client.OrganizationServiceProxy' does not contain a definition for 'CreateQuery' and no extension method 'CreateQuery' accepting a first argument of type 'Microsoft.Xrm.Sdk.Client.OrganizationServiceProxy' could be found (are you missing a using directive or an assembly reference?) – Firoz Ansari Feb 25 '13 at 20:06
  • Why are you doing a CreateQuery, and not using the code above? You'll need to post your code... – Daryl Feb 25 '13 at 20:13
  • I think I am missing something here. I updated my code above. – Firoz Ansari Feb 25 '13 at 20:16
  • I got it! My bad. I suppose to use OrganizationServiceContext instead of OrganizationServiceProxy. Thanks Daryl for your help. – Firoz Ansari Feb 25 '13 at 20:23