By some form of filtering e.g.
public Result TObj121(Autodesk.Revit.UI.ExternalCommandData commandData, ref string message, Autodesk.Revit.DB.ElementSet elements)
{
UIApplication UIApp = commandData.Application;
UIDocument UIDoc = UIApp.ActiveUIDocument;
if (UIDoc == null)
return Result.Cancelled;
Document Doc = UIDoc.Document;
FilteredElementCollector FEC = new FilteredElementCollector(Doc);
ElementClassFilter ECF = new ElementClassFilter(typeof(Material));
List<Element> Mats = FEC.WherePasses(ECF).ToElements().ToList();
Element Mat_El = Mats.Find(x => x.Name == "Gold");
if (Mat_El == null)
return Result.Cancelled;
int M_Id = Mat_El.Id.IntegerValue;
return Result.Succeeded;
}