Fork me on GitHub
2sxc 8.9 for DNN 7 to 9
Website Builder, Content Manager, App-System: free and amazing - done your way
You are here: Home  >  Docs  >  Prefill new Forms

Prefill Form for New Entity

Introduced in Version 05.05.01

Sometimes you want to pre-fill a new Entity with existing information - like the current date, the users name, or data from another list like a file-ID or infos from an XML-Stream. This is now possible.

Server Side Helpers in 8.4.5

UPDATE since 2sxc 8.4.5 you can also set the prefill with the server-side Edit.Toolbar helper. This is usually much easier. Not fully documented yet, but approx. like this:

@Edit.Toolbar(actions: "new", contentType: "NewsItem", prefill = new { Category = "something" } )

Note: You should have a Sherlock-Snippet in the template editor that helps you with this.

JS Methods (older, harder way)

 

It is not well documented yet, here a quick demo in the current implementation. We're basically doing the following

  1. Test if the user is currently editing this
  2. Test if the title of the entity is the demo-entity (the one configured in the list-settings)
  3. If no, then just give it the default toolbar
  4. If it is the demo entity, then an edit will of course give a new record. Then assemble the toolbar the way the automatic toolbar is built...
  5. ...and give it a parameter "prefill=" with JSON containing keys/values

 

<div class="sc-element">

    @if(DotNetNuke.Common.Globals.IsEditMode())

    {

        if(Content.Title != "Demo Feature")

        {

            @*currently not showing the demo-entity, so show the normal toolbar*@

            @Html.Raw(Content.Toolbar)

        }

        else

        {

            @*currently showing the demo-entity, so show the prefill-edit-button*@

            <ul class="sc-menu-deactivated">

                <li>

<a class="sc-menu-edit" href="javascript: $2sxc.beta.OpenDialog('new', { moduleId: @Dnn.Module.ModuleID, tabId: @Dnn.Tab.TabID, contentGroupId: @List.First().GroupId, sortOrder: 0, returnUrl: 'Default.aspx?tabId=@Dnn.Tab.TabID', prefill: { DocumentedOnPage: 'Page:@Dnn.Tab.TabID', Title: '@Dnn.Tab.Title' }}); ">create for this page <img src="/DesktopModules/ToSIC_SexyContent/Images/Edit.png" /></a>

                </li>

            </ul>

        }

    }

more stuff here...</div>

 

Here the List-version:

@foreach(var Element in List)

{

                var Content = Element.Content;

                <h1 class="sc-element" onclick="$2sxc.beta.OpenDialog('new', { moduleId: @Dnn.Module.ModuleID, tabId: @Dnn.Tab.TabID, contentGroupId: @Element.GroupId, sortOrder: @Element.SortOrder, prefill: { Name: 'Person ' + new Date() }});">

                               @Html.Raw(Content.Toolbar)

                               @Content.Name

                </h1>

}

Tags for this feature

Look inside

Content Item

These are the values a content-editor can manage.
Name Type Value
Title System.String Prefill Form for New Entity
Teaser System.String Sometimes you want to pre-fill a new Entity with existing information - like the current date, the users name, or data from another list like a file-ID or infos from an XML-Stream. This is now possibl...
AddedInVersion System.String 05.05.01
DescriptionLong System.String <p>Sometimes you want to pre-fill a new Entity with existing information - like the current date, the users name, or data from another list like a file-ID or infos from an XML-Stream. This is now poss...
ImageTeaser System.String
Image System.String
DocumentedOnPage System.String Page:465
StatusDocumentation System.String 2
StatusDevelopment System.String 10
Tags ToSic.Eav.Data.EntityRelationship 1848, 1844, 1861, 1910, 2440
RelatedFeatures ToSic.Eav.Data.EntityRelationship 1929, 2875, 2439
DemoAppGuid System.String
DemoPage System.String
DesignedContent ToSic.Eav.Data.EntityRelationship
ReplacementDocs null


Presentation Item

These are additional, optional presentation instructions a content-editor can manage. If none are entered, a default set (predefined by the designer) will be used.

No Presentation parameters specified, using default presentation for this item.


Template file

@using ToSic.Eav.DataSources
@{
	var feature = Content;
	int featureNumber = 0;
	int.TryParse(Request.QueryString["Feature"], out featureNumber); // try from QS
	if (featureNumber > 0)
	{
		var featSource = CreateSource<EntityIdFilter>();
		featSource.EntityIds = featureNumber.ToString();
		var found = featSource.List.FirstOrDefault().Value;
		if (found != null)
		{
			feature = AsDynamic(found);
		}
	}
}

