Dynamics crm 使用OrganizationServiceContext更新CRM 2011

Dynamics crm 使用OrganizationServiceContext更新CRM 2011,dynamics-crm,dynamics-crm-2011,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

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


}
使用System.Collections.Generic;
使用System.Linq;
使用System.ServiceModel.Description;
使用Microsoft.Xrm.Sdk.Client;
使用Microsoft.Xrm.Client;
使用Microsoft.Xrm.Sdk;
使用Microsoft.Xrm.Sdk.Linq;
使用Microsoft.Xrm.Sdk.Messages;
使用Microsoft.Xrm.Client.Services;
使用系统、数据、服务;
使用System.Text.RegularExpressions;
使用System.Web.UI.HTMLControl;
使用系统诊断;
使用System.Web.Security;
使用系统数据;
使用System.Collections.Specialized;
使用System.Web.SessionState;
使用制度;
使用System.Web.Profile;
使用系统配置;
使用System.Web.UI.WebControl;
使用系统集合;
使用System.Web.UI.WebControl.WebParts;
使用System.Web;
使用System.Web.UI;
使用系统图;
使用系统文本;
使用System.Web.Caching;
使用Telerik.Web.UI;
使用Microsoft.Xrm.Sdk.Discovery;
使用Microsoft.Data.Entity;
使用System.Data.Entity;
公共部分类LeadShareEditPanel:System.Web.UI.UserControl
{
受保护的无效页面加载(对象发送方、事件参数e)
{
}
受保护的无效imgBtnSaveNote_单击(对象发送者,图像ClickEventArgs e)
{
Uri organizationUri=新Uri(“http://server/CRMT/XRMServices/2011/Organization.svc");
Uri homeRealmUri=null;
ClientCredentials=新的ClientCredentials();
credentials.Windows.ClientCredential=新系统.Net.NetworkCredential(“用户”、“密码”、“域”);
OrganizationServiceProxy orgProxy=新的OrganizationServiceProxy(organizationUri,homeRealmUri,凭据,null);
//获取IOR组织服务
//Get-OrganizationServiceContext-组织服务上下文类实现IQueryable接口和
//一个.NET语言集成查询(LINQ)查询提供程序,因此我们可以针对Microsoft Dynamics CRM数据编写LINQ查询。
使用(var服务=新组织服务(orgProxy))
使用(var上下文=新的CrmOrganizationServiceContext(服务))
{
var contact=context.CreateQuery().First(c=>c.FirstName==“Bob”);
contact.JobTitle=“开发者”;
context.UpdateObject(联系人);
SaveChanges();
contact.EMailAddress1=”bob@contoso.com";
context.UpdateObject(联系人);
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();
}
使用(var服务=新组织服务(连接))
使用(var上下文=新的CrmOrganizationServiceContext(服务))
{
var contact=context.CreateQuery().First(c=>c.FirstName==“Bob”);
contact.JobTitle=“开发者”;
context.UpdateObject(联系人);
SaveChanges();
contact.EMailAddress1=”bob@contoso.com";
context.UpdateObject(联系人);
SaveChanges();
}
如果您还没有下载SDK,可以从这里下载。我非常推荐它,因为它有很多很好的样品。目前的版本是5.06


希望对您有所帮助。

这是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();
}
使用(var服务=新组织服务(连接))
使用(var上下文=新的CrmOrganizationServiceContext(服务))
{
var contact=context.CreateQuery().First(c=>c.FirstName==“Bob”);
contact.JobTitle=“开发者”;
context.UpdateObject(联系人);
SaveChanges();
contact.EMailAddress1=”bob@contoso.com";
context.UpdateObject(联系人);
SaveChanges();
}
如果您还没有下载SDK,可以从这里下载。我非常推荐它,因为它有很多很好的样品。目前的版本是5.06


希望这会有所帮助。

听起来您缺少了一些dll引用或使用了一些语句。从“谢谢”中查看此页面。在这一行代码var Contact=context.CreateQuery().First(c=>c.FirstName==“Bob”)中,我仍然得到“CS0246:找不到类型或命名空间名称‘Contact’(您是否缺少using指令或程序集引用?);我在上面发布了我的全部代码,我不认为我遗漏了任何名称空间,任何想法?谢谢是否使用早期绑定实体?您是否运行了CRMSvcUtil.exe来生成OData对象?我只使用过早期绑定方法,所以我不确定你遗漏了什么。我发现这是用晚装的。你可以从那个页面上看到,这个家伙使用了一个(EntityReference)类型转换,这也是你可能需要做的。听起来你缺少了一些dll引用或使用了一些语句。从“谢谢”中查看此页面。在这一行代码var Contact=context.CreateQuery().First(c=>c.FirstName==“Bob”)中,我仍然得到“CS0246:找不到类型或命名空间名称‘Contact’(您是否缺少using指令或程序集引用?);我在上面发布了我的全部代码,我不认为我遗漏了任何名称空间,任何想法?谢谢是否使用早期绑定实体?您是否运行了CRMSvcUtil.exe来生成OData对象?我只使用过早期绑定方法,所以我不确定你遗漏了什么。我发现这是用晚装的。你可以从那个页面上看到,这个家伙使用了一组(EntityReference),这也是你可能需要做的。你的代码有什么问题?您将在“代码有哪些问题?”中找到示例?您将在中找到示例