web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Answered

$ref error Associating record in a Many-to-Many relationship Web API

(1) ShareShare
ReportReport
Posted on by 9
I'm receiving the following error: The URI segment '$ref' is invalid after the segment <relationshipName>. I'm using the following Associate and disassociate table rows using the Web API (Microsoft Dataverse) - Power Apps | Microsoft Learn as an example for my call.  
 
My Code is below.  I've paused in the debugger to make sure on the call that the variables relatedEntitySetName, primaryEntitySetName, relationshipName, and the ids are set correctly.  Any idea what I may be doing wrong?
 
    var association = { '@odata.id': formContext.context.getClientUrl() + /api/data/v9.2/ + relatedEntitySetName + ( + relatedId + ) };
 
    var req = new XMLHttpRequest();
    req.open(POST, formContext.context.getClientUrl() + /api/data/v9.2/ + primaryEntitySetName + ( + primaryId + )/ + relationshipName + /$ref, true);
    req.setRequestHeader(Accept, application/json);
    req.setRequestHeader(Content-Type, application/json; charset=utf-8);
    req.setRequestHeader(If-None-Match, null);
    req.setRequestHeader(OData-MaxVersion/, 4.0);
    req.setRequestHeader(OData-Version, 4.0);
 
 
Apparently quotes are removed during posting.  Assume they're in their proper places. 
I have the same question (0)
  • Verified answer
    Trent Profile Picture
    9 on at
    Solved it. There are three things that you need to make sure of:
    1.) Give the relationship a name that is less than the max. Don't let the system truncate the name for you. (This is what was getting me)
    2.) Make sure you're using the relationship name, not the related table name (I tried both)
    3.) It's case sensitive.
  • haseem Profile Picture
    23 on at
    It seems you might have a formatting issue in your URI construction. 
    Ex...
    var association = { '@odata.id': formContext.context.getClientUrl() + "/api/data/v9.2/" + relatedEntitySetName + "(" + relatedId + ")" };
    var req = new XMLHttpRequest();
    req.open("POST", formContext.context.getClientUrl() + "/api/data/v9.2/" + primaryEntitySetName + "(" + primaryId + ")/" + relationshipName + "/$ref", true);
    req.setRequestHeader("Accept", "application/json");
    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    req.setRequestHeader("If-None-Match", null);
    req.setRequestHeader("OData-MaxVersion", "4.0");
    req.setRequestHeader("OData-Version", "4.0");

     
     
  • Suggested answer
    Guido Preite Profile Picture
    54,086 Moderator on at
    You can use my tool Dataverse REST Builder to generate the exact code based on your instance, one of the request type is "Associate"

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
ManoVerse Profile Picture

ManoVerse 196 Super User 2026 Season 1

#2
11manish Profile Picture

11manish 129

#3
CU11031447-0 Profile Picture

CU11031447-0 100

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans