0
votes

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

2
Are the $("select[title='Phase']") being created dynamically or when the page load they exist?DIEGO CARRASCAL
I believe when the page loads, not too sure, the script is added via either a script editor webpart for sharepoint, or a content editor web part which refers to a txt file containing the script. I tried the script with bothPaul
If they are being created after the DOM is loaded (they aren't a part of the original DOM, you should refer to them like $(document).find("select[title='Phase']").val("Closure"); the $(document) allows you to interact with the loaded DOM.DIEGO CARRASCAL
Thanks for the suggestion, I have been playing with _spBodyOnLoadFunctionNames.push() to make sure my script runs after DOMPaul
I added the previous comment by accident. Thanks for the suggestion i did learn some useful things from it. I think the problem may lay with how SP dispalys data in lookup/ choice fields. Originally i wanted to pass a querystring value to a lookup field in newform.aspx but even when running script in _spBodyOnLoadFunctionNames.push() it wasn't working I have since changed my previous page design to push out the ID field of an item instead of it's Title field as the querystring. Now i retreive the title value, by calling on it's ID via the cewp and script belowPaul

2 Answers

0
votes

Not sure whether you used _spBodyOnLoadFunctionNames.push() method for your logics. Usually you have to wait all SharePoint DOM objects are loaded and then to execute your jQuery code. If you used SharePoint JavaScript library, you probably needs to call ExecuteOrDelayUntilScriptLoaded() to make sure your call is called after curtain SharePoint .js files are loaded.

0
votes

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.