You might find it handy to want to know what the public property data is on a widget (Mvc Controller) sitting on a page. Like perhaps you need to pull in the data over a webservice. Doesn't matter, I'm not here to speculate on what your needs are!
Here's the code:
GetSavedControllerData.csView on GitHub public static class Helper
{
/// <summary>
/// Helper to query the sitefinity database for a Controllers saved properties on a page
/// You can get the controllers widget id like this this.ViewData["controlDataId"]
/// </summary>
/// <param name="controlId"></param>
/// <returns></returns>
public static List<ControlPropertyData> GetSavedControllerData(Guid controlId)
{
var data = new List<ControlPropertyData>();
//We need to lookup the param values from the database, as we can't be sure this widget only exists once on the page
var pageManager = PageManager.GetManager();
var provider = (IOpenAccessDataProvider)pageManager.Provider;
var context = provider.GetContext();
//Get the settings property Id for this control Id
var settingsIdQuery = $"select id from sf_control_properties where control_id = @controlId and nme = 'Settings'";
var controlIdParam = new OAParameter("controlId", controlId);
var settingsId = context.ExecuteScalar<Guid>(settingsIdQuery, controlIdParam);
//The properties are children of settings linked by the prnt_prop_id
var propertyQuery = $"select id, nme, val from sf_control_properties where prnt_prop_id = @settingsId order by nme";
var settingsIdParam = new OAParameter("settingsId", settingsId);
//Okay now we go query the control to get it's current properties
//Keep in mind it only stores CHANGED data
data.AddRange(context.ExecuteQuery<ControlPropertyData>(propertyQuery, settingsIdParam).ToList());
return data;
}
}
Very very important note though. The database only stores CHANGED properties.
So lets say you have a public property called Title
public string Title { get;set; } = "Some title";
The above code snippet will not return a property called Title in the array unless you have edited the widget and changed the Title to something other than "Some title". So you'll need to read the defaults from the Controller if what you need isn't there.
You can get the widgets (Controllers) control id with the ViewData
this.ViewData["controlDataId"]