Tagged: sitecore forms

Sitecore Forms : Custom Submit Action to Create and Publish Content Item (Part 2)

In Part 1, we saw

  • Create Custom Submit Action in Sitecore
  • Create a code-behind class that inherits SubmitActionBase
  • Create a secure API that can create and publish an item

https://madhuanbalagan.com/sitecore-forms-custom-submit-action-to-create-and-publish-item-from-form-data-part-1

Now, let’s focus on 

  • Create a separate Authorization in the Identity Server for Forms 
  • Create a Controller to bridge between Custom Submit Action and API call
  • Set the Custom Submit Action fields

1. Create a separate Authorization in the Identity Server for Forms 

Add the FormsServerClient node and its value to the Identity Server’s Sitecore.IdentityServer.DevEx.xml file. After making the change, make sure to IIS Reset for the change to be in effect. 

Note: Don’t forget to add transforms for ClientSecret!

 

Ideally, you can call it in Postman to verify that it’s generating a token using Form’s ClientID and ClientSecret values mentioned in the Sitecore.IdentityServer.DevEx.xml file.

Now that the config is all set to have its own ClientID and ClientSecret, let’s set up the Authorize method with BearerToken specifically for Form’s Identity Credentials.

The SitecoreRestServices handles authenticated HTTP requests to Sitecore’s REST APIs, managing access tokens and retrying requests if authentication fails. It uses dependency injection to configure the HTTP client and obtain the necessary settings.

2. Create a Controller to bridge between Custom Submit Action and API call

Let’s bridge the Custom Submit Action and API call – The CustomController class extends SitecoreController and uses dependency injection to obtain an instance of ICreateAutoPublishService.

It defines a CreateAndPublish method – HTTP POST endpoint that processes CreateAndAutoPublishModel, calls the service to create and publish content, and returns a JSON response indicating success or failure.

The method includes error handling to return appropriate HTTP status codes and messages for different exceptions.

3. Set the Custom Submit Action fields

The final step – Let’s set the Model Type and Error Message based on the class we created. 

Publish the form and its related items. It should be good to go!

Hope this helps.

Happy Sitecoring!

0

Sitecore Forms : Custom Submit Action to Create and Publish Content Item(Part 1)

 

Problem

I encountered a situation that the website was internally facing, which needed a form that would not save the data in the Experience Forms database. Also the data needs to be saved as an Item in the master database so it could be published and consumed in a listing page.

At first, I considered creating a Custom Submit Action to create and publish an item. However, I later realized that the CD server lacked access to the CM server, preventing it from directly generating an item in the master database. So, I came up with the idea of creating a custom API service and combining it with a custom submit action.

Solution

To implement this followed this process

  • Create Custom Submit Action in Sitecore
  • Create a code-behind class that inherits SubmitActionBase
  • Create a secure API that can create and publish an item
  • Create an API authorization user in the identity server 
  • Create a Controller to proxy between Custom Submit Action and API calls
  • Set the Custom Submit Action fields

 

1. Custom Submit Action in Sitecore

Create the custom submit action in the following path /sitecore/system/Settings/Forms/Submit Actions.

Sitecore_Forms_1.png

 

2. Create a code-behind class that inherits SubmitActionBase

Create a class that inherits the SubmitActionBase class and overrides the Execute method which calls the API asynchronously to create and publish an item.

The CreateAndAutoPublish class extends SubmitActionBase<string> and is designed to handle form submissions in Sitecore, creating and auto-publishing content items.

It uses dependency injection to obtain instances of IHttpClientFactoryBaseSettings, and ISitecoreRestServices, which are lazily initialized. The ExecuteAction method validates the form submission context and parameters, then calls the Execute method to prepare a model from form fields, serialize it to JSON, and send it to a Sitecore API endpoint using an HTTP POST request.

The class includes helper methods to extract values from form fields and handles errors by logging them and adding them to the form submission context’s error collection.

 

3. Create an API call that creates and publishes the item  

The API call does the following things – 

  1. Creates an item under a specified folder
  2. Move and Sync the item into a bucket
  3. Publish the item
  4. Clear the Sitecore cache
  5. Ensure the published item is indexed or force index the item
  6. Clear the Vercel Cache for frontend

