Nick Sarabyn
Nick Sarabyn

Reputation: 1680

Default new child list item to selected parent list item

I have a Sharepoint list called Projects and another list called Tasks. The Tasks list has a lookup field to the title of the projects list so that I can use the "Insert Related List" option. Once the related list is inserted, selecting a project will display only tasks associated with that project.
How can I have the Tasks list default the project lookup value to the currently selected project when a new task is added?

Upvotes: 0

Views: 4301

Answers (1)

Nick Sarabyn
Nick Sarabyn

Reputation: 1680

Based on Corey Martins related lists prefill scripts I was able to get the project to auto-select for lists. I modified the scripts to add a few additional features:

-Now uses the popup new item dialogs rather than switching to the new item page.
-Now works with announcement lists and document libraries (document libraries need to have the javascript added to the edit form not the new form).
-Will populate the SelectedID URL parameter which was not happening for me when the list was first loaded.

Here are my modified scripts:
RLHelper-ParentDisplayForm.js

/*
SharePoint 2010 Related List Prefill Version 1.2
Call JQuery and this file from the parent list's view item page that contains related list web parts.
Instructions: http://code.google.com/p/sp2010-related-list-prefill/
RLHelper-ParentDisplayForm.js
*/
function getQuerystring(ji) {
    hu = window.location.search.substring(1);
    gy = hu.split("&");
    for (i=0;i<gy.length;i++) {
        ft = gy[i].split("=");
        if (ft[0] == ji) {
            return ft[1];
        }
    }
}

_spBodyOnLoadFunctionNames.push("updateSelection");
function updateSelection() {
    var selId = getQuerystring("SelectedID");
    if (isNaN(selId) === true) {
        SelectField('VIEW GUID GOES HERE','1');
    }
    return false;
}

RLHelper-ChildNewForm.js

/*
SharePoint 2010 Related List Pre-fill Version 1.2
Call JQuery and this file from the child list's new item page.
Instructions: http://code.google.com/p/sp2010-related-list-prefill/

RLHelper-ChildNewForm.js
*/
function getQuerystring(ji, fromParent) {
    var hu;
    if(fromParent){
        hu = parent.window.location.search.substring(1);
    }
    else{
        hu = window.location.search.substring(1);
    }
    var gy = hu.split("&");
    var i = 0;
    for (i=0;i<gy.length;i++) {
        var ft = gy[i].split("=");
        if (ft[0] === ji) {
            return ft[1];
        }
    }
}

function fillfromParent(childfield) {
    var dlg = getQuerystring("IsDlg", false);
    if (isNaN(dlg) === false && dlg == 1) {
        var SelId = getQuerystring("SelectedID", true);
        var parentid = SelId.match(/\d+$/);
        if (isNaN(parentid) === false && parentid > 0) {
            $("select[title="+childfield+"]").val(parentid);
        }
    }
}

Upvotes: 1

Related Questions