Search code examples
javascriptoffice365sharepoint-onlinechoicefieldlookupfield

sharepoint Online/365 Jquery, Set Lookup Column Not working e.i ($("select[Title='Column']")


I've been doing some work on Sharepoint Online/365 and have got stuck trying to set the value of a lookup or choice column in NewForm.aspx

Narrowed my problem down to not being able to set lookup/Choice Columns

I have simplified a code on my page down to

 //Phase being a Choice Column & Closure a case sensitive valid option
$("select[title='Phase']").val("Closure");
//ProjectName being a Lookup Column & Test2 a case sensitive valid entry in the list
$("select[title='ProjectName']").val("Test2"); 

I have no problem setting text fields as the following code works fine on a text field I created

$("input[title='TextField']").val("Test2");

I have used the following Jquery libraries 1.7.2/min.js and 1.11.3


Solution

  • First Thanks "Verona Chen" and " DIEGO CARRASCAL" because of who i've learnt a few other tricks in SharePoint which will help with other projects.

    My original script before the question was trying to use a query string to populate a field in newform.aspx (which i have done on sharepoint 2013 with code i have found here on)

    Unforuntaly with sharepoint online/365 This code was no longer working.

    This code has fixed my issue (though it does change how a few previous pages are constructed)

    Appologies if this doesn't directly answer the above question (as this was me trying to breakdown the overall issue i was having into something simpler and easier to address based on me narrowing down the issue in my original code) however as I am now up and running, it seems only polite to post the outcome.

    Prior to code, i have a projects list with a "ProjectName" field. I was sending the field name into a URL and querystring to get mylist/newform.aspx?ProjectName=Test2

    I was then trying to pull that Test2 into the lookup field (liked to the project list) "ProjectName" in the list "MyList"

    But even when loading the function for my old script with _spBodyOnLoadFunctionNames.push() it wasn't working.

    After playing with this for a while and after some looking around i found this peice of code

    <script type="text/javascript">
    
    (function () {
    var ctx = {};
    ctx.Templates = {};
    ctx.Templates.Fields = {
        'ProjectName': {
            'NewForm': renderTaskCategory
        }
    };
    SPClientTemplates.TemplateManager.RegisterTemplateOverrides(ctx);
    })();
    
    
    function renderTaskCategory(ctx) {
    //extract cat parameter from a query string 
    var GetProjID = GetUrlKeyValue('ProjID'); 
    //set lookup field value 
    ctx.CurrentFieldValue =  GetProjID;
    //default template for rendering Lookup field control
    return SPFieldLookup_Edit(ctx); 
    }
    
    </script>
    

    This means that i have to change my previous url builds to create mylist/newform.aspx?ProjID=2

    This script then finds item ID 2 (which happens to be test2 in this case) and puts the title of item 2 in my lookup field ProjectName

    Thanks again to those that helped earlier. And apologies again if this doesn't directly answer the question i originally asked.