You are on page 1of 2

4/10/2015 Orchard 

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

CodePlex Project Hosting for Open Source Software Register Sign In Search all projects

HOME SOURCE CODE DOWNLOADS DOCUMENTATION DISCUSSIONS ISSUES PEOPLE LICENSE

New Thread Subscribe

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


Topics: Core, Customizing Orchard, Writing modules Wiki Link: [discussion:584382]

cloudsurfer I have created a widget programmatically through migration as following:


Mar 3 at 2:30 PM
Edited Mar 3 at 2:30
 ContentDefinitionManager.AlterTypeDefinition("MyWidget", x => x
PM
                .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

sfmskywalker Instead of accessing the TitlePart ﴾which isn't part of your type definition﴿, access the WidgetPart, which stores the title for
Developer your widget.
Mar 3 at 3:39 PM

cloudsurfer Thank you spike, it helped alot, I have one another question.
Mar 3 at 7:57 PM
Edited Mar 3 at 8:00 Suppose I have made MyPart attachable and I have added this part into a content type. I have also added the TitlePart
PM 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.

sfmskywalker 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
Developer the title of some content item in the view, best to use the Html.ItemDisplayText helper, which in turn uses
Mar 3 at 8:32 PM IContentManager.GetItemMetadata().DisplayText to get the title of the content item ﴾which is provided by certain
Edited Mar 5 at 4:34 content handlers, such as the content handler in the Title core module﴿.
PM You could even do this from your property getter, since every content item has a reference to the content manager:

http://orchard.codeplex.com/discussions/584382 1/2
4/10/2015 Orchard Project ­ How to get the Title of the Widget inside a Part Property

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.

cloudsurfer That was a very good explanation. Thanks alot Spike.


Mar 4 at 4:57 AM

Sign in to post message or set email notifications

© 2006‐2015 Microsoft Get Help Privacy Statement Terms of Use Code of Conduct Advertise With Us Version 3.31.2015.20983

http://orchard.codeplex.com/discussions/584382 2/2

You might also like