How to programatically change link category and connect to a port?

I have a situation in which I need to change a link category and connect it programmatically to a port. I have a linkTemplateMap with two categories which use different fromPortId and toPortId. I found that there is a way to change the category of a link by using setCategoryForLinkData, and i thought that setToPortIdForLinkData is used to specify to which port the link should connect, however this doesn’t seem to work, or am I missing something?

Is the Link supposed to remain connected between the same two Nodes, but changing the category/template will cause it to be reconnected to a new port on the same node? If so, that should work.

But more generally you can just call GraphLinksModel.setToKeyForLinkData in order to connect the “to” end of a link to a particular node, and GraphLinksModel.setToPortIdForLinkData to connect to a particular port on that node.

Or you can set the corresponding properties on the link data object via Model.setDataProperty.

It should connect to a new node.
I’ve tried connecting by calling the two mentioned methods, the GraphLinksModel.setToKeyForLinkData function does work (I can verify by logging the link.data). However, the portId doesn’t appear to be set - each time I try to log it, I get an empty string, but if you say it should work, I might try using setDataProperty.

Just for completeness, I should ask: what value you have set GraphLinksModel.linkToPortIdProperty?

Hi walter, thanks, that was the solution. The diagram worked without setting those properties when I had only one link template… so I missed that, thanks!