Dynamics 2011:设置活动记录的所有者

时间:2013-02-25 19:28:00

标签: dynamic dynamics-crm-2011 dynamics-crm

我正在尝试在Dynamics 2011中创建活动记录,并将所有者名称设置为“test_user”。相反,下面的代码是采用用于访问Dynamics API的凭据。有没有办法冒充“test_user”用户而不传递他的密码?谢谢。

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

修改后的代码

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

1 个答案:

答案 0 :(得分:2)

您有两种方法可以在CRM中设置实体的owenr id。

  1. 创建后使用AssignRequest更新记录。
  2. 使用impersonation with the OrganizationServiceProxy, CallerId可以在创建时使用您想要成为所有者的特定用户。您不需要他们的密码来进行模拟,只需要他们的CRM SystemUserId