In Part 2, we will see how to

  • Create an API authorization user in the identity server 
  • Create a Controller to proxy between Custom Submit Action and API calls
  • Set the Custom Submit Action fields

Hope this helps.

Happy Sitecoring!

1

2024 – Contributions

The 2024 year has been great with presenting at Sitecore Symposium, and various Sitecore User Groups worldwide. 

Symposium Presentation: 

 

Blog Posts : 

 

Presentations : 

 

Contributions: 

  • Dec 22, 2023 – Feedback/Review for Sitecore Content Hub Administrator Certification Exam – 
  • Dec 15, 2023Feedback for Sitecore MVP Program – 
  • Mar 01, 2024 – Sitecore CDP & Personalize Certification Exam Beta review
  • April 19, 2024 – Reviewed Sitecore Dev 10 Beta Exam

 

 Co-organized SUG-Pittsburgh Meetups: 

 

YouTube – Videos published for SUG – Pittsburgh  

  • Jan 02, 2024Abstracting personalization – CP
  • Mar 20, 2024Sitecore XM Cloud 101 – A Beginner’s Guide
  • Apr 03, 2024From Brainstorm to Brilliance: The Untold Story of Sitecore Hackathon
  • Apr 12, 2024Sitecore XM Cloud Forms
  • May 6, 2024Achieving Behavioral Personalization with XM Cloud Plus 
  • May 22, 2024Sitecore Search and XM Cloud – Experiences and Learnings
  • June 11, 2024Tips and Tricks for Next.js and Sitecore Headless
  • Aug 5, 2024 Content Hub Web Client SDK – Internal & External Integrations
  • Sep 3, 2024Sitecore Troubleshooting 101
  • Oct 8, 20248 Content Migration Pitfalls to Avoid and Lessons Learned

 

Co-organized SUG-Philadelphia Meetups: 

 YouTube – Videos published for SUG – Philadelphia 

 

Conferences I attended: 

  • Symposium 
  • MVP Summit 
  • MVP Lunches   
  • MVP Webinars 
  • All SUG Pittsburgh and Queen City meetups  
  • Many SUG Boston/ Columbus/ Atlantic meetups   

 

Plans for 2026: 

Learn and contribute: 

  • Composable DXP 
  • Sitecore Saas Offerings 
  • Containerization 

Co-organize : 

  • Monthly SUG-Pittsburgh meetup 
  • Quarterly SUG-Philadelphia meetup 

Present: 

  • SUGCON EU/Symposium 
  • SUG Meetups 

Happy New Year! Happy Sitecoring!

0

My 2022 Reflections

2022 has been a good year so far – I traveled to India and Chicago for the Sitecore Symposium. Lots of learning and fun!

Presentations:

Blog Posts :

Certifications:   

  • Feb 2022 – Sitecore OrderCloud Certification
  • Apr 2022 – Sitecore 10 System Administrator Certification
  • May 2022 – Sitecore CDP and Personalize Certification

Co-organized SUG-Pittsburgh Meetups :

Co-organized SUG-QueenCity Meetup :

Conferences I attended:

  • Sitecore Symposium
  • Virtual Developer Day
  • MVP Sitecore Lunches
  • All SUG Pittsburgh and Queen City meetups 
  • Many SUG Boston/ Columbus/ Atlantic meetups  

Plans for 2023 :

Learn and contribute:

  • Composable DXP
  • Sitecore Send
  • Sitecore CDP and Personalize

Co-organize :

  • Monthly SUG-Pittsburgh meetup
  • Monthly SUG-QueenCity meetup

Present:

  • Sitecore Virtual Developer Day
  • SUGCON EU/Symposium
  • SUG Meetups

Happy Sitecoring!

1

Sitecore Forms missing | No Forms to display – Issue fix

Sitecore Forms were missing in Forms Designer view after being created. I experience the same and here are the things I tried to get fixed.

  • Cleared browser cache
  • Cleared Sitecore cache (/sitecore/admin/cache.aspx)
  • Incognito/private mode to see if Forms are showing up
  • Rebuilt Forms folder from Developer menu (faster way)
  • Rebuilt Sitecore Master Index

After doing above all, Forms weren’t still showing up 🙁  Decided to take a deeper dive.

