Link to home
Start Free TrialLog in
Avatar of bissellGR
bissellGR

asked on

Sharepoint 2013 - JSOM - Not Saving record when People object added

Hello,

We are running SharePoint 2013, on Premise. I am using JSOM on an .aspx page in SharePoint to create a custom form for a list (as I have to combine two lists for this). When I comment out the People assignment, the solution goes through with an error (SCRIPT5007: Unable to get property 'apply' of undefined or null reference), but it saves the list item. With the People item, it fails without an error message and does not save the item.

This is using JavaScript (JSOM) for the manipulation of the values. As you can see, I am not using the People selector for this form, but just a hidden field that I am storing a value. I have access to the full SP.User object. Based on the reference material below, what I am doing should work.

I have tried the following:
user.get_title (User Full Name)
user.get_id (user ID in SharePoint
user.get_loginName (AD user identifier)

for the value to be sent. It is currently set to the SP.User object, and that is not working either.

Help
Any help on solving the following two problems would be appreciated:
SCRIPT5007: Unable to get property 'apply' of undefined or null reference
Person Set for adding to SharePoint list saving

Error Information/location
// MicrosoftAjax.js
Function.__typeName="Function";Function.__class=true;Function.createCallback=function(b,a){return function(){var e=arguments.length;if(e>0){var d=[];for(var c=0;c<e;c++)d[c]=arguments[c];d[e]=a;return b.apply(this,d)}return b.call(this,a)}};Function.createDelegate=function(a,b){return function(){return b.apply(a,arguments)}};Function.emptyFunction=Function.emptyMethod=function(){};Function.validateParameters=function(c,b,a){return 

Open in new window


HTML (of just the single Person Element)
        <div class="row bhiOutlinedRow">
            <div class="col-xs-12 col-sm-4 bhiCAFLabel"><span class="bhiFormLabel"><span class="ms-formvalidation"> *</span> Requestor:</span></div>
            <div class="col-xs-12 col-sm-8">
                <div id="RequestorName"></div>
                <input type="hidden" id="ff3{$Pos}" name="Requestor" data-type="Person" />
            </div>
        </div>

Open in new window


JavaScript:

            function addCAFForm() {
                siteURL = 'https://intracleandev.bissell.com/legal';
                var clientContext = new SP.ClientContext(siteURL);
                var oList = clientContext.get_web().get_lists().getByTitle('CAF - Full Form');
                var itemCreateInfo = new SP.ListItemCreationInformation();

                this.oListItem = oList.addItem(itemCreateInfo);
                getCurrentUser();

                $("#bhiCAFForm :input").each(function (index, element) {
                    // Let's make sure we are not capturing any buttons here
                    if ($(this).attr('type') != 'button') {
                        switch ($(this).attr("data-type")) {
                            case 'text':
                                oListItem.set_item($(this).attr('name'), $(this).val());
                                break;
                            case 'number':
                                if (!isNaN(parseInt($(this).val()))) {
                                    oListItem.set_item($(this).attr('name'), parseInt($(this).val()));
                                }
                                break;
                            case 'money':
                                if (!isNaN(parseFloat($(this).val()))) {
                                    oListItem.set_item($(this).attr('name'), parseFloat($(this).val()));
                                }
                                break;
                            case 'choice':
                                if ($(this).attr('type') == 'radio') {
                                    oListItem.set_item($(this).attr('name'), $('input[name=' + $(this).attr('name') + ']:checked').val());
                                }
                                else if ($(this).attr('type') == 'checkbox') {
                                    var checkboxes = $('input[name=' + $(this).attr('name') + ']:checked');
                                    var result = new Array();

                                    for (var i = 0; i < checkboxes.length; i++) {
                                        result[i] = $(checkboxes[i]).val();
                                    }
                                    oListItem.set_item($(this).attr('name'), result);
                                }
                                else {
                                    oListItem.set_item($(this).attr('name'), $(this).val());
                                }
                                break;
                            case 'Person':
                            	//var user = SP.FieldUserValue.fromUser(curUser.get_loginName());
                            	var user = SP.FieldUserValue.fromUser("homecare\\hertenrw");
                            	oListItem.set_item('RequestorId', user);
                                console.log($(this).attr('name') + ", " + $(this).val());
                                //                                oListItem.set_item($(this).attr('name'), $(this).val());
                                console.log(curUser.get_loginName());
                                console.log(user);
                                //oListItem.set_item($(this).attr('name'), curUser.get_loginName());
                         //       oListItem.set_item($(this).attr('name'),  SP.FieldUserValue.fromUser(curUser.get_loginName()));
                                break;
                            case 'date':
                                if (($(this).val() != null) && ($(this).val() != "") && (isValidDate($(this).val()))) {
                                    oListItem.set_item($(this).attr('name'), $(this).val());
                                }
                                break;
                            default:
                                oListItem.set_item($(this).attr('name'), $(this).val());
                                break;
                        }
                    }
                });         // end of $(":input").each(function(){
			

                oListItem.update();
                clientContext.load(oListItem);
                clientContext.executeQueryAsync(
                        Function.createDelegate(this, this.addListSuccess),
                        Function.createDelegate(this, this.addListFailure)
                    );
            }


            // The following SharePoint management code is from:
            // http://www.plusconsulting.com/blog/2013/05/crud-on-list-items-using-rest-services-jquery/



            function addListSuccess() {
                console.log("Success");
            }       // end of function addListSuccess(data) {

            function addListFailure() {
                console.log("Failure");
            }

            function getCurrentUser() {
                this.clientContext = new SP.ClientContext.get_current();
                this.oWeb = clientContext.get_web();
                currentUser = this.oWeb.get_currentUser();
                this.clientContext.load(currentUser);
                clientContext.executeQueryAsync(
                    function () {
                        $("#RequestorName").html(currentUser.get_title());
                        $("input[name=Requestor]").val(currentUser.get_loginName());
                        curUser = currentUser;
                    },
                    function () { }
                );
            }       // end of function getCurrentUser() {

Open in new window


Specific section of JavaScript with the issue:
                            case 'Person':
                            	//var user = SP.FieldUserValue.fromUser(curUser.get_loginName());
                            	var user = SP.FieldUserValue.fromUser("homecare\\hertenrw");
                            	oListItem.set_item('RequestorId', user);
                                console.log($(this).attr('name') + ", " + $(this).val());
                                //                                oListItem.set_item($(this).attr('name'), $(this).val());
                                console.log(curUser.get_loginName());
                                console.log(user);
                                //oListItem.set_item($(this).attr('name'), curUser.get_loginName());
                         //       oListItem.set_item($(this).attr('name'),  SP.FieldUserValue.fromUser(curUser.get_loginName()));
                                break;

Open in new window


XML of the SharePoint List Object
      <m:properties>
        <d:FileSystemObjectType m:type="Edm.Int32">0</d:FileSystemObjectType>
        <d:Id m:type="Edm.Int32">38</d:Id>
        <d:ContentTypeId>0x0100540A2BBE36BDFB419BA3433C172D2ED1</d:ContentTypeId>
        <d:Title m:null="true" />
        <d:CAF_x0020_Number m:type="Edm.Double">0</d:CAF_x0020_Number>
        <d:RequestorId m:null="true" />
        <d:Is_x0020_this_x0020_CAF_x0020_a_>DRAFT</d:Is_x0020_this_x0020_CAF_x0020_a_>
        <d:Choose_x0020_one m:null="true" />
        <d:Type_x0020_of_x0020_Agreement m:null="true" />
        <d:Choose_x0020_One_x003a_>New Contract</d:Choose_x0020_One_x003a_>

Open in new window


Highlighted field
        <d:RequestorId m:null="true" />

Open in new window


Reference of code:
http://pointofint.blogspot.com/2014/03/how-to-set-any-spfield-value-with-jsom.html
ASKER CERTIFIED SOLUTION
Avatar of bissellGR
bissellGR

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial