Firoz Ansari
Firoz Ansari

Reputation: 2515

Dynamics 2011: Setting Owner of Activity record

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();

Upvotes: 2

Views: 678

Answers (1)

Daryl
Daryl

Reputation: 18895

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

Upvotes: 2

Related Questions