Sergey Litvinov
Sergey Litvinov

Reputation: 7478

Update just one property in CRM via WCF Data Services

I have some code that gets an object from CRM, then update one property, and then push it back to CRM. I generated WCF Data Services context based on Organization Data Service (OData).

But when I've try to send update, then it sends all loaded properties, and not just one changed, so i got access deny exception, as my account can't change all properties in the object.

My sample code:

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

I checked request that it sends via Fiddler, and it sends all properties that object has, and not just changed on. Is there a way to send just changed property?

If I send such request manually, then it works like a charm and update just one property, but how I can send similar request via WCF Data Services?

My sample manual request:

<?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>

Thanks!

UPDATE1

When I try to create a new in-memory contact, fill id and needed property, and then send, it sends nulls for non specified properties, and CRM clears all properties for object except specified, so after that object is almost erased.

My code:

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

Request for this code(I cutted part of it, as it was huge):

<?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>

Upvotes: 0

Views: 841

Answers (1)

Daryl
Daryl

Reputation: 18895

Create a new in memory Contact, populate the id and the field you want to update, add it to the context, and have the context update it.

Upvotes: 1

Related Questions