Creating a widget as a custom user control--inside the widge

Posted by Community Admin on 04-Aug-2018 20:34

Creating a widget as a custom user control--inside the widget, is there a way to get the widget instance ID?

All Replies

Posted by Community Admin on 04-Jan-2016 00:00

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

Posted by Community Admin on 05-Jan-2016 00:00

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;

Posted by Community Admin on 06-Jan-2016 00:00

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

 
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Sitefinity CMS Ideas&Feedback Portal and vote to affect the priority of the items
 

This thread is closed