Wednesday, July 17, 2013

Query string value in lookup field in NewForm of list/ libraries.

If you created custom action on list and want to pass query string value in lookup field  and removed the other option from lookup so user cant change the value of lookup field used the follwoing javascript.

e.g. You've built a nice custom page which lists your parent items and you have links to each parent items to add child items with respect to parent item. Also when user open the child item form user not allow to select other parent id from lookup column add the following script on child item form.

your links looks like:-

http://ServerName/Sites/SiteName/Lists/Listname/NewForm.aspx?ItemId ={ItemId}.

// Javascript for Querystring values selected in Lookup dropdown and removed others values on Form.
//Description:  If Lookup column value items less than 20, html tag of dropdown 'select'. If the items greater than 20 items, html tag automaticaaly converted into 'input' in sharepoint 2010. So this script work for both condition.


//looks for the control of the requested type with a title that matches our current field
function getField(typeOfField,fieldDisplayTitle)
{  
   var matchingDocs = document.getElementsByTagName(typeOfField);  
   for (var i=0; i < matchingDocs.length; i++)
   {  
     if (matchingDocs[i].title == fieldDisplayTitle)
     {  
       return matchingDocs[i];  
     }  
   }  
   return false;  
}   
   
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, " "));
}
  
//Set query string value to lookup field when lookup items less than 20 and removed other values from dropdown.
function setSelectValue(lookupControl, valueToSet)
{
   for(var i = 0; i < lookupControl.length; i++)
   {
     if(lookupControl[i].value == valueToSet)
     {
       lookupControl[i].selected = true;
        //lookupControl.disabled=true;
     }
   }
   //to remove other option from the field
   var options = lookupControl.options;
   var iCount = 0;
   while (lookupControl.length > 1 )
   {
     if(options[iCount].selected != true)
     {
       options[iCount] = null;
     }
     else
     {
      iCount = iCount + 1
     }
   }
                   
}
  
//function to be registered on SP Body On Load to attempt to select the corresponding lookup field information
function attemptToSetField()
{
  var fieldNameToBeAutoSet = "File ID";  //This is the display name of the field you want to auto-set
  var queryStringParameterName = "ItemId";  //This is the query string key name you will be retrieving the ID from
  var idToBeSet = getParameterByName(queryStringParameterName);
  if(idToBeSet)//only run the auto-set if it's been passed to us via query string
  {
    var lookupControl = getField('select',fieldNameToBeAutoSet);
    if(lookupControl)
    {
      setSelectValue(lookupControl, idToBeSet);
    }
    else
    {
       lookupControl = getField('input', fieldNameToBeAutoSet);
       if(false === lookupControl)
       {
          alert('Unable to get dropdown named ' + fieldNameToBeAutoSet);

       }
       else
       {
          //Set query string value to lookup filed when lookup items more than 20 and removed other values from dropdown.
          lookupControl.setAttribute('choices',idToBeSet + '|' + idToBeSet);
          lookupControl.value = idToBeSet;
          lookupControl.selected = true;

       }
     
                    
    }
  }
}
  _spBodyOnLoadFunctionNames.push('attemptToSetField');

No comments:

Post a Comment