4

我创建了一个 SharePoint 托管应用程序(Javascript 对象模型),它在主机 Web 上创建列表。

我需要将一些 javascript 放入新表单和编辑表单中,以便在 2 个查找字段上创建级联下拉效果。

以下是我为它创建列表和字段的方法:

    // Create a new list on host web
    var createList = function (listTitle, onSuccess, onFieldsReady) {
    var listCreationInfo = new SP.ListCreationInformation();
    listCreationInfo.set_title(listTitle);
    listCreationInfo.set_templateType(SP.ListTemplateType.genericList);

    var lists = hostWeb.get_lists();
    var newList = lists.add(listCreationInfo);

    currentContext.load(newList);
    currentContext.executeQueryAsync(onSuccess(newList, onFieldsReady), onListCreationFail);
}

    // Create a new field on a list
    var createField = function (list, fieldType, fieldName, fieldDisplayName, fieldRequired, onSuccess) {
    var fields = list.get_fields();
    var fieldXml = "<Field Type='" + fieldType + "' Required='" + fieldRequired + "' DisplayName='" + fieldDisplayName + "' Name='" + fieldName + "'></Field>";
    var createdField = fields.addFieldAsXml(fieldXml, true, SP.AddFieldOptions.addFieldInternalNameHint | SP.AddFieldOptions.addFieldToDefaultView);

    currentContext.load(createdField);
    currentContext.executeQueryAsync(onSuccess, onProvisionFieldFail);
}

你能给我一些建议吗?

问候,

玛丽安

4

2 回答 2

0

您应该考虑放弃使用 NewForm 和 Editform.aspx 的想法。只需编写您自己的表单并使用 JSOM 或 WebApi 来添加/编辑列表项。

将项目添加到列表的示例代码:

jQuery.ajax({
    url: "http://<site url>/_api/web/lists/GetByTitle('Test')",
    type: "POST",
    data: JSON.stringify({ '__metadata': { 'type': 'SP.List' }, 'Title': 'New title' }),
    headers: { 
        "X-HTTP-Method":"MERGE",
        "accept": "application/json;odata=verbose",
        "content-type": "application/json;odata=verbose",
        "content-length": <length of post body>,
        "X-RequestDigest": $("#__REQUESTDIGEST").val(),
        "IF-MATCH": "*"
    },
    success: doSuccess,
    error: doError
 });

参考: http: //msdn.microsoft.com/en-us/library/office/jj164022 (v=office.15).aspx

于 2014-10-21T15:17:13.593 回答
0

尝试这个。

var clientContext = new SP.ClientContext.get_current();
var list = clientContext.get_web().get_lists().getByTitle('ListName');

if(list) {
    var fldCollection = list.get_fields();

    var fieldLookup1 = clientContext.castTo(
        fldCollection.addFieldAsXml('<Field Name="FieldName1" Type="Lookup" DisplayName="My Lookup Field 1" List="Lists/MyLookupList" ShowField="Title" />', true, SP.AddFieldOptions.addToDefaultContentType),
        SP.FieldLookup
    );

    fieldLookup1.set_title("MyLookupField1");
    fieldLookup1.set_description("Lookup field 1 description");
    fieldLookup1.update();
    list.update();

    clientContext.executeQueryAsync(
        Function.createDelegate(this, this.onQuerySucceeded),
        Function.createDelegate(this, this.onQueryFailed)
    );
}

请让我知道它是否有效;)

祝你今天过得愉快!

于 2015-11-11T19:28:51.040 回答