通过WCF数据服务更新CRM中的一个属性



我有一些代码从CRM获取一个对象,然后更新一个属性,然后将其推回CRM。我基于组织数据服务(OData)生成了WCF数据服务上下文。

但是当我尝试发送更新时,它会发送所有加载的属性,而不仅仅是一个更改,所以我得到了访问拒绝异常,因为我的帐户不能更改对象中的所有属性。

我的示例代码:
Guid guid = new Guid("aaa4f83e-0017-e261-9ba4-001517264c77");
Contact contact = context.ContactSet.Where(c => c.ContactId == guid).ToList().First();
contact.New_SomeField = "SomeData";
context.UpdateObject(contact);
DataServiceResponse response = context.SaveChanges();

我检查了它通过Fiddler发送的请求,它发送对象拥有的所有属性,而不仅仅是改变了。有没有办法发送刚刚更改的属性?

如果我手动发送这样的请求,那么它就像一个魅力一样工作,只更新一个属性,但是我如何通过WCF数据服务发送类似的请求?

我的示例手动请求:

<?xml version="1.0" encoding="utf-8"?>
<entry xmlns="http://www.w3.org/2005/Atom" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
    <id>http://SomeUrl/XRMServices/2011/OrganizationData.svc/ContactSet(guid'aaa4f83e-0017-e261-9ba4-001517264c77')</id>
    <category term="Microsoft.Crm.Sdk.Data.Services.Contact" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
    <title type="text">Some Name</title>
    <updated>2014-07-15T17:49:06Z</updated>
    <author>
        <name />
    </author>
    <content type="application/xml">
        <m:properties>
            <d:ContactId m:type="Edm.Guid">aaa4f83e-0017-e261-9ba4-001517264c77</d:ContactId>
            <d:New_SomeField>
                Some Data
            </d:New_SomeField>
        </m:properties>
    </content>
</entry>

谢谢!

UPDATE1

当我尝试在内存中创建一个新的联系人,填写id和所需的属性,然后发送时,它会为非指定的属性发送null,并且CRM会清除指定对象之外的所有属性,因此在该对象几乎被擦除之后。

我代码:

var guid = new Guid("D866D2A0-1706-E111-845F-001517264C77");
Contact contact = new Contact();
contact.ContactId = guid;
contact.New_SomeField = "SomeData"
context.AttachTo("ContactSet", contact);
context.ChangeState(contact, EntityStates.Modified);
DataServiceResponse newResponse = context.SaveChanges();

请求这段代码(我剪掉了一部分,因为它太大了):

<?xml version="1.0" encoding="utf-8"?>
<entry xmlns="http://www.w3.org/2005/Atom" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
    <id>http://SomeServer/SomeOrganization/XRMServices/2011/OrganizationData.svc/ContactSet(guid'd866d2a0-1706-e111-845f-001517264c77')</id>
    <category term="Microsoft.Crm.Sdk.Data.Services.Contact" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
    <title type="text"></title>
    <updated>2014-07-15T19:31:57Z</updated>
    <author>
        <name />
    </author>
    <content type="application/xml">
        <m:properties>
            <d:AccountId m:type="Microsoft.Crm.Sdk.Data.Services.EntityReference">
                <d:Id m:type="Edm.Guid" m:null="true" />
                <d:LogicalName m:null="true" />
                <d:Name m:null="true" />
            </d:AccountId>
            <d:AccountRoleCode m:type="Microsoft.Crm.Sdk.Data.Services.OptionSetValue">
                <d:Value m:type="Edm.Int32" m:null="true" />
            </d:AccountRoleCode>
            <d:Address1_AddressId m:type="Edm.Guid" m:null="true" />
            <d:Address1_AddressTypeCode m:type="Microsoft.Crm.Sdk.Data.Services.OptionSetValue">
                <d:Value m:type="Edm.Int32" m:null="true" />
            </d:Address1_AddressTypeCode>
            <d:Address1_City m:null="true" />
            <d:Address1_Country m:null="true" />
            <d:Address1_County m:null="true" />
            <d:Address1_Fax m:null="true" />
            <d:Address1_FreightTermsCode m:type="Microsoft.Crm.Sdk.Data.Services.OptionSetValue">
                <d:Value m:type="Edm.Int32" m:null="true" />
            </d:Address1_FreightTermsCode>
            <d:Address1_Latitude m:type="Edm.Double" m:null="true" />
            <d:Address1_Line1 m:null="true" />
            <d:Address1_Line2 m:null="true" />
            <d:EducationCode m:type="Microsoft.Crm.Sdk.Data.Services.OptionSetValue">
                <d:Value m:type="Edm.Int32" m:null="true" />
            </d:EducationCode>
            <d:EMailAddress1 m:null="true" />
            <d:FamilyStatusCode m:type="Microsoft.Crm.Sdk.Data.Services.OptionSetValue">
                <d:Value m:type="Edm.Int32" m:null="true" />
            </d:FamilyStatusCode>
            <d:Fax m:null="true" />
            <d:FirstName m:null="true" />
            <d:FtpSiteUrl m:null="true" />
            <d:FullName m:null="true" />
            <d:GenderCode m:type="Microsoft.Crm.Sdk.Data.Services.OptionSetValue">
                <d:Value m:type="Edm.Int32" m:null="true" />
            </d:GenderCode>
            <d:GovernmentId m:null="true" />
            <d:HasChildrenCode m:type="Microsoft.Crm.Sdk.Data.Services.OptionSetValue">
                <d:Value m:type="Edm.Int32" m:null="true" />
            </d:HasChildrenCode>
            <d:JobTitle m:null="true" />
            <d:LastName m:null="true" />
            <d:New_SomeField>
                Some Data
            </d:New_SomeField>
        </m:properties>
    </content>
</entry>

创建一个新的内存联系人,填充id和您想要更新的字段,将其添加到上下文,并让上下文更新它

最新更新