使用OrganizationServiceContext更新CRM 2011

时间:2011-08-30 15:41:55

标签: dynamics-crm dynamics-crm-2011

如何使用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();
    }


}

}

1 个答案:

答案 0 :(得分:4)

这是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。

希望有所帮助。