2

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

1 回答 1

2

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

  1. 在创建记录后 使用AssignRequest更新记录。
  2. 使用OrganizationServiceProxy 和 CallerId 模拟来使用您希望在创建时成为所有者的特定用户。您不需要他们的密码来模拟,只需要他们的 CRM SystemUserId
于 2013-02-25T19:58:02.927 回答