I checked the JobsViewer(/sitecore/admin/jobs.aspx) to check what jobs are running and to my surprise, the re-indexing only Forms folder was running for more than 2 hours and they were so many jobs queued up (Content Authors may be busy creating more content for the website).

I was wondering what’s causing the indexing queue to be clogged. Checked the Crawling log using Sitecore Log Analyzer to see what’s happening and for every single update, it was triggering re-index synchronously and immediately checked the Indexing Strategy for sitecore_master_index and it was set to syncMaster! 

This was the root cause of the problem because every time the Content Author was making a change it was triggering the re-index synchronously, whereas it should asynchronously re-indexed with an interval. I switched it to the intervalAsyncMaster strategy to fix the issue.

Tip: Sitecore Log Analyzer is a powerful tool for parsing Sitecore log files. It provides the interface to explore and navigate through a large amount of log data.

 

Here is the patch I used to update the index strategy. 

Now within few minutes, all queue jobs cleared and moved to finished jobs.

Checked Forms in Designer view, all forms were showing up! Tada!

Hope this helps someone.

Happy Sitecoring!

0

My 2020 Reflections

2020 has been a rough ride, Covid-19 has changed our lives. Hope we all get vaccinated in the upcoming year. Stay home, stay safe.

Here are my contributions for the year –

Presentations:

  • Feb 11 – Sitecore 9.3 new features and live installation – SUG Pittsburgh
  • March 26 – Smart Experience Forms – SUG Pittsburgh
  • April 12 – Sitecore 9.3 new features and live installation – SUG Jordon
  • July 15 – What’s new in Sitecore 9.3 and Live Installation – SUG QueenCity

Blog Posts:

Podcast:

YouTube:

Conferences I attended:

  • Sitecore Symposium
  • All SUG Boston/ Pittsburgh/Queen City meetups.

Co-organized SUG-Pittsburgh Meetups :

Co-organized SUG-QueenCity Meetups :

Participation:

  • Sitecore Lunch
  • Hackathon

Plans for 2021 :

Learn and contribute:

  • Content Hub
  • Cortex
  • Sitecore AI Personalization
  • Sitecore Experience Forms
  • Docker Containers
  • Sitecore 10

Co-organize :

  • Monthly SUG-Pittsburgh meetup
  • Monthly SUG-QueenCity meetup

Presentations:

  • SUG Meetups
  • Sitecore SUGCON EU/Symposium

Happy Sitecoring!

0

Generic method to read values from any type of Form fields

Are you looking for a generic method to read the values from any type of Sitecore Form fields? Here is the solution.

         private static Dictionary<Guid, string> FormFieldsToDictionary(IList<IViewModel> fields)
		{
			Dictionary<Guid, string> fielDictionary = new Dictionary<Guid, string>();
			foreach (var field in fields)
			{
				fielDictionary.Add(Guid.Parse(field.ItemId), field.GetType().GetProperty("Value")?.GetValue(field, null)?.ToString() ?? string.Empty);
			}
			return fielDictionary;
		}
	private static string GetValue(IViewModel field)
		{
			if (field == null)
			{ return default(string); }


			if ((field as StringInputViewModel) != null)
			{
				return (string)(object)(field as StringInputViewModel).Value;
			}

			if (field is ListViewModel)
			{
				var listField = (ListViewModel)field;
				var array = listField?.Value?.ToArray();
				if (array == null)
				{
					return string.Empty;
				}
				return String.Join(",", array);
			}

			if (field is DateViewModel)
			{
				var dateField = (DateViewModel)field;
				return dateField.Value.HasValue ? dateField.Value.Value.ToShortDateString() : string.Empty;
			}

			if (field is NumberViewModel)
			{
				var numberField = (NumberViewModel)field;
				return numberField.Value.HasValue ? numberField.Value.ToString() : string.Empty;
			}

			if (field is TextViewModel)
			{
				var textField = (TextViewModel)field;
				return (string)(object)textField.Text;
			}

			if (field is CheckBoxViewModel)
			{
				var checkbox = (CheckBoxListViewModel)field;
				return (string)(object)checkbox.Value;
			}


			return default(string);
		}

Utilization:

protected override bool Execute(UpdateContactData data, FormSubmitContext formSubmitContext)
		{
			var fieldsDictionary = FormFieldsToDictionary(formSubmitContext.Fields);

           // From here you can create a model from fieldsDictionary, call any API and return call status

       }

