I have a request list with different content types set up for different types of request. I would like to show a different CEWP at the top of each form depending on which type of request is being made. I do not have access to SharePoint Designer. Is there another way to create different .aspx pages for different content types? OR can I conditionally show different CEWPs on the single newform.aspx? Currently I link to the different content types NewForm.aspx by appending the content type ID to the URL. Thanks
-
1Why different CEWPs, and not the same CEWP that shows different content depending on the content type?– ChristopheCommented Nov 3, 2016 at 1:52
-
+1 for @Christophe... this make sense as it would be easier to control content in single CEWP than adding different CEWP for different content type..– Siddharth VaghasiaCommented Nov 3, 2016 at 6:57
-
Yes that would work. I don't 'need' to have two separate CEWPs. How can I show different content in the one CEWP, depending on content type? Thanks in advance– Bee DohCommented Nov 8, 2016 at 1:28
1 Answer
Unless you create your own URL to NewForm.aspx without parameters,
the ContentTypeId is by default present as a QueryString parameter.
So you can add some JavaScript to that single NewForm.aspx (edit in advanced mode) just before the table of the form, that matches by ContentTypeId and prints a header.
If you don't have SharePoint Designer add a ContentEditorWebPart above the Form in the page.
<script>
JSRequest.EnsureSetup();//proces all parameters from the URL Query String
var ctID=JSRequest.QueryString['ContentTypeId'];//get the ContentTypeId from the URL
var titles={
'0x0100223....010019ABEA5C55C59D4383F98D5A938894D8' : 'Expense',
'0x0100223....02006C9B8D0CCE23024EA3A01C9A93FAFBB3' : 'Invoice',
};
//get a defined Title or the CTId when it isn't defined
var title = titles[ctID] || ctID;
//directly write the Title in the document (so the CEWP must be at the top)
document.write( String.format('<h1 title="{1}">New: {0}</h1>', title,ctID ));
</script>
Always get the correct ContentTypeId
After pageload the ContentType info is always present in:
WPQ2FormCtx.ItemContentTypeId
and
WPQ2FormCtx.ItemContentTypeName
But you better make sure you reference the correct WebPart (WPQ2 most of the time)
-
Thanks Danny. I understand how to add code to a page using a CEWP webpart, but JavaScript is beyond my abilities. Does that code cause a different heading to display for each content type? ie if content type id = '0x0100223....010019ABEA5C55C59D4383F98D5A938894D8' , the form heading will be 'Expense'?– Bee DohCommented Nov 8, 2016 at 1:37
-
Yes. I added some comments. Use JSFiddle to play with JavaScript snippets. Note that JSRequest is SharePoint specific code. Commented Nov 8, 2016 at 9:06
-
Hi again, I'm making progress, thanks, but could you show me how to stop an additional title being added after each time I create a new request? After I have made a few requests, the following gets added to the bottom of the code: <h1 title="0x0100FBA03415B8BE3B4996DAB90AD509E44C">New: Domestic / Trans-Tasman</h1> <h1 title="0x0100FBA03415B8BE3B4996DAB90AD509E44C">New: Domestic / Trans-Tasman</h1> <h1 title="0x010069D1A9F47BFC3B49B9F65DEDD7EEBD2200D7C5BB9689E71340844DB3B320C02E8A">New: International</h1>– Bee DohCommented Nov 9, 2016 at 22:56
-
Yes
document.write
will always add a line. You have to learn more JavaScript to add aDIV
element once and change theinnerHTML
of thatDIV
Commented Nov 10, 2016 at 7:42