Sitefinity ASP.NET CMS - Content Management System

Support Forum Thread

Home >  Support >  Forums home > Sitefinity 3.x > Modules > Extending Existing Modules

Reply
brettlj, 10/4/2007 3:24:53 PM
I need to extend the News module so that attachments can be included along with each news item (we're using it for press releases, and attaching a PDF version with each release is one of the client's requirements). 

I haven't been able to find much documentation for extending the existing built-in modules--can you provide some general recommendations for doing so?  Of course, we won't be able to avoid merge issues every time Sitefinity is updated, but some tips on how to minimize those issues would be greatly appreciated as well.

Thanks!

Brett

Reply
bnye, 10/5/2007 12:25:51 PM
This might not be as complete a solution as you were hoping. However,  you can simply use the document manager to include a link to the pdf at the beginning of the news item.

Sincerely,

Ben

Reply
, 10/8/2007 6:52:47 AM
Hi Brett,

Actually there is a very simple way to achieve this.
All Generic Content based modules (Generic Content, News, Blogs) have the ability to define custom meta fields. So the basic idea is to define a PDFAttachement meta field to News module, add control that will accept the URL of the attachement and then in SingleNews and/or NewsList control add hyperlink control that will display this URL (for your PDF attachement). That's the solution in short. I'll give you now some more detailed instructions.

Defining a new meta field

Open the web.config file. In the <cmsEngine> section find the <metaFields> section. You'll see there numerous meta fields that modules use by default. Add the following key :

<add key="News.PDFAttachement" valueType="ShortText" visible="True" searchable="false" sortable="False" defaultValue=""></add>


Basically, you are telling Sitefinity that with each news item a PDFAttachement field, which is of ShortText type will be stored. So far so good. Now you need to provide some user interface in the admin area for your users to enter the URL of .PDF file for the news item.

Modifying admin templates

Next thing you need to do is modify the templates that Sitefinity uses for inserting new news items and editing existing news items. To do so, you need to open in Visual Studio following files :
  • ~/Sitefinity/Admin/ControlTemplates/News/ControlPanelInsert.ascx and
  • ~/Sitefinity/Admin/ControlTemplates/News/ControlPanelEdit.ascx

Observe how we have created the markup for e.g. Author meta field. You have two options here, you can just add TextBox control and set it's id to PDFAttachement (that's how Sitefinity knows that value of this textbox should be used for saving/displaying PDFAttachement meta field). The drawback of this solution is obviously that the user needs to know exact URL of the attachment (I assume that attachments would be uploaded through the File Manager). Another option you have is to create a custom control that would have maybe a "Select document to attach button". When this button is clicked, you can open RadWindow or something like that and provide a File System treeview, so your users can select the document they wish to attach. If you decide to use a custom control, make sure it implements ITextControl interface; it is the Text property that will be used as value of your meta field.

So, after you've done this, you have set up everything on the administration side. Users have the option to attach a document (by setting the .PDF document URL) to a news item, they can edit it later or remove it. The last step is to display this link on the news public control.

Displaying meta field values on public controls

Once again we are going to modify the templates, only this time we are going to do that for the public controls. Let's assume you only want the PDF attachment link to appear only when user opens a news item. In that case, you only need to modify the template for the SingleNews control. Open the following file in Visual Studio :

  • ~/Sitefinity/ControlTemplates/SingleNews.ascx

What you want to do in this template is very similar to what you have already done in the admin templates. You will add a Hyperlink control that has the id same as the meta key (PDFAttachement) so that Sitefinity knows that this is the control to which the value of that meta field will be bound. However, Sitefinity will only bind the Text property, so you are still left with setting the NavigateUrl. Here you can use a little trick, so your Hyperlink control looks like this :

<asp:HyperLink ID="PDFAttachement" runat="server" NavigateUrl='<% = this.Text %>'></asp:HyperLink>

Basically, you tell the control that the NavigateUrl property is same as the Text property of that control, and the text property will be set by Sitefinity.

And that's it.

Obviously you have countless options here... You could have added two meta fields (attachement file title ad attachement file url), or you can make the meta field to store the collection of files (delimited by semi-colon ";")... it is up to you, but this is the basic principle I would use if it is up to me to create this kind of functionality.

I hope this helps. Let me know if you need any clarifications.

All the best,
Ivan
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center

Reply
brettlj, 10/8/2007 7:09:40 AM
Ivan, that's fantastic...thanks for such a thorough and helpful response!

Ben, thanks for your response too...you're right, I was looking for something a little more than that, but it certainly would have worked for a temporary solution.  :o)

Reply
SelArom, 7/15/2008 8:47:29 PM
any chance to be able to define what property will be defined by the meta? that way I can set different link for text and link.
Reply
SelArom, 7/15/2008 8:50:48 PM
wait, the sample above doesn't work, it's actually trying to embed the actual text < %= this.Text %> into the navigate field!
Reply
, 7/17/2008 2:51:20 AM
Hello SelArom,

One of the reasons why the sample may not work is because user controls will by default use VB.NET if not other specified, and the given sample is in C#. You can try to use Me.Text instead of this.Text.

However, this approach may prove to be limited in some scenarios. I've created a sample control (it's not production quality and not official part of Sitefinity - but it does work and it's open source) which lets you map meta fields to any property. More about this control, as well as the download link, you can find at this link:

http://blogs.sitefinity.com/Ivan/Post/08-07-03/metamaster_2008_finally_released_.aspx

I hope you'll find this information useful. Let us know if there is anything else we can do for you.

All the best,
Ivan
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center

Reply
SelArom, 7/17/2008 9:01:23 AM
that is an AWESOME TOOL! thank you for providing it, it fills a HUGE gap in sf, man I love sf it gets better every day

thanks!