4

I have created a SharePoint Hosted App(Javascript Object Model) that creates lists on the host web.

I need to put some javascript into new and edit forms in order to create the cascaded drop down effect on 2 lookup fields.

Here is how I create the lists and the fields for it:

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

Can you please give me some advise?

Regards,

Marian

2 Answers2

0

You should consider ditching the idea of using NewForm and Editform.aspx. Just write your own form and use the JSOM or WebApi to add/edit list items.

Sample code for adding item to a list:

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

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

phil
  • 3,538
  • 2
  • 24
  • 24
0

Try this.

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

Please let me know if it works ;)

Have a nice day!