Calling Selection.AutomaticLink from a C# add-in


The Developer Reference and the SDK both show examples of how to use the AutomaticLink method of the Selection object but nobody has a sample of its use in C#. The sample below uses a dataset that has a column name of “Object Name” with a value that should match to a shape data field named “Name” with the same value.

// specify to use the value in the Object Name column of our data set
System.Array columnNames = Array.CreateInstance(typeof(string), 1);
columnNames.SetValue("Object Name", 0);

// specify to use a shape data field that has its Label value set to ‘Name’
System.Array fieldTypes = Array.CreateInstance(typeof(int), 1);
fieldTypes.SetValue(Visio.VisAutoLinkFieldTypes.visAutoLinkCustPropsLabel, 0);

System.Array fieldNames = Array.CreateInstance(typeof(string), 1);
fieldNames.SetValue("Name", 0);

// this will be the list of shape IDs that were linked
System.Array linkedShapeIDs = Array.CreateInstance(typeof(long), 0);

selection.AutomaticLink(
   ourRecordSet.ID,
   ref columnNames,
   ref fieldTypes,
   ref fieldNames,
   (int)Visio.VisAutoLinkBehaviors.visAutoLinkNoApplyDataGraphic,
   out linkedShapeIDs);

// apply our own data graphic to the selection after the link is successful
selection.DataGraphic = _VisioDocument.Masters["Sample Data Graphic"];

Setting up your Selection object

This method is only available on the Selection object so you might have to build up the Selection object manually unless you are relying on the user to select shapes before you call this method.