0

When I submit the info to the list, I was getting the following error (which I assume is due to trying to incorrectly post to a Person or Group column:

A 'PrimitiveValue' node with non-null value was found when trying to read the value of a navigation property; however, a 'StartArray' node, a 'StartObject' node, or a 'PrimitiveValue' node with null value was expected.

So in my item I am sending I changed my Person or Group field value from

"Employee" : employeeName,

//to

"Employee": {
                "results" : employeeName
              },

and this causes me to a get a new error:

value: "Value cannot be null.\r\nParameter name: entitySet"

I have also tried posting to the Person or Group field like so,

"Employee": {
                "results" : {
                  "__metadata" : { type: "SP.Data.UserInfoItem"},
                  "Title" : employeeName
                }
              },

and to no surprise, I get the same error. The original I tried posting to a Single line of text field and it worked.

UPDATE

Here is the update to the item I am trying to post per Ganesh's answer, and I am still getting the same answer.

              const employeeName = $("#empName").val();
              const daySelect = $("#daySelect").val();
              const startDate = $("#startDate").val();
              const endDate = $("#endDate").val();
              const totalHours = $("#totalHours").val();
              const leavePurpose = $("#leavePurpose").val();
              const totalPTO = $("#totalPTO").val();

var myUserId = _spPageContextInfo.userId; // To get current user ID

const item = {
              "Title": newItemTitle,
              "Employee": {
                  "Title" : employeeName,
                  "Id" : myUserId
              },
              "FullorPartial": daySelect,
              "StartDate": startDate,
              "EndDate": endDate,
              "TotalHrs": totalHours,
              "LeavePurpose": leavePurpose,
              "AvailablePTO": totalPTO
              };
1
  • 1
    What value is stored in employeeName? You have to pass the user "ID" to set the person or group field. User display name or email will not work. Commented Jun 27, 2022 at 16:33

2 Answers 2

1

Person or Group fields are special fields in SharePoint. You cannot just pass the text (user display name or email address) to set the person or group field like single line of text field.

To set person or group field, you hav to pass the user ID.

Example:

To set the current user in person or group field use below code:

var userId = _spPageContextInfo.userId; // To get current user ID
var itemProperties={Title:"New Item",EmployeeId:userId};

Note ID added after the internal name of person or group column. You can get the internal name of your person or group column by following this article: How to find the Internal name of columns in SharePoint Online?


Update from comments:

No need to pass the employee name to person or group field. Only pass the ID like:

const item = {
              "Title": newItemTitle,
              "EmployeeId": myUserId,
              "FullorPartial": daySelect,
              "StartDate": startDate,
              "EndDate": endDate,
              "TotalHrs": totalHours,
              "LeavePurpose": leavePurpose,
              "AvailablePTO": totalPTO
              };

If Employee column allows multiple selection, use this:

const item = {
              "Title": newItemTitle,
              "Employee": { 'results': [myUserId] },
              "FullorPartial": daySelect,
              "StartDate": startDate,
              "EndDate": endDate,
              "TotalHrs": totalHours,
              "LeavePurpose": leavePurpose,
              "AvailablePTO": totalPTO
              };
2
  • That makes since, the value I am passing into employeeName is an name as a string which I am trying to send to the people or group column. I tried with the userId and and it is still giving me the same error. I even hard coded my userId to test. "Employee": { "Title" : employeeName, "Id" : myUserIdVal },. My userIdVal is the value I hard coded with my userId
    – BeerusDev
    Commented Jun 27, 2022 at 17:23
  • Please see the update to my original post
    – BeerusDev
    Commented Jun 27, 2022 at 17:30
0

I had the exact same issue using PnPjs. I resolved it by manually adding an item to my list in SharePoint, then getting that item programmatically with a GET request and inspecting the results. The column name in my list in SharePoint was 'User'. However, what I received as a property in the item object returned by SharePoint was 'UserId'. Changing the property name in my POST request from 'User' to 'UserId' and just passing the userId value as a number got things working and my item was successfully added to the list.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.