0
votes

When using Kendo Scheduler to work with a SharePoint list I am able to display and edit item but running into an issue adding/creating a new item. I do not receive a javascript error when I save the item, but stepping through the done function in jquery, I do receive a bad request error.

When editing the record, a __metadata object exists in the data, but during a create, this item is missing. The __metadata object contains the url and other important information.

When I stringify the edit call the data looks like this: "{\"__metadata\":{\"id\":\"Web/Lists(guid'2abecf66-35ed-4c67-b1f1-8b7255ebf0e2')/Items(2)\",\"uri\":\"https:///_api/Web/Lists(guid'2abecf66-35ed-4c67-b1f1-8b7255ebf0e2')/Items(2)\",\"etag\":\"\\"4\\"\",\"type\":\"SP.Data.6001C5110ListItem\"},\"Author\":{\"__metadata\":{\"id\":\"e10cdb3d-b3da-4f1f-8a64-fca71ddeafa9\",\"type\":\"SP.Data.UserInfoItem\"},\"Id\":5,\"Title\":\"User name here>\"},\"Id\":2,\"ID\":2,\"Title\":\"6001-C5-110 Test 2 A\",\"Start1\":\"2018-11-19T17:00:00.000Z\",\"OData__x0045_nd1\":\"2018-11-19T19:00:00.000Z\",\"RecurrenceRule\":null,\"RecurrenceParentID\":null,\"CategoryDescription\":\"Test2\",\"IsAllDay\":false}"

The create looks like this: "{\"startTimezone\":\"\",\"endTimezone\":\"\",\"recurrenceException\":\"\",\"ID\":null,\"Title\":\"No title\",\"Start1\":\"2018-11-06T05:00:00.000Z\",\"OData__x0045_nd1\":\"2018-11-06T05:00:00.000Z\",\"RecurrenceRule\":\"\",\"RecurrenceParentID\":0,\"CategoryDescription\":\"\",\"IsAllDay\":true}"

I would think the __metadata tag is necessary. The message I get back from the server when the create is attempted is

"{\"error\":{\"code\":\"-1, Microsoft.SharePoint.Client.InvalidClientQueryException\",\"message\":{\"lang\":\"en-US\",\"value\":\"An entry without a type name was found, but no expected type was specified. To allow entries without type information, the expected type must also be specified when the model is specified.\"}}}"

As I mentioned, I receive a Bad Request error in jquery.

Here is my scheduler code:

$("#scheduler").kendoScheduler({
    date: new Date("2018/11/11"),
    startTime: new Date("2018/11/11 07:00 AM"),
    height: 600,
    views: [
        "day",
        "workWeek",
        "week",
        { type: "month", selected: true },
        "agenda",
        { type: "timeline", eventHeight: 50}
    ],
    save: function (e)
    {
        // alert('scheduler save');
    },
    dataSource: {
        transport: {
            read: {
                url: "https://<ShrePoint Site Collection>/_api/web/lists/getbytitle('6001-C5-110')/items?$expand=Author&$select=Author/Id,Author/Title,ID,Title,Start1,OData__x0045_nd1,RecurrenceRule,RecurrenceParentID,CategoryDescription,IsAllDay&$filter=Start1 ge datetime'2018-11-01T00:00:00Z'",
                //url: "https://demos.telerik.com/kendo-ui/service/tasks",
                beforeSend: function (xhr) {
                    xhr.setRequestHeader("Accept", "application/json; odata=verbose");
                },
            },
            update:
                {
                    url: function (data) {
                        return "https://<SharePoint site collection>/_api/web/lists/getbytitle('6001-C5-110')/items" + "(" + data.ID + ")";
                    },
                    type: "POST",
                    dataType: "json",
                    contentType: "application/json;odata=verbose",
                    headers: {
                        "accept": "application/json;odata=verbose",
                        "X-RequestDigest": $("#__REQUESTDIGEST").val(),
                        "If-Match": "*",
                        "X-HTTP-Method": "MERGE",
                    },
                },
            create: {
                // The create function should perform a similar routine as the update one with a couple of notable differences:
                //      •The newly created data items have no ID, so they must be added by the function script or returned by the remote service.
                //      •The newly created data items must be returned in the success method with their IDs assigned. Otherwise, the DataSource 
                //          instance is going to operate with incorrect data and subsequent data operations can fail.
                //      •If the schema.data configuration is set, the success method should receive the created data item in an object 
                //          with the same structure as the object that is passed to the success method of the read function. See the example below.

                url: function (data) {
                    return "https://<SharePoint Site collection>/_api/web/lists/getbytitle('6001-C5-110')/items";
                },
                type: "POST",
                dataType: "json",
                contentType: "application/json;odata=verbose",
                headers: {
                    "accept": "application/json;odata=verbose",
                    "X-RequestDigest": $("#__REQUESTDIGEST").val(),
                },
                success: function (data) {
                    alert('Success!');
                },
                error:  function (error) {
                    alert('Error!');
                    console.log(JSON.stringify(error));
                },
            },
            save: function (e) 
            {
                alert('saving');
            },
            parameterMap: function (data, type) {

                return kendo.stringify(data);
            }
        },
        schema: {
            data: function (data) {
                return data.d && data.d.results ? data.d.results : [data.d];
            },
            model: {
                id: "id",
                fields: {
                    id: { from: "ID", type: "number" },
                    title: { from: "Title", defaultValue: "No title", validation: { required: true } },
                    start: { type: "date", from: "Start1" },
                    end: { type: "date", from: "OData__x0045_nd1" },
                    recurrenceRule: { from: "RecurrenceRule" },
                    recurrenceId: { from: "RecurrenceParentID", type: "number" },
                    description: { from: "CategoryDescription" },
                    isAllDay: { type: "boolean", from: "IsAllDay" }
                }
            }
        }
    }
});

Thank you for your time!

1

1 Answers

0
votes

Added the __metadata property in the parameterMap section, works now (obviously will not hard code list names, this is just to get it working):

if (type == "create") {
    return kendo.stringify({
        Title: data.Title,
        Start1: data.Start1,
        OData__x0045_nd1: data.OData__x0045_nd1,
        Id: type != "create" ? data.Id : undefined,
        CategoryDescription: data.CategoryDescription,
        __metadata: type == "create" ? { type: "SP.Data.6001C5110ListItem" } : undefined
   });
}