Hope this helps someone. Any questions, leave a comment.

Happy Sitecoring!

0

What’s new in Sitecore Forms 9.3

You can now publish the forms within Forms Designer. No need to navigate to Content Editor to publish. This is pretty handy when developing forms.

Users can now delete submitted data for a particular form and specify a date range.​​

You can now use the Sitecore bot detection functionality to verify whether visitor is human. This removed the need for a Captcha element. Nice work Sitecore!

Now the forms element that allows you to add email confirmation to a form.​​

Users can now use the Redirect submit action to redirect to a URL and pass parameters to it.

To add file upload functionality to your forms, you can now use the File upload forms element. I use to utilize the Forms Extensions module(It’s very nice), it’s now included in forms element. 

 

Apart from these, there are bunch of enhancements made  – 
  • Improved database performance by increasing our ability to prevent deadlocks.​
  • You can now use the client-side API to retrieve form fields, for example when you build a custom submit action that needs to show all the form fields.
  • You no longer need to rebuild the master index after you install Sitecore XP.

 

Hope this helps. Any questions, leave a comment below.

Happy Sitecoring!

0

My 2019 Reflections

2019 is going great so far, wanted to share my contributions to the Sitecore community(Awesome!).

Presented Sitecore 9 Forms(Intermediate/Advance) in 7 SUG’s-

• April 18 2019 – SUG Bulgaria
• May 4 2019 – SUG Bangalore
• June 4 2019 – SUG Manchestor, NH
• June 6 2019 – SUG Belarus
• June 12 2019 – SUG Boston
• June 25 2019 – SUG NCR(Delhi -India)
• Aug 29 2019 – SUG Educador

 

The blog posts I have written and one of my blog post received ‘You are life saver, Thank you’ comment.

• Install Sitecore 9.1 quickly in 5 easy steps
• Sitecore 9 Forms: Redirecting to formbuilder on Submit?
• Sitecore 9 Forms: The required anti-forgery cookie __requestVerificationToken is not present
• Sitecore license file expired?
• Sitecore 9.1 Forms: Conditional Logic
• Sitecore 9 Forms: Custom Control – Conditional Section
• Sitecore 9 Forms: Success message
• Sitecore 9 Forms: Custom Submit Action
• Tips and Tricks : GIT Cherry Pick
• Sitecore Certification : Exam and Review
• Sitecore 9 Forms: Save Data
• Sitecore 9 Forms: Custom Regex Validation
• Sitecore 9 Forms: Custom Grouped dropdown
• Sitecore 9 Forms: Uncaught TypeError: Cannot read property ‘unobtrusive’ of undefined
• XConnect: Certificate was not found
• Sitecore 9 Forms: Access landing page fields in FormSubmitContext?
• Sitecore 9 Forms: The date range is invalid. Please select a date range that is within the range of the list.
• Sitecore Forms: Export Data to CSV shows Field Types on column header?
• Sitecore Forms: Redirecting to External URL

Youtube
• How to add Forms Custom Submit Actions – https://www.youtube.com/watch?v=wE_aaFN7GvY

Slack Community
• Active on #wffm-forms

Certification
• Sitecore 9.0 Certified Platform Associate Developer

Participation
• Hackathon

Conferences I attended
• SUGCON India
• SYMPOSIUM
• All SUG Boston meetups.

2020 Plans

Learn and contribute

  • Cortex
  • Sitecore AI Personalization
  • Sitecore 9.3 Forms
  • Sitecore 9.3 Overall

Co-organize (Virtual)

  • Quarterly SUG-Pittsburgh meetup

Present

  • SUG Meetups
  • Sitecore SUGCON EU/Symposium 2020
0

Sitecore Forms: Export Data to CSV shows Field Types on column header?

I exported the Forms data using Export the selected data into CSV button and noticed one of the column header was showing field type not field name.

I checked at many places, couldn’t figure out. Later I checked on Content Editor – Forms and found that item name was showing the field type!

I gave the appropriate Field Name in Forms and the CSV file looks good now. 

Forms Field Name sets Item Name and Label sets the Title field in Content Editor.

Don’t forget to publish the Forms!

Happy Sitecoring.

0