簡體   English   中英

使用OrganizationServiceContext更新CRM 2011

[英]Update CRM 2011 using OrganizationServiceContext

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


}

}

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

希望能有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM