如何通过API调用在Dynamics CRM中关联自关联N:N的自定义实体rooms实例?
Hey there! Let’s tackle your two questions about working with your self-referential N:N rooms custom entity in Dynamics CRM via API—super common use case, so I’ve got you covered:
rooms entity with each other via API calls? Absolutely you can! Dynamics CRM’s Web API (and Organization Service, if you prefer SOAP) fully supports creating N:N relationships between entity records—including custom self-referential ones like your rooms entity. Here’s how to do it with the Web API:
- First, confirm you have the logical name of your N:N relationship (you’ll find this in CRM’s customization area under the
roomsentity’s Relationships tab—it’ll look something likenew_rooms_rooms, depending on how you named it). - To link two existing
roomsrecords, send aPOSTrequest to theAssociateaction endpoint for one of the records:POST [Your CRM Org URI]/api/data/v9.2/rooms(<first-room-id>)/Microsoft.Dynamics.CRM.Associate - Include this JSON body to specify the relationship and the second room to link:
{ "target": { "entitysetname": "rooms", "entities": [ { "id": "<second-room-id>", "logicalname": "new_rooms" } ] }, "relationship": { "relationshipname": "new_rooms_rooms" } } - If you’re creating a new
roomsrecord and want to immediately associate it with an existing one: firstPOSTto/roomsto create the new record, grab its ID from the response, then run theAssociateaction above using that new ID.
Associating custom entities works slightly differently depending on the relationship type (N:N, 1:N, N:1), but the core API patterns are consistent:
For N:N Relationships (like your rooms self-link)
Follow the same process outlined in Question 1. The key rules here are:
- Always use the relationship’s logical name (not the friendly display name) in your request.
- You can associate multiple records at once by adding more entries to the
entitiesarray in the request body.
For 1:N or N:1 Relationships
If you’re linking a child record to a parent (e.g., a custom booking entity to a room), you have two simple options:
- Set the lookup when creating the child record:
When sending aPOSTto create the child record, include the lookup field with the parent’s ID:{ "new_bookingname": "Conference Room Booking #456", "new_roomid": { "id": "<target-room-id>", "logicalname": "new_rooms" } } - Associate after creation:
Use aPATCHrequest to update the child record’s lookup field with a binding to the parent:
Request body:PATCH [Your CRM Org URI]/api/data/v9.2/new_bookings(<booking-id>){ "new_roomid@odata.bind": "/rooms(<target-room-id>)" }
Quick Pro Tips
- Stick to the latest API version (e.g., v9.2) for the most stable and feature-rich experience.
- Double-check all logical names (entity, field, relationship) — CRM uses these internally, so typos will throw errors.
- If you’re using the Organization Service (SOAP), use the
AssociateRequestclass for N:N links, or set the lookup property directly for 1:N/N:1 links.
内容的提问来源于stack exchange,提问作者Russell Quao

