Creating a widget as a custom user control--inside the widget, is there a way to get the widget instance ID?
Hello,
I'm creating a custom widget, similar the NewsRotator widget described here.
After I register that widget and drop it onto several pages, Sitefinity is internally going to assign it a unique ID in the database, where it tracks what belongs on each page.
From inside the widget, is there a way to get at that unique ID? I'm working on a Quiz widget. When a user drops the widget on a page, it needs to be able to allow the user to add, edit, and update questions for that particular quiz, without messing with any other Quizzes that have been placed on other pages.
I could count on the user making sure that they use globally unique control IDs (which you can define in the advanced options for a widget that you've dropped onto a page), but I would much rather not do this. I know that internally Sitefinity must have a way to uniquely identify every instance of a widget, and I'm hoping there's somewhat I can tap into that. This is similar to the ModuleId property that DNN Evoq exposes for custom modules.
I looked through the SimpleView class using dotPeek and I couldn't find anything that looked like an instance ID.
Any help?
-Josh
So after searching the forums and pouring over the APIs, here's what I came up with.
Essentially, I'm doing the following:
1) Get the Guid of the current node
2) Find the Telerik.Sitefinity.Pages.Model.PageData object for the current node
3) Inspect the PageData object to find a control which has an "ID" property that matches this.ID. I'm going on the assumption that any given page will have unique control IDs.
You'll wind up with a Telerik.Sitefinity.Pages.Model.PageControl object that corresponds to the "this" object.
// Get the current Guid of this page node
Guid currentPageGuid = SiteMapBase.GetActualCurrentNode().Id;
// Retrieve the PageData object for the PageNode. We look up
// the PageData by the Guid of the PageNode
PageData pageData
= Telerik.Sitefinity.App.WorkWith().Pages().LocatedIn(PageLocation.Frontend)
.Where(pt => pt.Id.Equals(currentPageGuid)).Get().FirstOrDefault().GetPageData();
// Find the PageControl that has a property named "ID"
// with the same value as this.ID
PageControl tpc =
pageData.Controls.FirstOrDefault
(c => c.Properties.Count(p => p.Name ==
"ID"
&& p.Value ==
this
.ID) > 0);
string
controlInfo =
""
;
if
(tpc !=
null
)
// Do whatever you'd like with the Telerik PageControl associated
// with this user control. Here I am dumping some info to a string
controlInfo =
"Telerik Page Control for this User Control: "
+ tpc.Id.ToString() +
" -- "
+ tpc.ObjectType;
Hello Josh,
Thank you for sharing this with the community.
This is indeed the correct approach. The id of the User Control is contained withing the this.ID property.
Regards,
Velizar Bishurov
Telerik