3

I'm trying to create a list item in a default SharePoint Tasks list, with a specific parent id to create a hierarchy of tasks, e.g.

Parent Task --> Child Task --> etc.

The internal property "ParentID" is what needs to be set, and via JSOM it works fine, e.g. code:

function newSubTask() {
    var oList = context.get_web().get_lists().getByTitle('TaskList');
    var itemCreationInfo = new SP.ListItemCreationInformation();
    var oListItem = oList.addItem(itemCreationInfo);
    oListItem.set_item('Title', 'new001');
    oListItem.set_item('ParentID', '1');
    oListItem.update();
    context.load(oListItem);
    context.executeQueryAsync(Function.createDelegate(this, function (){
        alert('Item created: ' + oListItem.get_id());        
    }), Function.createDelegate(this, function (ctx, args){        
        alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
    }));
}

(Source: http://blog.csdn.net/tristan_dong/article/details/9358547)

That works as expected and the new task named "new001" is created as a child of task with ID 1.

However, I need to do this in a workflow so I need to use REST, for example using Postman in Chrome;

POST /sites/pwa/_api/Web/Lists/getbytitle('TaskList')/Items HTTP/1.1
Host: [removed].sharepoint.com
Accept: application/json; odata=verbose
Content-Type: application/json
Origin: https://[removed].sharepoint.com
X-RequestDigest: [removed]
Cache-Control: no-cache
Postman-Token: cddcccdf-3a74-0e35-a8db-740293504c4a

{ "Title": "Test from REST", "ParentID": 1 }

Returns the error:

{
  "error": {
    "code": "-1, Microsoft.SharePoint.Client.InvalidClientQueryException",
    "message": {
      "lang": "en-US",
      "value": "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."
    }
  }
}

If I leave out the "ParentID" and just post a body of { "Title": "Test from REST" } it DOES work. (So my Digest and all the other stuff is working fine)

I have tried modifying the ParentID data type, any type of string or integer (primitive value) returns the above error, however an Object requested like { "Title": "Test from REST", "ProjectID": {"ID","1" }} alwaysreturns the following regardless of the contents of the object:

{
  "error": {
    "code": "-2147467261, System.ArgumentNullException",
    "message": {
      "lang": "en-US",
      "value": "Value cannot be null.\r\nParameter name: entitySet"
    }
  }
}

What's going on? Why does it work in JSOM (or CSOM for that matter) but not in REST?

  • BTW, I don't have a ""__metadata": {"type": "SP.Data.PV5ProjektdataListItem" }," parameter in my REST body as that is not accepted anymore for Office365 endpoints, however if I include that and test this in an OnPremise SharePoint 2013 farm, the same errors occur. – Martin Laukkanen Oct 22 '15 at 13:10
2

Your body should look like so:

{
    "__metadata": {
        "type": "SP.Data.TaskListListItem"
    },
    "Title": "Sub Test 4",
    "ParentIDId": 1
}

For lookups, SP takes the column's name and appends "Id" to it for referencing the column in your POST body. It looks funny in this case, but "ParentIDId" is indeed what you should use.

  • It's been a while since I looked at this issue, but this sounds like the correct answer. – Martin Laukkanen Aug 8 '17 at 15:02
0

Not sure if this will work but SharePoint uses a weird notation for lookup fields, something like {0};#{0}.

Try something like this and see if it works:

{ "Title": "Test from REST", "ProjectID": {"ID","{1};#{1}" }}
  • Thanks for the suggestion, unfortunately I have tried that and all sorts of variations based on the SPFieldType. :( – Martin Laukkanen Nov 17 '15 at 16:09
0

I've come to believe that this is either; a. A bug, or b. An internal undocumented feature that is not "not supported". If I had time / budget to take this to MS Support I'm sure they would confirm either of those.

However before throwing out the requirement and re-engineering my solution to not require this, I did create a workaround so I'm going to post this as a "solution".

In effect, the ParentID field does one thing; it is referenced by the SharePoint JSLink script: _layouts/15/hierarchytaskslist.js which is applied automatically to the views of the list in order to show parent child hierarchy, so my workaround was to re-implement this script after creating my own NEW ParentTaskID column.

Below is my JSLink code, however it is a PoC only and only implements one level of hierarchy, my full solution was to literally open the above mentioned sp script and copy the required bits.

SP.SOD.executeFunc("clienttemplates.js", "SPClientTemplates", function() {

  SPClientTemplates.TemplateManager.RegisterTemplateOverrides({
    OnPostRender: function(ctx) {

      var rows = ctx.ListData.Row;
      for (var i=0;i<rows.length;i++)
      {
        var summary = rows[i]["Summary"];
        var rowId = GenerateIIDForListItem(ctx, rows[i]);
        var row = document.getElementById(rowId); 
        if (summary == 'Yes' || summary == 'Ja') {
          row.style.fontWeight = 'bold';
          if (rows[i]["ParentTaskID"] === '1')
            row.cells[2].style.paddingLeft = '16px';
        }
        else 
          row.cells[2].style.paddingLeft = '32px'

      }
    } 
  }); 
});

Maybe someone will find that useful someday..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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