Developer forum

Forum » CMS - Standard features » Send Welcome email to clients [changed error, due to some kind of progress, I think]

Send Welcome email to clients [changed error, due to some kind of progress, I think]

Siv Hansen
Siv Hansen
Reply

I'm trying to create a template that sends a welcome to <business> email to clients, and I want it to contain more text that the recovery url, so I extended the custom section in Website settings with a subject and a content field.

Now I need to grab these fields into my email template, which I have located in the \Files\Templates\ExtranetExtended (not Rapido. If I created the same path in Rapido, it doesn't show up).

I copied the code from an earlier created template (I want the business logo and colours in header and footer). I had to change the include paths. But now I get an error, nullpointerexception


This is my item type settings

 

I have tried two different ways of retrieving the values, but they both fail.

string emailSubject = Pageview.AreaSettings.GetItem("Custom").GetItem("CustomSettings").GetString("EmailNewCustomerSubject");

string emailSubject = Pageview.AreaSettings.GetItem("CustomSettings").GetString("EmailNewCustomerSubject");

 

 I also tried 

string emailSubject = Model.Item.GetItem("Custom").GetItem("CustomSettings").GetString("EmailNewCustomerSubject");

 

Now, how do I proceed?


Replies

 
Nicolai Pedersen
Reply

In your screendump, you can see that the system name of the custom item types is "CustomWebsiteSettings" - so you need to use that:

Pageview.AreaSettings.GetItem("CustomWebsiteSettings")

 
Siv Hansen
Siv Hansen
Reply

Hmm, I hear you, but this does not work either.

In another install, where the setting is on the same place, they refer it like this:

bool showSpesificationTab= Pageview.AreaSettings.GetItem("Custom").GetItem("CustomSettings").GetBoolean("ShowSpecificationTab");
 
This works in that install, to show things on frontend. My template file resides in Templates\ExtranetExtended (outside Rapido) and intended use is an email. I want to refer elements under the Custom (Settings) tab under apps->websites->website.
 
Is it possible that templates outside the Rapido folder cannot use item types?
 
Thanks.
 
Nicolai Pedersen
Reply

Hi Siv

It depends on where the email is send from. If it is send from the backend, there is no pageview context and it will not work.

If it is an email running in the frontend, i.e. forgot password, you can get hold of the pageview by going after Pageview.Current...

BR Nicolai

 
Siv Hansen
Siv Hansen
Reply

From the backend. No workaround available?

 
Nicolai Pedersen
Reply

You can create a new instance of the area directly and get hold of the item in your Razor code like this: 

Dynamicweb.Services.Areas.GetArea(1).Item.*

But the above will give you an Item instance - and not an ItemViewmodel as you know

So something like this - code has not been tried:

int areaid = 1;
    var area = Dynamicweb.Services.Areas.GetArea(areaid).Item;
    object item;
    area.TryGetValue("customSettings", out item);
    if(item.GetType() == typeof(Dynamicweb.Content.Items.Item))
    {
        Dynamicweb.Content.Items.Item myItem = (Dynamicweb.Content.Items.Item)item;
        var subject = myItem["Subject"];
    }

Subject can be changed in the screen where the data is send out.

BR Nicolai

 
Martin Grønbekk Moen
Martin Grønbekk Moen
Reply

Your code is normally working Nicolai, but we have one issue.
This solution is using Rapido, and we are going to get the custom settings from the "Custom" tab in website settings.

The problem here is that this code:

var area = Dynamicweb.Services.Areas.GetArea(1);
var websiteSettingsTabs = area.Item;
var custom = websiteSettingsTabs["Custom"];

Return a string with a number/id and not a item object, since it is of type "Item Tab" not "Item Type".
So how can we get this value (see image below) programatically, from backend.

 
Adrian Ursu Dynamicweb Employee
Adrian Ursu
Reply

Hi Martin,

Can you try (untested)

var custom = websiteSettingsTabs.GetItem("Custom");

Adrian

 
Martin Grønbekk Moen
Martin Grønbekk Moen
Reply

Nice try Adrian, but it still return a object of type String, not Item.

var custom = websiteSettingsTabs.GetItem("Custom");
custom.GetType(); // Return System.String
 
Nicolai Pedersen
Reply

Add the code to a class so you can attach the debugger and find out what properties are available on the item...

 
Morten Bengtson Dynamicweb Employee
Morten Bengtson
Reply

Try something like this...

var area = Dynamicweb.Services.Areas.GetArea(1);
var websiteSettingsTabs = area.Item;

var customItemFieldSystemName = "Custom";
var customItemType = "Custom";
var customItemId = websiteSettingsTabs[customItemFieldSystemName] as string;
var customItem = Dynamicweb.Services.Items.GetItem(customItemType, customItemId);

var customSettingsFieldSystemName = "CustomSettings";
var customSettings = customItem[customSettingsFieldSystemName] as string;

You should probably add some null checks to this code to avoid exceptions.

To help with debugging you can serialize your items and output the json in your template:

<div>
    <pre>@Dynamicweb.Core.Converter.Serialize(websiteSettingsTabs)</pre>
</div>

 

You must be logged in to post in the forum