Page 1 of 1

Content Types & Related items

Posted: 30 Sep 2013
by Hubbaroo
We are clear on how to create a related item using the information contained in this site. We would like to understand how to create a related item in a list with multiple content types. The current method only creates the default content type. Can you explain how to, based on a field in the parent form, create the related item using other than the default content type.


Thank you.

Re: Content Types & Related items

Posted: 04 Oct 2013
by Dmitry Kozlov
Hello,

You can override the standard function NewItem2 and inject the specific content type id into the new item link by placing the following code into JS-editor of Forms Designer:

Code: Select all

var contentTypeId = '0x010600818BDDA1EEF42845841F01B81D898F04';

if (typeof (window.NewItem2) === "function") {
  var NewItem2Orig = window.NewItem2;
  window.NewItem2 = function (evt, url) {
    var ch = url.indexOf('?') >= 0 ? '&' : '?';
    url += ch + 'ContentTypeId=' + contentTypeId;
    return NewItem2Orig(evt, url);
  }
}
You have to set contentTypeId variable in the required content type id. To get it go to the list settings, select the specific content type and find ctype parameter in the query string. It contains the ID of the selected content type.

Re: Content Types & Related items

Posted: 11 Aug 2017
by Adam Reyes
I realize this is an old post but this doesn't seem to be working in my version of Forms Designer. I'm using Forms Designer v3.1.3

The javascript code you provided doesn't seem to affect the new item button and continues to open the default content type which is "Document".

Re: Content Types & Related items

Posted: 14 Aug 2017
by Nikita Kurguzov
Hello, Adam!
The code still works. Follow these steps to make sure everything works as intended.

1) Make sure you created a New Form for the Content Type. Content Type for the Form can be selected in Forms Designer in the top right corner:
ContentTypeDesigner.png
ContentTypeDesigner.png (2.33 KiB) Viewed 2986 times
2) You need to make sure you use correct ID for the Content Type you are trying to open Form for. Add Content Type field to the Form and use this command in browser's console to get the correct ID:

Code: Select all

fd.field('ContentType').control()._el().find('select').val();
[/code]
ContentTypeField.png
ContentTypeField.png (3.65 KiB) Viewed 2986 times
ContentTypeConsole.png
ContentTypeConsole.png (2.72 KiB) Viewed 2986 times
3) Insert this code inside Parent Form(one with Related Items) and check browser's console to see if it works or not:

Code: Select all

var contentTypeId = '0x010700F7059CD2918A9648AAD4EC65A40178CC';

if (typeof (window.NewItem2) === "function") {
  console.log("NewItem2 updates");
  var NewItem2Orig = window.NewItem2;
  window.NewItem2 = function (evt, url) {
    var ch = url.indexOf('?') >= 0 ? '&' : '?';
    url += ch + 'ContentTypeId=' + contentTypeId;
    console.log("NewItem2 opens");
    return NewItem2Orig(evt, url);
  }
}

Re: Content Types & Related items

Posted: 13 May 2019
by Office365Guy
We have 5 related item controls on the form. Please clarify how we insure the correct one creates the correct content type. In other words, where in the script is the related control part.

Thank you!

Re: Content Types & Related items

Posted: 16 May 2019
by AlexZver
Hi!

Unfortunately, you can't do that for a concrete one. "NewItem2" is a global JS function - you can't override it for a certain Related Items control - only globally.