【问题标题】:Update CRM 2011 using OrganizationServiceContext使用 OrganizationServiceContext 更新 CRM 2011
【发布时间】:2011-08-31 03:26:29
【问题描述】:

如何使用 OrganizationServiceContext 更新 CRM 2011 中的记录?谁能提供一个简单的例子?谢谢!

这是我的代码:

using System.Collections.Generic;
using System.Linq;
using System.ServiceModel.Description;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Client;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Linq;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Client.Services;
using System.Data.Services;
using System.Text.RegularExpressions;
using System.Web.UI.HtmlControls;
using System.Diagnostics;
using System.Web.Security;
using System.Data;
using System.Collections.Specialized;
using System.Web.SessionState;
using System;
using System.Web.Profile;
using System.Configuration;
using System.Web.UI.WebControls;
using System.Collections;
using System.Web.UI.WebControls.WebParts;
using System.Web;
using System.Web.UI;
using System.Drawing;
using System.Text;
using System.Web.Caching;
using Telerik.Web.UI;
using Microsoft.Xrm.Sdk.Discovery;
using Microsoft.Data.Entity;
using System.Data.Entity;

public partial class LeadShareEditPanel : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{


}
protected void imgBtnSaveNote_Click(object sender, ImageClickEventArgs e)
{
    Uri organizationUri = new Uri("http://server/CRMT/XRMServices/2011/Organization.svc");
    Uri homeRealmUri = null;
    ClientCredentials credentials = new ClientCredentials();
    credentials.Windows.ClientCredential = new System.Net.NetworkCredential("user", "password", "domain");
    OrganizationServiceProxy orgProxy = new OrganizationServiceProxy(organizationUri, homeRealmUri, credentials, null);
    // Get the IOrganizationService

    //Get OrganizationServiceContext -the organization service context class implements the IQueryable interface and
    //a .NET Language-Integrated Query (LINQ) query provider so we can write LINQ queries against Microsoft Dynamics CRM data.

    using (var service = new OrganizationService(orgProxy))
    using (var context = new CrmOrganizationServiceContext(service))
    {
        var contact = context.CreateQuery<Contact>().First(c => c.FirstName == "Bob");
        contact.JobTitle = "Developer";
        context.UpdateObject(contact);
        context.SaveChanges();
        contact.EMailAddress1 = "bob@contoso.com";
        context.UpdateObject(contact);
        context.SaveChanges();
    }


}

}

【问题讨论】:

  • 您的代码有什么问题?您将在SDK 中找到示例

标签: dynamics-crm dynamics-crm-2011


【解决方案1】:

这是来自 5.05 SDK 帮助文件的示例

using (var service = new OrganizationService(connection))
using (var context = new CrmOrganizationServiceContext(service))
{
var contact = context.CreateQuery<Contact>().First(c => c.FirstName == "Bob");
contact.JobTitle = "Developer";
context.UpdateObject(contact);
context.SaveChanges();
contact.EMailAddress1 = "bob@contoso.com";
context.UpdateObject(contact);
context.SaveChanges();
}

如果您还没有,可以从这里crm 2011 sdk 下载 SDK。我强烈推荐它,因为它有很多很棒的样品。当前版本是 5.06。

希望对您有所帮助。

【讨论】:

  • 听起来您缺少一些 dll 引用或 using 语句。从msdn查看此页面
  • 谢谢。我仍然在这行代码 var contact = context.CreateQuery() 上收到“CS0246:找不到类型或命名空间名称'Contact'(您是否缺少 using 指令或程序集引用?)”。 First(c => c.FirstName == "鲍勃");我在上面发布了我的整个代码,我认为我没有遗漏任何命名空间,有什么想法吗?谢谢!
  • 您使用的是早期绑定实体吗?您是否运行 CRMSvcUtil.exe 来生成 OData 对象?我只使用过早期绑定方法,所以我不确定你错过了什么。我发现这个example 使用后期绑定。您可以从该页面中看到该人使用了 (EntityReference) 的演员表,这也是您可能必须做的。
猜你喜欢
  • 2012-01-21
  • 2011-09-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-30
  • 2018-09-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多