Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Key not used in request #254

Closed
vidarkongsli opened this issue May 23, 2016 · 3 comments
Closed

Key not used in request #254

vidarkongsli opened this issue May 23, 2016 · 3 comments

Comments

@vidarkongsli
Copy link

I have this code where I try to delete an entity:

client.For<Account>()
                    .Key("foo")
                    .DeleteEntryAsync()
                    .Wait();

which constructs the following Uri to the server: https://localhost:44305/odata/Account() instead of the expected https://localhost:44305/odata/Account('foo'). Any idea what could be the cause of this? I use Web Api to implement the Odata service.

@object
Copy link
Member

object commented May 23, 2016

The only thing I can think of is that the OData service definition doesn't have the key. Can you check the service metadata (go to service_url/$metadata)?

@vidarkongsli
Copy link
Author

Yes, that was it. Thanks a lot! 👍

@toryb
Copy link

toryb commented Nov 16, 2018

I don't know if should open a new issue or use this one. I have a scenario where the OData service (MS Dynamics 365 Customer Engagement A.K.A. CRM) I am accessing uses "alternate keys" in which the key notation is accepted but the metadata does not define them in the Key node for the entity. As a result, the Client will not use the key I define using the .Key() statement.

Here are the details:

The metadata:

  <EntityType Name="account" BaseType="mscrm.crmbaseentity">
    <Key>
      <PropertyRef Name="accountid" />
    </Key>
    <Property Name="accountid" Type="Edm.Guid" />
    <Property Name="lastusedincampaign" Type="Edm.DateTimeOffset" />
    <Property Name="address1_name" Type="Edm.String" Unicode="false" />
    <Property Name="accountnumber" Type="Edm.String" Unicode="false" />
    ... (Properties and Navigation left out for brevity)
    <Annotation Term="OData.Community.Keys.V1.AlternateKeys">
      <Collection>
        <Record Type="OData.Community.Keys.V1.AlternateKey">
          <PropertyValue Property="Key">
            <Collection>
              <Record Type="OData.Community.Keys.V1.PropertyRef">
                <PropertyValue Property="Alias" String="accountnumber" />
                <PropertyValue Property="Name" PropertyPath="accountnumber" />
              </Record>
            </Collection>
          </PropertyValue>
        </Record>
      </Collection>
    </Annotation>
  </EntityType>

The essence of the code:

  var keyValues = new Dictionary<string, object>();
  keyValues.Add("accountnumber", "US-50");
  var selectFields = new List<string>()
  {
    nameof(customer.accountid),
    nameof(customer._owningbusinessunit_value),
    nameof(customer._primarycontactid_value),
    nameof(customer.lev_Account_AccountAddress),
    nameof(customer.accountnumber),
    nameof(customer.contact_customer_accounts),
    nameof(customer.description),
    nameof(customer.lev_CustomerId),
    nameof(customer.name),
    nameof(customer.statecode),
    nameof(customer.statuscode)
  };
  var query = Client.For("accounts")
    .Key(keyValues)
    .Select(selectFields);
  return query.FindEntryAsync()

The request:

GET https://[our_tenant].crm.dynamics.com/api/data/v9.0/accounts()?$select=accountid,_owningbusinessunit_value,_primarycontactid_value,description,statecode,accountnumber,name,statuscode,lev_CustomerId,lev_Account_AccountAddress,contact_customer_accounts

Here is a working request that I tested:

GET https://[our_tenant].crm.dynamics.com/api/data/v9.0/accounts(accountnumber='US-50')?$select=accountid,_owningbusinessunit_value,_primarycontactid_value,description,statecode,accountnumber,name,statuscode,lev_CustomerId,lev_Account_AccountAddress,contact_customer_accounts

I know there is a plan to add support for custom attributes in the next major release. However, until that time is there any possible workaround?

For example, is there a way to tell the client to use the key values provided even though the Metadata doesn't appear to define them as keys? I could use the ODataClientSettings.BeforeRequest but since I only have the HttpRequestMessage in the method I won't know what keys were defined to modify the request. Does anyone have any ideas for a workaround besides modifying the code myself to ignore the Key definition?

Thanks,

toryb

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants