Code throwing errors selectively

Forums Dynamic Forms for SharePoint Code throwing errors selectively

This topic contains 2 replies, has 2 voices, and was last updated by  becca 5 days, 18 hours ago.

  • Author
    Posts
  • #26559

    becca
    Participant

    I have some code in place to set the current user as the value for a people picker field. I have other code that changes fields based on appended text to the url. The people picker code runs fine on the normal newform.aspx but throws errors on the changed urls, despite those changes occurring later in the code than the people picker code. As far as the code is concerned the people picker code should be the same both ways since it’s read first, I’m at a loss for what’s going on (but I’m also new to reading errors in the console so maybe there’s an easy fix?). Below is the relevant code in order it exists in my code

    url field setting code

    
    
    function getUrlParameter(name) {
        name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
        var regex = new RegExp('[\\?&]' + name + '=([^&#]*)');
        var results = regex.exec(location.search);
        return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
    };
    setFieldValue('view',getUrlParameter('view'));

    people picker code

    
    
    $(document).ready(function () {
        // Wait until SP.JS has loaded before calling getWebUserData 
        ExecuteOrDelayUntilScriptLoaded(SetCurrentUsernameToPeoplePicker, "sp.js");
    });
    
    function SetCurrentUsernameToPeoplePicker(){
    	var ctx = new SP.ClientContext.get_current();
    	this.website = ctx.get_web();
    	this.currentUser = website.get_currentUser();
    	ctx.load(currentUser);
    	ctx.executeQueryAsync(Function.createDelegate(this, this.onSucceess), 
    	Function.createDelegate(this, this.onFail));
    }
    
    function onSucceess(sender, args){
    	var loginName = currentUser.get_loginName();
    	var ppDiv = $("div[id$='ClientPeoplePicker'][class='sp-peoplepicker-topLevel']");	
    	var spPP = SPClientPeoplePicker.SPClientPeoplePickerDict[ppDiv[0].id];
    	spPP.AddUserKeys(loginName);	
    }
    function onFail(sender, args){
    	alert('request failed ' + args.get_message() + '\n'+ args.get_stackTrace());
    }

    using the url field

    
    
    if (getFieldValue('view') === "thing1"){
    //hide, show, readonly, set fields
    }
    else if (getFieldValue('view') === "thing2"){
    //hide, show, readonly, set fields
    }
    else {
    //all the rest of my code with rules and responsiveness that doesn't apply to these limited pre-set views
    }

    f12 console error (can’t copy/paste off my work machine so starting brief, let me know if you need more detail)

    
    
    Uncaught ReferenceError: SPClientAutoFill is not defined 
    at Function.SPClientPeoplePicker.AddAutoFillMetaData
    at Function.SPClientPeoplePicker.BuildAutoFillMenuItems
    at Function.SPClientPeoplePicker.UpdateSuggestions
    at Function.SPClientPeoplePicker.UpdateUnresolvedUser
    at Array.ExecutePickerQuery.g
    at SP.ClientRequest
    at Array.<anonymous>

    and for extra clarity, this does NOT happen for url/newform.aspx? but DOES for url/newform.aspx?view=thing1 and url/newform.aspx?view=thing2

  • #26563

    Alexander Bautz
    Keymaster

    Not sure why you get this error, but you can do this with less code – try this:

    function dffs_ready(){
        var currUser = spjs.utility.userInfo(_spPageContextInfo.userId);
        setFieldValue("PeoplePickerFieldInternalName",currUser.Name);
        var view = GetUrlKeyValue("view");
        // if you want to write view to the field named view you can use the below line
        // setFieldValue("view", view);
        switch(view){
            case "thing1":
                alert("Thing 1");
            break;
            case "thing2":
                alert("Thing 2");
            break;
            default:
                alert("No view");
            break;
        }
    }

    Replace PeoplePickerFieldInternalName with your field name.

    Alexander

  • #26720

    becca
    Participant

    Side effect of being new to js and getting references from multiple different sources is code not being designed cohesively, I’ll clean up my code using this and let you know if I’m still getting errors. Thanks a ton 🙂

You must be logged in to reply to this topic.