简体   繁体   中英

Update CRM 2011 using OrganizationServiceContext

How do you update records in CRM 2011 using OrganizationServiceContext? Can anyone provide a simple example? Thanks!

This is my code:

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();
    }


}

}

This is an example from the 5.05 SDK help file

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();
}

If you haven't already you can download the SDK from here crm 2011 sdk . I would highly reccomend it as it has lots of great samples. The current version is 5.06.

Hope that helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM