Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I am struggling with some issue. I need to automatically fill the value of a field (in Edit or New.aspx) based on other lookup column.

Now I have list Lookup with Name and Surname columns

In the second list I have lookup value to this list Name. If some user will select it the idea is to automatically fill the surname of the same ID. But also with option to change this field by the users.

Thanks all for help Dariusz

share|improve this question
    
I was trying to find something connecting to COM and this task but I could fund anything. If you have some info where to start? Thanks Dariusz –  user7322 Mar 12 '12 at 7:43
add comment

1 Answer

I find it very hard to understand your exact criteria for setting the item in the SELECT box. But you should be able to accomplish this with JQuery in a HTML Form webpart. Here is a pseudo-code example.

    $(document).ready(function(){
              //Criteria to determine the value to select.
              $('#really-long-guid-id-of-select').val('value-to-select');
     });

EDIT: enter image description here Looking at the image above, with which you provided me, what you need to do is query List2 for an item where the field "Name" matches the value of the selected item and assign the value of the Surname field to the selected value of the surname select box. Here is a script I use to do something very similar.

function setTitle(){
        var reqType = $('#ctl00_m_g_96b4d1be_781c_44eb_941f_c01b7ae00ede_ff61_ctl00_DropDownChoice').val();
        var requestType;
        if(reqType == 'Delete Process')
        {
            requestType = reqType + ' - ';
        }else if(reqType == 'New Process Rev')
        {
            requestType = reqType + ' - ';
        }
        //get teh text of the selected option.
        var processToReplace = $('#ctl00_m_g_96b4d1be_781c_44eb_941f_c01b7ae00ede_ff371_ctl00_Lookup').find('option:selected').text();

        //Use Client Object Model to query the "Process Requests" list for an item with
        //the value above in its title.
        var context = new SP.ClientContext.get_current();
        var listTitle = "Process Requests";
        list = context.get_web().get_lists().getByTitle(listTitle); 

        //Build a CAML query to return only the item with the newest date (top 1)
        var query = '<View><Query><Where><Contains><FieldRef Name=\'Link_x0020_to_x0020_PDF\' />'
                   +'<Value Type=\'URL\'>'+ processToReplace +'</Value></Contains></Where>'
                   +'<OrderBy><FieldRef Name=\'Created\' Ascending=\'False\' /></OrderBy></Query>'
                   +'<ViewFields><FieldRef Name=\'Title\' /></ViewFields>'
                   +'<RowLimit>1</RowLimit></View>';


        var camlQuery = new SP.CamlQuery();
        camlQuery.set_viewXml(query);

        //all the items returned by the query.
        var allItems = list.getItems(camlQuery);

        context.load(allItems); 

        context.executeQueryAsync( 
            function(sender, args) {
            //This code is run on success.
                var enumerator = allItems.getEnumerator();

                  while (enumerator.moveNext()) {
                    var listItem = enumerator.get_current();
                    //Walk through the collection of items (should be only 1)
                    //Set the value of the text box to the value returned by the query.
                    $('#ctl00_m_g_96b4d1be_781c_44eb_941f_c01b7ae00ede_ff11_ctl00_ctl00_TextField').val(requestType + listItem.get_item('Title'));
                    $('#ctl00_m_g_96b4d1be_781c_44eb_941f_c01b7ae00ede_ff11_ctl00_ctl00_TextField').attr('disabled', true);
                  }
            }, 
            function(sender, args) {alert("error: " + args.get_message());} 
        );

    }
share|improve this answer
    
Thanks for fast answer. I am really newbie in AJAX. If you could give me some tips how to start this :$('#really-long-guid-id-of-select').val('value-to-select'); I know a SQL statements and .NET and of course structure of SharePoint. Thank you very much for help –  Dariusz Mar 8 '12 at 15:09
    
Certainly, first JQuery is a JavaScript library. It makes JavaScript much easier to use. You need to add JQuery to your MasterPage. There are a number of resources you can find via Google. Then I suggest a short tutorial on JQuery. Take a look at this tutorial: evotech.net/blog/2008/11/… –  Robert Kaucher Mar 8 '12 at 15:22
    
Once you get an understanding of JQuery view the source of your SharePoint web page and find the <select> tag you wish to set and get the value in the id attribute. You will use that in the JQuery script. Write the if-else logic and set the value of the select tag. –  Robert Kaucher Mar 8 '12 at 15:24
    
And just to be clear, I'm talking about the HTML select tag, not a SQL select. –  Robert Kaucher Mar 8 '12 at 15:34
    
I have added src to JQuery to my site and also spservices and it seams that works. Running $(document).ready(function() { alert("jQuery"); }); give me alert :). I am not sure <select tag is ID Name ?? I have found it is source of the page but i am not sure which value is it. According to the value that I would like to take is the value from different list (which I am connected via lookupfield now –  Dariusz Mar 8 '12 at 15:40
show 12 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.