1

I have two list for example List A and List B the List A is The parent of List B means one to many relationship

Is it possible in SharePoint 2013 to have a single form that have fields from both list

if a user submit the form it should first insert data of List A and pack the inserted id and insert data into List B based on Inserted Id of List A

to make it simple: How to create CRUD with one to many relationships using REST?

2

Yep it is possible. I use this for a little Timesheet application. First, the list setup:

  1. Timesheet - The parent, contains metadata
  2. Timesheet Entries - The child, can be many entries per parent
    • Has a "Timesheet" lookup column back to the parent list
    • The Column shows the ID of the parent Timesheet
    • You can't delete the parent Timesheet if there are existing entries (restrict delete turned on)

Second, I created a regular HTML form with jQuery and Knockout.js. This is dependent on your requirements so you can make this however you want and then include your HTML page using a Content Editor Web Part.

Finally, to answer your question, you create your JavaScript for handling the AJAX operations. For example, creating a timesheet (the PARENT):

self.CreateTimesheet = function(title, weekEnding, ownerId) {
    var data = {
        '__metadata': { 'type': 'SP.Data.TimesheetListItem' },
        Title: title,
        Week_x0020_Ending: weekEnding,
        State: 'Started',
        OwnerId: ownerId
    };
    var promise = $.ajax({
        'url': self.sheetUrl,
        type: "POST",
        contentType: "application/json;odata=verbose",
        data: JSON.stringify(data),
        headers: {
            "Accept": "application/json;odata=verbose",
            "X-RequestDigest": $("#__REQUESTDIGEST").val()
        }
    });
    return promise;
};

Then, you can create the Timesheet Entries (the CHILDREN) by populating TimesheetId (remember this is my lookup column back to my parent list):

self.CreateTimesheetEntry = function() {
    var item = {
        '__metadata': { 'type': 'SP.Data.Timesheet_x0020_EntriesListItem' },
        'Title': self.job(),
        'Entry_x0020_UserId': self.userId,
        'Entry_x0020_Date': self.entryDate,
        'TimesheetId': self.timesheetId
        // More fields if you want.....
    };
    var promise = $.ajax({
        'url': self.entryUrl,
        type: "POST",
        contentType: "application/json;odata=verbose",
        data: JSON.stringify(item),
        headers: {
            "Accept": "application/json;odata=verbose",
            "X-RequestDigest": $("#__REQUESTDIGEST").val()
        }
    });
    return promise;
};

I recommend packaging all your AJAX functions into a shared object, that way you can define your constants at the top.

self.siteUrl = "/company";
self.sheetUrl = self.siteUrl + "/_api/Web/Lists(guid'254561B8-20E1-40FE-A710-CF9DBA07182C')/Items";
self.entryUrl = self.siteUrl + "/_api/Web/Lists(guid'32C561F2-EF45-402D-9EBB-BF98A831A4EB')/Items";

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.