我正在尝试在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();

最佳答案

您可以通过两种方法在CRM中设置实体的所有者ID。


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

关于c# - Dynamics 2011:设置事件记录的所有者,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15074855/

10-10 23:33