How to get the Title of the Widget inside a Part Property

Topics: Core, Customizing Orchard, Writing modules
Mar 3, 2015 at 3:30 PM
Edited Mar 3, 2015 at 3:30 PM
I have created a widget programmatically through migration as following:
 ContentDefinitionManager.AlterTypeDefinition("MyWidget", x => x
                .Attachable()
                .WithPart("CommonPart")
                .WithPart("IdentityPart")
                .WithPart("MyPart")
                .WithPart("WidgetPart")
                .WithSetting("Stereotype", "Widget"));
I have created a part like MyPart as follows:
public class MyPart: ContentPart
    {
        public string Title
        {
            get
            {
                var titlePart = this.As<TitlePart>();
                return titlePart != null ? titlePart.Title : string.Empty;
            }
        }
    }
This property will return empty if TitlePart is not present.

I want to know how to get The title of the Widget.

Thanks
Developer
Mar 3, 2015 at 4:39 PM
Instead of accessing the TitlePart (which isn't part of your type definition), access the WidgetPart, which stores the title for your widget.
Marked as answer by cloudsurfer on 3/3/2015 at 9:56 PM
Mar 3, 2015 at 8:57 PM
Edited Mar 3, 2015 at 9:00 PM
Thank you spike, it helped alot, I have one another question.

Suppose I have made MyPart attachable and I have added this part into a content type. I have also added the TitlePart along with MyPart part into the content type. Now I will need my property to be smart enough to get Title from TitlePart, if TitlePart is attached or from WidgetPart, if it is a Widget. How should I achieve this. Please suggest.

Thanks.
Developer
Mar 3, 2015 at 9:32 PM
Edited Mar 5, 2015 at 5:34 PM
Well, the best thing would be for your part to not have a Title property that relies on other parts to be there. If you need the title of some content item in the view, best to use the Html.ItemDisplayText helper, which in turn uses IContentManager.GetItemMetadata().DisplayText to get the title of the content item (which is provided by certain content handlers, such as the content handler in the Title core module).
You could even do this from your property getter, since every content item has a reference to the content manager:
public string Title
{
   get
   {
      return ContentItem.ContentManager.GetItemMetadata().DisplayText;
   }
}
Alternatively, you could write the following code, since both TitlePart and WidgetPart implement the ITitleAspect interface:
public string Title
{
   get
   {
      var titleAspect = this.As<ITitleAspect>();
      return titleAspect != null ? titleAspect.Title : String.Empty;
   }
}
The first snippet is preferred since it doesn't care about who or what is providing the title, while the second snippet limits getting the title through ITitleAspect.
Which, in all fairness, is just fine in practice.
Marked as answer by cloudsurfer on 3/3/2015 at 9:56 PM
Mar 4, 2015 at 5:57 AM
That was a very good explanation. Thanks alot Spike.