【问题标题】:Kendo UI Scheduler create SharePoint item fails. Missing metadata tagKendo UI Scheduler 创建 SharePoint 项目失败。缺少元数据标记
【发布时间】:2019-04-29 04:17:50
【问题描述】:

使用 Kendo Scheduler 处理 SharePoint 列表时,我能够显示和编辑项目,但在添加/创建新项目时遇到问题。保存项目时我没有收到 javascript 错误,但是在 jquery 中单步执行 done 函数时,我确实收到了错误的请求错误。

编辑记录时,数据中存在__metadata对象,但在创建过程中,缺少此项。 __metadata 对象包含 url 和其他重要信息。

当我对编辑调用进行字符串化时,数据如下所示: "{\"__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\":\"这里的用户名>\"},\"Id\":2, \"ID\":2,\"Title\":\"6001-C5-110 测试 2 A\",\"Start1\":\"2018-11-19T17:00:00.000Z\",\" OData__x0045_nd1\":\"2018-11-19T19:00:00.000Z\",\"RecurrenceRule\":null,\"RecurrenceParentID\":null,\"CategoryDe​​scription\":\"Test2\",\"IsAllDay \":false}"

创建看起来像这样: "{\"startTimezone\":\"\",\"endTimezone\":\"\",\"recurrenceException\":\"\",\"ID\":null,\"Title\":\ "无标题\",\"Start1\":\"2018-11-06T05:00:00.000Z\",\"OData__x0045_nd1\":\"2018-11-06T05:00:00.000Z\",\" RecurrenceRule\":\"\",\"RecurrenceParentID\":0,\"CategoryDe​​scription\":\"\",\"IsAllDay\":true}"

我认为 __metadata 标记是必要的。尝试创建时我从服务器返回的消息是

"{\"error\":{\"code\":\"-1, Microsoft.SharePoint.Client.InvalidClientQueryException\",\"message\":{\"lang\":\"en- US\",\"value\":\"找到了没有类型名称的条目,但没有指定预期的类型。要允许没有类型信息的条目,还必须在指定模型时指定预期类型。\"}}}"

正如我所提到的,我在 jquery 中收到错误请求错误。

这是我的调度程序代码:

$("#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" }
                }
            }
        }
    }
});

感谢您的宝贵时间!

【问题讨论】:

    标签: jquery ajax sharepoint kendo-ui


    【解决方案1】:

    在 parameterMap 部分添加了 __metadata 属性,现在可以使用(显然不会硬编码列表名称,这只是为了让它工作):

    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
       });
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-05
      • 1970-01-01
      • 2013-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多