<div class="ym-clearfix co-full-width-inner co-box-with-image sc-element">
    @if(Permissions.UserMayEditContent)
    {
        if (feature.Title != "Demo Feature")
        {
            @*currently not showing the demo-entity, so show the normal toolbar*@
            @feature.Toolbar
        }
        else
        {
            @*currently showing the demo-entity, so show the prefill-edit-button            
<ul class="sc-menu-deactivated">
                <li>
                    <a class="sc-menu-edit" href="javascript: $2sxc(@Dnn.Module.ModuleID).manage.openDialog({ action: 'edit', moduleId: @Dnn.Module.ModuleID, tabId: @Dnn.Tab.TabID, contentGroupId: @List.First().GroupId, sortOrder: 0, returnUrl: 'Default.aspx?tabId=@Dnn.Tab.TabID', prefill: { DocumentedOnPage: 'Page:@Dnn.Tab.TabID', Title: '@Dnn.Tab.Title' }}); ">create for this page <img src="/DesktopModules/ToSIC_SexyContent/Images/Edit.png" /></a>
                </li>
            </ul>*@
	<ul class='sc-menu' data-toolbar='[{"action":"edit", "useModuleList":true, "sortOrder": 0, "prefill": { "DocumentedOnPage": "page:@Dnn.Tab.TabID", "Title": "@Dnn.Tab.Title" } }]'></ul>

        }
    }
    <div>
        <div class="@if (!String.IsNullOrEmpty(feature.Image)) {<text>ym-g50</text>} ym-gl">
            <div class="ym-gbox-half">
                <h1>@feature.Title</h1>
                
                @if(!String.IsNullOrEmpty(feature.ReplacementDocs)) {
                   <h2>Important: new docs available</h2>
                   <div>This information has been improved so it's best to <a href="@feature.ReplacementDocs" target="_blank">continue in the new wiki</a>.</div>
                   <div>This page is currently still left here so old links will continue to work, but the content is not updated any more. </div>
                   <br/>
                   <br/>
                }

	            <div><em>Introduced in Version @feature.AddedInVersion</em></div>
				@if (!String.IsNullOrEmpty(feature.DemoPage) || !String.IsNullOrEmpty(feature.DemoAppGuid))
				{
					<div><strong>Demos of this feature</strong>
				<ul>
						@if (!String.IsNullOrEmpty(feature.DemoPage))
						{
							<li><a href="@feature.DemoPage">live demo</a></li>
						} 
						@if (!String.IsNullOrEmpty(feature.DemoAppGuid))
						{
							<li><a href="/en/Apps/Details?AppGuid=@feature.DemoAppGuid">try this App</a></li>

						}</ul>
					</div>
				}
                <div>@Html.Raw(feature.DescriptionLong)</div>
            </div>
        </div>

        @if (!String.IsNullOrEmpty(feature.Image))
        {
            if (Presentation != null && Presentation.ImageAction == "block")
            {
                @:<a href="@feature.Image" class="fancybox">
			}

            <div class="ym-g50 ym-gr co-image-space" style="text-align:right;">
                <img src="@feature.Image?w=454" />
            </div>

            if (Presentation != null && Presentation.ImageAction == "block")
            {
                @:</a>
			}
        }
    </div>
    
</div>

<div class="sc-content-block-list" @Edit.ContextAttributes(Content, field: "DesignedContent")>
    @foreach(var contentBlock in Content.DesignedContent){
        @contentBlock.Render()
    }
</div>


@{
    // Here's some special stuff necessary to find all related features
    // 1. leave the simple API, go to the power API of entities, must be typed to work
    ToSic.Eav.IEntity ent = AsEntity(feature);

    // 2. Get all incomming and outgoing relationships (all parents & children)
    var entitiesReferringToThis = ent.Relationships.AllParents.Union(ent.Relationships.AllChildren);

    // 3. Filter. We only want relationships to the same type of data
    // 4. Make sure we don't have duplicates with Distinct()
    var RelFeatures = entitiesReferringToThis.Where(c => c.Type.Name == "2SexyContent Feature").Distinct();
}
@if (RelFeatures.Count() > 0)
{
<div class="ym-clearfix co-full-width-inner">
    <h2>Related Features</h2>
    <ol>
        @foreach (var feat in RelFeatures)
        {
            var dynFeat = AsDynamic(feat);
            <li><a href="@(!String.IsNullOrEmpty(dynFeat.DocumentedOnPage) ? dynFeat.DocumentedOnPage : "/en-us/docsmanuals/feature.aspx?feature=" + dynFeat.EntityId)">@dynFeat.Title</a></li>
        }
    </ol>
</div>
}

<h2>Tags for this feature</h2>
<ul>
    @foreach (var tag in feature.Tags)
    {
        <li><a href="/en/Docs-Manuals?tag=@tag.Tag">@tag.Tag</a></li>
    }
</ul>
2serve . 2invent . 2create is 2be.