SPLookupAddNew default to newly created item

Sep 13, 2011 at 4:26 AM

Is it possible to have the return windows lookup value default to the the newly created item in SPLookupAddNew? I threw together this code, which populates the input box(lookup) value, but when I save it the lookup value does not save (I'm assuming because only the name is passed into the value and not the id+name). I am using SP 2010.   Note my Lookup has over 20 items so the dropdown is rendered as an input box.

 

 

<script src="/Config%20Files/jquery.min.js" type="text/javascript"></script><script src="/Config%20Files/jquery.SPServices-0.6.2.min.js" type="text/javascript"></script><script language="javascript" type="text/javascript">



$(document).ready(function() 
{
    var queryStringValues = $().SPServices.SPGetQueryString();
    var title = queryStringValues["Title"];

    
    
        $().SPServices({
            operation: "GetListItems",
            listName: "IIAS",
            async: false,
            CAMLQuery: "<Query><Where><Eq><FieldRef Name='Title'/><Value Type='Text'>" + title + "</Value></Eq></Where></Query>",
            completefunc: function (xData, Status) {
                $(xData.responseXML).find("[nodeName=z:row]").each(function(){
                    san = $(this).attr("ows_Title");
                });
            }
        });

	if(san)
	{
	var SANTextBox = getTagFromIdentifierAndTitle("input","","SAN");
	SANTextBox.value = san;
	}
	
	
	
    function getTagFromIdentifierAndTitle(tagName, identifier, title) 
	{
		var len = identifier.length;
		var tags = document.getElementsByTagName(tagName);
		for (var i=0; i < tags.length; i++)
		{
			var tempString = tags[i].id;
			if (tags[i].title == title && (identifier == "" || tempString.indexOf(identifier) == tempString.length - len)) 
			{
				return tags[i];
			}
		}
		return null;
    }
	
	




});</script>

 

 

Thanks,

Colin

 

Coordinator
Sep 13, 2011 at 2:31 PM

Colin:

That funky input/select hybrid takes some work to understand. I think you probably need to trigger a click event on the down arrow image to get the value to actually regiester. You'll need to play around with it and figure out what works.

Also, in the line below, note that as of jQuery 1.5, you must use quotes around z:row. I'm surprised that your code above is working.

$(xData.responseXML).find("[nodeName='z:row']").each(function(){

M.

Sep 13, 2011 at 3:28 PM
Edited Sep 13, 2011 at 3:28 PM

Thanks Marc, I will play around with that and post the code when I figure it out.

 

Also thanks for the heads up on the z:row!!! I am using the latest version of jQuery and I don't use the quotes in any of my JavaScript and everything has worked like a charm so far but I will go through and update all my scripts to make sure I don’t run into any errors down the road. I haven’t tested in any other browsers besides IE 9 because that is all that is supported at my company, might that be why it’s working?

 

Also on a side note, I threw together a script that pulls in a CSV, parses through it, and uploads it to a list using your tools, is that something you'd be interested in putting into a future version if I got the code your way?

 

Colin

Sep 13, 2011 at 3:54 PM

Seems like a pretty cool solution Colin.  Mark Rackley has spent some time deciphering the 20+ lookup problem.  Take a look at this post:

http://sharepointhillbilly.com/archive/2011/04/06/setting-sharepoint-drop-down-lists-w-jquery---20-items.aspx

It might be all you need.

Cheers,

Matt

Sep 14, 2011 at 1:05 AM
Edited Sep 14, 2011 at 1:24 AM

Thanks for that Matt, unfortunately his method doesn’t work for me. In his method he is setting the optHid value to the ID of the lookup, when I dig into the source code of my form I see that optHid references a hidden inputs value. After figuring that out I was able to get everything to work with a minor modification to my script above. Hopefully this helps others. I am using SharePoint 2010 Standard Edition.

 

<script src="/Config%20Files/jquery.min.js" type="text/javascript"></script><script src="/Config%20Files/jquery.SPServices-0.6.2.min.js" type="text/javascript"></script><script language="javascript" type="text/javascript">



$(document).ready(function() 
{
    var queryStringValues = $().SPServices.SPGetQueryString();
    var title = queryStringValues["Title"];

    var san;
    var sanid;
    

    $().SPServices(
    {
        operation: "GetListItems",
        listName: "IIAS",
        async: false,
        CAMLQuery: "<Query><Where><Eq><FieldRef Name='Title'/><Value Type='Text'>" + title + "</Value></Eq></Where></Query>",
        completefunc: function (xData, Status) 
        {
            $(xData.responseXML).find("[nodeName=z:row]").each(function(){
                    san = $(this).attr("ows_Title");
                    sanid = $(this).attr("ows_ID");
            });
        }
    });

    if(san)
    {
        $("input[Title='Scorecard Application Name']").attr("value", san);
        $("input[id='SPScorecardApplicationName_Hidden']").attr("value", sanid);
    }
	
	

	
});</script>

 

Colin