0

i know there is an answer to this in shareponit 2013 and that SharePoint Online/365 should be pretty much the same, however i can't seem to get the following code (not my own, this is taken from a StackExcange Answer ) to work

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>

<script type="text/javascript">

jQuery(document).ready(function($) {

    //get the ProjectName from they query string
    var ProjectName = getParameterByName("ProjectName");

    //Set the corresponding Lookup field value to the Contact ID
    $("select[Title='ProjectName']").val(ProjectName);    

    //use this line to disable the lookup field selection
    $("select[Title='ProjectName']").attr('disabled','disabled')

    //use this line to hide the lookup field and label entirely
    //$("select[title='ProjectName']").closest("tr").hide();            
});

function getParameterByName(name)
{
  name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
  var regexS = "[\\?&]" + name + "=([^&#]*)";
  var regex = new RegExp(regexS);
  var results = regex.exec(window.location.href);
  if(results == null)
    return "";
  else
    return decodeURIComponent(results[1].replace(/\+/g, " "));
}

</script>

I am manually for now adding a URL "/NewForm.aspx?ProjectName=Test2" for example

Test2 (no spaces) is available from my lookup drop down list

Checked the source of my new form page to make sure i had the right field name

    <!-- FieldName="ProjectName"
         FieldInternalName="ProjectName"
         FieldType="SPFieldLookup"
      -->

I also used the Jquery file both from a library in my own site and directly from the source in the above code

Any help would be appreciated.

  • I added the alert alert ('Project Name Is: '+ProjectName); to the script and the alert is returning the correct project name, the problem now is setting that ProjectName Val to my field ProjectName – Paul Jul 14 '16 at 16:08
  • fyi, you do not need that getParameter function, SharePoint gives you at least 3 functions: sharepoint.stackexchange.com/questions/159289/… – Danny '365CSI' Engelman Jul 14 '16 at 18:55
0

Thanks to Danny for putting me onto GetUrlKeyValue

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) {
var GetProjID = GetUrlKeyValue('ProjID'); //extract cat parameter from a query string 
ctx.CurrentFieldValue =  GetProjID; //set lookup field value 
return SPFieldLookup_Edit(ctx); //default template for rendering Lookup field control
 }

 </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

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.