Yes, I got the form info. but it is only returning 2 entries which I have plenty of forms.
I think it maybe doesn't search through the all pages
Index already updated
Hi Vahid, does it have to be programatically? or can you do it through the database?
Hello,
A bit late to the party, but I encountered a similar issue while attempting to retrieve all instances of FormContainerBlock
. Initially, I used the following code to obtain form instances:
var formsInfoCollection = _formRepository.GetFormsInfo(null);
This approach effectively fetched forms located under the 'forms' root folder, aligning with expected behavior. However, it didn't return all form instances located under the 'blocks' root folder or those created 'for this page'— which is where a significant number of form instances were situated for me.
In search of an alternative that could access forms from the 'blocks' root and for this page, I explored various methods within formRepository
to no avail. My exploration led me to experiment with ContentModelUsage
, which ultimately seemed to return the results I expected. For those facing similar challenges, this approach might offer a solution.
var formContainerBlocks = GetAllFormContainerBlocks();
private IEnumerable<FormContainerBlock> GetAllFormContainerBlocks()
{
var references = FindAllFormContainerBlocks();
var formContainerBlocks = new List<FormContainerBlock>();
foreach (var reference in references)
{
if (_contentRepository.TryGet(reference, out FormContainerBlock formContainerBlock))
{
formContainerBlocks.Add(formContainerBlock);
}
}
return formContainerBlocks;
}
public IEnumerable<ContentReference> FindAllFormContainerBlocks()
{
var contentType = _contentTypeRepository.Load<FormContainerBlock>();
var usages = _contentModelUsage.ListContentOfContentType(contentType)
.Select(x => x.ContentLink.ToReferenceWithoutVersion())
.Distinct();
return usages;
}
I decided to share my experience here, as I stumbled upon this thread while searching for solutions to my issue.
Thanks
There is an extension method in EPiServer.Forms.UI for the IContentTypeRepository that does all of this for you:
var forms = _contentTypeRepository.GetExistingForms();
If you decompile the code it does basically the same things ... the following is the decompiled code.
public static IEnumerable<FormInfo> GetExistingForms(this IContentTypeRepository contentTypeRepository)
{
IEnumerable<ContentType> enumerable = from item in contentTypeRepository.List()
where typeof(FormContainerBlock).IsAssignableFrom(item.ModelType)
select item;
List<FormInfo> list = new List<FormInfo>();
IContentModelUsage instance = ServiceProviderExtensions.GetInstance<IContentModelUsage>(ServiceLocator.Current);
IPermanentLinkMapper permanentLinkMapper = ServiceProviderExtensions.GetInstance<IPermanentLinkMapper>(ServiceLocator.Current);
foreach (ContentType item in enumerable)
{
IList<ContentUsage> list2 = instance.ListContentOfContentType(item);
if (list2 != null && list2.Any())
{
list.AddRange(list2.Select((ContentUsage x) => new FormInfo
{
FormGuid = permanentLinkMapper.Find(x.ContentLink).Guid,
Id = x.ContentLink.ID,
Name = x.Name
}));
}
}
return list;
}
Hey Mark,
Thank you for the information. I suspected there would be a mechanism to accomplish this, I just couldn't find it 🙃!
To elaborate further on my implementation, I have a custom form container that extends the default FormContainerBlock
. For example,
public class MyCustomFormContainerBlock : FormContainerBlock
{
// Custom implementation details
}
During the development, I observed that the code snippet below exclusively retrieves forms matching the exact type specified. I was expecting inheritance to play a part but it didn't. I also called Load()
passing in the IFormContainerBlock
interface but that caused a null exception.
var contentType = _contentTypeRepository.Load<FormContainerBlock>();
I later stumbled upon https://world.optimizely.com/blogs/Anders-Hattestad/Dates/2014/2/Query-content-of-inherit-type-or-those-that-implements-an-interface/ which mentions inheritance and offers solutions to this. However, I needed this for a one-off task so just created two lists for each type and merged them. Not ideal, especially if you have lots of extended forms but got the job done. Is there where you tell me there is something to help me here too :) ?
I'll take a look at the GetExistingForms() method you shared.
Thanks
The database is your friend...
SELECT c.pkID, cl.Name
FROM tblContent c, tblContentLanguage cl
WHERE c.pkId = cl.fkContentID AND c.fkContentTypeId IN (SELECT pkId FROM tblContentType WHERE Name = 'FormContainerBlock')
Hi
Is there any way to retrieve the all Episerver forms that created in all sites ?
I've found this one but didn't work:
https://lockevn.medium.com/how-do-i-programmatically-get-a-list-of-all-episerver-forms-that-are-submitted-17476c7b8e58