Kentico 12 MVC - Add a Forms selector inside a widget - kentico

How do I add a Forms widget inside another widget? I tried using FormZone() within the widget but nothing shows up.

It is not possible to render a Forms widget inside another widget in Kentico 12 MVC.
You will need to upgrade to Kentico Xperience 13 for this functionality - see https://docs.xperience.io/developing-websites/page-builder-development/rendering-widgets-in-code#Renderingwidgetsincode-Renderingwidgets

Technically, you can render a Form inside a Widget in Kentico 12, however it is not officially supported and requires a bit of custom development.
The key is to use IFormProvider and IFormComponentVisibilityEvaluator to get all the Form info so that you can render it manually (in a Controller):
var formInfo = BizFormInfoProvider
.GetBizFormInfo(formName, SiteContext.CurrentSiteName);
string className = DataClassInfoProvider
.GetClassName(formInfo.FormClassID);
var existingBizFormItem = className is null
? null
: BizFormItemProvider
.GetItems(className)?.GetExistingItemForContact(
formInfo, contactContext.ContactGuid);
var formComponents = formProvider
.GetFormComponents(formInfo)
.GetDisplayedComponents(
ContactManagementContext.CurrentContact,
formInfo, existingBizFormItem, visibilityEvaluator);
var settings = new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver(),
TypeNameHandling = TypeNameHandling.Auto,
StringEscapeHandling = StringEscapeHandling.EscapeHtml
};
var formConfiguration = JsonConvert.DeserializeObject<FormBuilderConfiguration>(
formInfo.FormBuilderLayout, settings);
var prefix = Guid.NewGuid().ToString();
ViewData.TemplateInfo.HtmlFieldPrefix = prefix;
return new FormWidgetViewModel
{
DisplayValidationErrors = true,
FormComponents = formComponents.ToList(),
FormConfiguration = formConfiguration,
FormName = formName,
FormPrefix = prefix,
IsFormSubmittable = true,
SiteForms = new List<SelectListItem>(),
SubmitButtonImage = formInfo.FormSubmitButtonImage,
SubmitButtonText = string.IsNullOrEmpty(formInfo.FormSubmitButtonText)
? ResHelper.GetString("general.submit")
: ResHelper.LocalizeString(formInfo.FormSubmitButtonText)
};
Then you would render the Form Model as an HTML Form using Kentico's Form rendering APIs:
<!-- ~/Views/Form/Form.cshtml -->
#using Kentico.Forms.Web.Mvc;
#using Kentico.Forms.Web.Mvc.Widgets;
#using Kentico.Forms.Web.Mvc.Widgets.Internal
#model FormWidgetViewModel
#{
var config = FormWidgetRenderingConfiguration.Default;
// #Html.Kentico().FormSubmitButton(Model) requires
// this ViewData value to be populated. Normally it
// executes as part of the Widget rendering, but since
// we aren't rendering a Widget, we have to do it manually
ViewData.AddFormWidgetRenderingConfiguration(config);
}
#using (Html.Kentico().BeginForm(Model))
{
#Html.Kentico().FormFields(Model)
#Html.Kentico().FormSubmitButton(Model)
}
You can read about the full configuration and setup in my blog post Kentico EMS: MVC Widget Experiments Part 3 - Rendering Form Builder Forms Without Widgets

Related

Inherited Kentico MVC Widget not registering in the CMS Admin site

I am registering a custom kentico widget.
When it was created, I could not locate it in the collection of other widgets on the page tab in the admin website of the CMS solution.
I also looked for my widget in the CMS_Widget Table with no luck. Is there another table which might house custom widgets?
My Controller
using System.Web.Mvc;
using Website.Contracts;
using Company.Views.FeaturedProduct;
[assembly: RegisterWidget(OptInOptOutWidgetController.Identifier, typeof(OptInOptOutWidgetController), "Opt In Opt Out List", Description = "Lists all products that are opted in", IconClass = "icon-bullseye")]
namespace Website.Controllers.Widgets
{
public class OptInOptOutWidgetController : WidgetController
{
public const string Identifier = "Website.Controllers.Widgets.OptInOptOutWidgetController";
public IApplyLoanService ApplyLoanService { get; }
public OptInOptOutWidgetController( IApplyLoanService _ApplyLoanService)
{
ApplyLoanService = _ApplyLoanService;
}
public ActionResult Index()
{
List<StateInfo> stateInfoList = ApplyLoanService.GetStateDetails();
List<SelectListItem> statesList = new List<SelectListItem>();
OptInOptOutWidgetViewModel viewModel = new OptInOptOutWidgetViewModel();
viewModel.StateList = stateInfoList.Where(x => x.StateName != null).Select(x => new OptInOptOutStateViewModel
{
StateName = x.StateName
}).ToList();
ViewBag.States = viewModel.StateList;
return PartialView("Widgets/OptInOptOut/_OptInOptOutProductList", viewModel);
}
}
}
My View
#using Company.Models.Widgets
#model Company.Models.Widgets.OptInOptOut.OptInOptOutWidgetViewModel
#if (Context.Kentico().PageBuilder().EditMode && Context.Kentico().PageBuilder().Initialized())
{
#Html.Kentico().PageBuilderScripts()
}
<div>
#if (Model.StateList != null)
{
<p>Select State</p>
<select>
#Html.DropDownList("States", new SelectList(Model.StateList), "")
</select>
}
else
{
<p>There are no states</p>
}
</div>
I referenced this post:
Unable to Create New MVC Widget in Kentico 12
I started making updates the code base but still nothing:
The AssemblyDiscoverable was already where it was supposed to be
We have other widgets published to the CMS but there is no clear path on how to reproduce. Are there any suggestions?
The CMS_Widget table is used for Portal Engine Widgets, which are very different from MVC Page Builder Widgets.
Portal Engine Widgets are used to create the Administration UI which is still built on ASP.NET Web Forms technology.
MVC Page Builder Widgets are not used anywhere in the Administration UI, only on the Live Site.
There is no database table that records all Widgets (or any Page Builder components) defined in the Live Site. Instead, these are all discovered at runtime by the Xperience framework based on your [assembly: RegisterX(...)] component registration attributes.
Your Widget will appear in the Page Builder UI in a dialog that opens when you click the + button in a Widget Zone.
If your Widget is registered in the MVC Live Site application, you do not need the AssemblyDiscoverable attribute - that is only required when your Xperience components are in separate class libraries. AssemblyDiscoverable tells Xperience to scan the class library assembly for components - without it, scanning that assembly is skipped.

