This project has moved. For the latest updates, please go here.

Postback When Changing Content Type

Jun 28, 2016 at 7:56 PM
I'm having an issue where I can't get the postback to trigger automatically after changing the content type. (Disclaimer: I'd rate my coding skills at about a 7/10....so piecing code together without truly understanding how to might be my issue haha)

Currently I have the editform referencing the url to set the content type field. To do so I tried to rig two of your examples together (example 1, example 2) but seem to be missing something. I can get the content type to display correctly but can't seem to figure out why the postback won't trigger.

At first I tried using field.Dropdown.onchange(); but never got that to work. The only way I was able to trigger the change was by telling it the function to run, once a user made a change to the drop down. I know this worked because if you selected a different content type the value would revert back to what the change function was telling it to be, but the form would now show the content type specific fields.

So ultimately what I'm trying to achieve is to have the editform automatically set the correct content type based off the url, and then have the form automatically update to display the fields relevant to that content type. I'll add my current code below. Any help would be beyondddddd appreciated!!

script url's were shortened for this post
<script src="jquery-1.11.3.js"></script>
<script src="sputility.min.js"></script>
<script type="text/javascript">

//Gets parameters from URL
var urlParams;
(window.onpopstate = function () { 
var match, 
pl = /\+/g,  // Regex for replacing addition symbol with a space 
search = /([^&=]+)=?([^&]*)/g, 
decode = function (s) { return decodeURIComponent(s.replace(pl, " ")); }, 
query = window.location.search;
query = query.substring(1); 
urlParams = {}; 
while (match = search.exec(query)) 
urlParams[decode(match[1])] = decode(match[2]); 
})();

function changeCT(){

    var field = SPUtility.GetSPField('Content Type');
    var ct = ''
    var urlValue = urlParams['RootFolder'];
    var str = urlValue;
    var n = str.lastIndexOf('/');
    var result = str.substring(n + 1);
    if(result == 'Discovery'){
        var ct = 'Discovery Document';
    }
    if(result == 'Data Requests'){
        var ct = 'Data Request Document';
    }
    if(result == 'Testimony'){
        var ct = 'Testimony Document';
    }
    
    field.SetValue(ct);
    $(field.Dropdown).on('change', changeCT);

};

// wait for the window to load
$(window).load(function () {

changeCT();

});
</script>
Jul 5, 2016 at 7:14 PM
OK so I ended up figuring it out but figured I'd post my solution in case anyone else ran into this. While stepping through the code in dev tools I found that once it went to actually set the content type 'ChangeContentType' was undefined so the form was never updated. To fix this I tried changing window.load to document.ready and moving the content editor web part referencing the script to below the form, but neither fixed the issue. What ended up working was to put a half second delay before it set the value and adding a condition so that the form didn't continually refresh. Not sure if this is the best way to do this, but for now it works.
<script type="text/javascript">

//Gets parameters from URL
var urlParams;
(window.onpopstate = function () { 
var match, 
pl = /\+/g,  // Regex for replacing addition symbol with a space 
search = /([^&=]+)=?([^&]*)/g, 
decode = function (s) { return decodeURIComponent(s.replace(pl, " ")); }, 
query = window.location.search;
query = query.substring(1); 
urlParams = {}; 
while (match = search.exec(query)) 
urlParams[decode(match[1])] = decode(match[2]); 
})();

function changeCT(){

    var field = SPUtility.GetSPField('Content Type');
    var curVal = field.GetValue();
    
    //prevents code from endlessly looping
    if(curVal == 'Regulatory Proceedings'){
        var ct = ''
        var urlValue = urlParams['RootFolder'];
        var str = urlValue;
        var n = str.lastIndexOf('/');
        var result = str.substring(n + 1);
        if(result == 'Discovery'){
            field.SetValue('Discovery Document');
        }
        if(result == 'Data Requests'){
            var ct = 'Data Request Document';
        }
        if(result == 'Testimony'){
            var ct = 'Testimony Document';
        }
        setTimeout(function(){
            field.SetValue(ct);
            field.Dropdown.onchange();
            },500);
    }

};

// wait for the window to load
$(window).load(function () {

changeCT();

});
</script>