0

I have a small requirement where in i have a Text field named Employee Name and i want to Populate projects based on that Employee Name from other List. I have already populated the Employee Name but now i want to Populate Projects based on Employee Name.

Requirement : To Fetch Project Name based on Employee Name selection from other list and dynamically changing that Project Name Text field to a Dropdown.

How do i do this?
Thanks.

  • Does Project list has lookup of Employee Name? – users1100 Oct 10 '18 at 5:52
  • No, So actually i am getting Employee name through Rest API and i want to get Project Names based on employees through REST API – jayant khushalani Oct 10 '18 at 6:36
  • I suggest you the list structure(list name, field names, fields types). – LZ_MSFT Oct 10 '18 at 6:59
  • Sure LZ_MSFT. List Name: Should Cost Projects. Projects : The field from which we have to pick up data is Project_x0020_Name(Calculated Column). Now as we have solved my problem earlier the employee names that i was getting in my previous solution based on that i want to filter the above project names. Should_x0020_Cost_x0020_Modeler(People Picker) – jayant khushalani Oct 10 '18 at 7:08
1

The following code for your reference.

<script src="https://code.jquery.com/jquery-1.12.4.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function(){
    var employeeName=$("input[title^='Employee Name']").val();
    $("input[title^='Employee Name']").hide();
    $("input[title^='Employee Name']").after("<select id='EmployeeField' class='ms-RadioText'><option value=''></option></select>");
    $("input[title^='Project Name']").hide();
    $("input[title^='Project Name']").after("<select id='ProjectField' class='ms-RadioText'><option value=''></option></select>");
    var allEmployeeNames=getAllEmployeeNames();
        $.each(allEmployeeNames,function(i,employee){
        $.each(employee.Should_x0020_Cost_x0020_Modeler.results,function(j,item){
            if(employeeName==item.Title){
                $("#EmployeeField").append("<option selected='selected' value='"+item.Title+"'>"+item.Title+"</option>");
            }else{
                if(item.Title!=undefined){
                    $("#EmployeeField").append("<option value='"+item.Title+"'>"+item.Title+"</option>");
                }           
            }   
        });         
    });
    $("#EmployeeField option").each(function() {
        $(this).siblings('[value="'+ $(this).val() +'"]').remove();
    });
    $("#EmployeeField").change(function(){
        $("input[title^='Employee Name']").val($(this).val());
        var projects=getAllProjects($(this).val());
        $("#ProjectField").html("<option value=''></option>");
        $.each(projects,function(i,project){
            $("#ProjectField").append("<option value='"+project.Project_x0020_Name+"'>"+project.Project_x0020_Name+"</option>");
        });
        $("#ProjectField option").each(function() {
            $(this).siblings('[value="'+ $(this).val() +'"]').remove();
        });
    }); 
    $("#ProjectField").change(function(){
        $("input[title^='Project Name']").val($(this).val());       
    });
})
function getAllEmployeeNames(){
    var results;
    $.ajax({
        url: _spPageContextInfo.siteAbsoluteUrl + "/_api/web/lists(guid'4BFF0A20-43A2-4444-881C-18932D468E54')/items?$select=Should_x0020_Cost_x0020_Modeler/Title&$expand=Should_x0020_Cost_x0020_Modeler/Id",
        type: "GET",
        async:false,
        headers: {
            "Accept": "application/json;odata=verbose",
        },
        success: function (data) {
            if(data.d.results.length>0){
                results=data.d.results;
            }
        },
        error: function (data) {
            //alert("Error");
        }
    });
    return results;
}
function getAllProjects(employeeName){
    var results;
    $.ajax({
        url: _spPageContextInfo.siteAbsoluteUrl + "/_api/web/lists(guid'4BFF0A20-43A2-4444-881C-18932D468E54')/items?$select=Project_x0020_Name,Should_x0020_Cost_x0020_Modeler/Title&$filter=Should_x0020_Cost_x0020_Modeler/Title eq '"+employeeName+"'&$expand=Should_x0020_Cost_x0020_Modeler/Id",
        type: "GET",
        async:false,
        headers: {
            "Accept": "application/json;odata=verbose",
        },
        success: function (data) {
            if(data.d.results.length>0){
                results=data.d.results;
            }
        },
        error: function (data) {
            //alert("Error");
        }
    });
    return results;
}
</script>

enter image description here

  • Hi LZ_MSFT, is it possible to fetch project data based on project name? – jayant khushalani Oct 11 '18 at 5:11
  • Yes, using the similar method in the code above. – LZ_MSFT Oct 11 '18 at 6:46
  • So , in order to populate Data based on Project, where will the function be lets say i want to populate the End Date for that project. Where do i write that code? I am confused on that part – jayant khushalani Oct 11 '18 at 7:05
  • Hi LZ_MSFT , could you just give an example as to how will i bring in a date of project on selection of project. If i see that i can work on it now. – jayant khushalani Oct 11 '18 at 7:10
  • Which list store the Project End Date and this list have a Project Name field? – LZ_MSFT Oct 11 '18 at 7:44

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.