Display PDF in Vaadin version 14+

What is the best way to display PDF file in Vaadin 14? i want to display a pdf file in a dialog, but i'm not sure how to render the pdf files. I saw some post about embedded pdf view,pdf browser and EmbeddedPdfDocument, but i can't tell if they are compatible with 14 or not.Is there a new method to do this?
There is a third party addon to render a PDF in Vaadin 14.
Your can find it here: https://vaadin.com/directory/component/pdf-browser/
That gives you the possibility to render a pdf with this code:
StreamResource streamResource = new StreamResource(
"report.pdf", () -> getClass().getResourceAsStream("/report.pdf")); // file in src/main/resources/
PdfBrowserViewer viewer = new PdfBrowserViewer(streamResource);
viewer.setHeight("100%");
layout.add(viewer);
Alternatively you can do it in same way as it was commonly done in previous Vaadin framework versions, embedding in IFrame (see Show PDF in a Vaadin View ), which could look something like this
StreamResource streamResource = new StreamResource(
getPresenter().createPdfStreamSource(), report.getName() + ".pdf");
StreamRegistration registration = VaadinSession.getCurrent().getResourceRegistry().registerResource(resource);
IFrame iframe = new IFrame(registration.getResourceUri().toString());
iframe.setHEight("100%");
layout.add(iframe);
To do it in Vaadin Flow (without an addon) and in a dialog as requested, I'd like to present the following code for your dialog which can be called like any other class. I had to tweak Jean-Christophe his answer a bit.
public class ManualDialog extends Dialog {
private IFrame iFrame;
private final String fileName = "fileNameAsFoundUnderYourResourceMap";
public ManualDialog() {
this.setHeight("calc(100vh - (2*var(--lumo-space-m)))");
this.setWidth("calc(100vw - (4*var(--lumo-space-m)))");
buildLayout();
}
private void buildLayout() {
// HEADER
HorizontalLayout header = new HorizontalLayout();
header.setMaxHeight("1em");
header.setAlignItems(FlexComponent.Alignment.CENTER);
header.setJustifyContentMode(FlexComponent.JustifyContentMode.BETWEEN);
header.getStyle().set("margin-top", "-1em");
Span caption = new Span(getTranslation("main.download.manual"));
caption.getStyle().set("color", "black");
caption.getStyle().set("font-weight", "bold");
Icon closeIcon = new Icon(VaadinIcon.CLOSE);
closeIcon.setColor(GENERIC_BUTTON_COLOR.getDescription());
Button closeButton = new Button();
closeButton.setIcon(closeIcon);
closeButton.getStyle().set("border", "none");
closeButton.getStyle().set("background", "transparent");
closeButton.addClickListener(click -> this.close());
header.add(caption, closeButton);
this.add(header);
// PDF-VIEW
iFrame = new IFrame();
iFrame.setSizeFull();
StreamResource resource = new StreamResource(fileName, () -> new BufferedInputStream(getClass().getClassLoader().getResourceAsStream(fileName)));
StreamRegistration registration = VaadinSession.getCurrent().getResourceRegistry().registerResource(resource);
iFrame.setSrc(registration.getResourceUri().toString());
this.add(iFrame);
this.open();
}
}

Highlight links in a Quick Links web part on SharePoint modern page

We have added 4 "Quick Links" web/app part in one section on a SharePoint modern page. We would like to highlight links under "Quick Links web part 2" and "Quick Links web part 4" only. I have added React modern script editor. How do we archive the above requirement using CSS ? If it is not possible in CSS then we would like to introduce JS. I couldn't find a fixed tag name that I can grab and apply CSS except GUID.
You could try to inject CSS by SPFX.
Check the demo shared by hugoabernier.
Main code:
export default class InjectCssApplicationCustomizer
extends BaseApplicationCustomizer<IInjectCssApplicationCustomizerProperties> {
#override
public onInit(): Promise<void> {
Log.info(LOG_SOURCE, `Initialized ${strings.Title}`);
const cssUrl: string = this.properties.cssurl;
if (cssUrl) {
// inject the style sheet
const head: any = document.getElementsByTagName("head")[0] || document.documentElement;
let customStyle: HTMLLinkElement = document.createElement("link");
customStyle.href = cssUrl;
customStyle.rel = "stylesheet";
customStyle.type = "text/css";
head.insertAdjacentElement("beforeEnd", customStyle);
}
return Promise.resolve();
}
}

Liferay instanceable portlet does not work properly

I created a custom instanceable portlet, setting the instanceable property to true:
<portlet>
<portlet-name>portletFiltriPTF</portlet-name>
<instanceable>true</instanceable>
<header-portlet-javascript>/js/mediolanumadvice/portletFiltriPTF.js</header-portlet-javascript>
</portlet>
The problem is that I am able to insert the portlet multiple times inside the same page, but only in one of that the content is visible, as you can see in the following image:
Is there anything that I have to do in addition to set that property?
Thank you all,
Marco
Two things to check:
You might have a portlet on page that's (still) non-instanceable because you've added it before you've made your portlet instanceable. They now have different IDs and need to be removed from the page
You might use IDs that are conflicting - e.g. if both portlets create content with the same ID, they'll end up in the DOM. Use these for formatting or any treatment through JS and weird things happen.
I'm not sure if this will help the original poster, but in case anyone else comes across this...
My project is using React and we mount our React root to an element with a static id, referred to as the html root, in index.jsp.
When you try to instantiate multiple instances of the same portlet on one page, subsequent instances will not render because there are multiple elements with the same id.
The solution for this is to create an instance id of your own and create a new element to replace the html root, so that the html root becomes something of a placeholder. Since the html element is removed every time you add a portlet instance, you can reliably mount your React root multiple times without issue.
Here you can see a util I wrote to create a unique root and replace the placeholder (I call it RootUtil):
export default {
makeId: function(length) {
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for( var i=0; i < length; i++ )
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
},
renderRoot: function(placeholderId) {
// create unique id
var instanceId = this.makeId(10);
var id = "react-root-instance-" + instanceId;
// create unique root
var root = document.createElement("div");
root.setAttribute("id", id);
// find placeholder
const placeholder = document.getElementById(placeholderId);
// replace placeholder
//placeholder.replaceWith(root); **breaks in IE**
placeholder.parentElement.replaceChild(root, placeholder); //workaround
// return the new element
return document.getElementById(id);
}}
And I call it here:
ReactDOM.render(
<Provider store={Store}>
<div>
...
</div>
</Provider>,
RootUtil.renderRoot("html-root"));

Accessing HTML controls inside iFrame

How do you access html controls inside an iframe from javascript in CRM?
I have:
var height = document.getElementById("IFRAME_TransactionProduct_RA").contentWindow.document.getElementById("txt").value;
but that results in "Error on page" and the content is not loaded.
The element I want to access is an html input with id of 'txt':
<input id="txt" type="hidden" />
Here's an example how you copy a value from a CRM field to a control in an embedded HTML control in an IFRAME. I'm assuming the names of the web resource and the field. You'll have to adapt those. You also might throw in a try-catch in case CRM throws in en exception (got the joke?) and please mind that I'm typing the code on my phone so there might be a typo somewhere (auto-correction, yey).
var source = Xrm.Page.data.entity.attributes.get("oneCoolField")
var information = source.getValue();
var customHtml = Xrm.Page.ui.controls.get("WebResource_EmbeddedHtmlContent");
var destination = customHtml.getObject().contentWindow.document;
if(destination) {
var customControl = destination.getElementById("elementToAccess");
if(customControl) {
customControl.value = information;
}
}
EDIT:
This gets you to the web resource.
var customHtml = Xrm.Page.ui.controls.get("WebResource_EmbeddedHtmlContent");
This gets you to the DOM of the IFRAME.
var destination = customHtml.getObject().contentWindow.document;
This gets you to the control on the custom page.
var customControl = destination.getElementById("elementToAccess");
This gets you the contents of the control.
var contents = customControl.innerHTML;
Which part fails on your computer?
With jQuery:
$(Xrm.Page.ui.controls.get('IFRAME_TransactionProduct_RA').getObject()).contents().find('#txt').val();
Pure JS:
Xrm.Page.ui.controls.get('IFRAME_TransactionProduct_RA').getObject().contentWindow.document.getElementById('txt').value;
http://msdn.microsoft.com/en-us/library/gg334266.aspx#BKMK_getObject

Resources