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');

“Failed to get value of the “Attachments” column from the “Attachments” field type control. See details in log. Exception message: Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)”

The attachment issue comes after customization of NewForm.aspx/EditForm.aspx page. After Customizing page even you have not attached any attachement the following error message showing on click of save button:-
“Failed to get value of the “Attachments” column from the “Attachments” field type control. See details in log. Exception message: Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)”

Resolution:-
Disabled attachment option from list
Go to list --> settings --> advance settings ---> disable attachments.

OR

If you want the attachment column to be present, open page in sharepoint designer and findout the below tag and replace it....

<TemplateName xmlns="http://schemas.microsoft.com/WebPart/v2/ListForm">ListForm</TemplateName>

To

<TemplateName xmlns="http://schemas.microsoft.com/WebPart/v2/ListForm">ListForm2</TemplateName>

Friday, July 5, 2013

SharePoint Lookup field: - Behaves differently when items greater than 20

SharePoint Lookup field: - Behaves differently when items greater than 20
Lookup column having less than 20 items:
If your lookup column having 20 or less items, the lookup column rendered like a normal dropdown filed (i.e. if you checked the source code of the page, you will get HTML tag <Select title=”File ID”…..) as shown in below images.
1.       The below image showing no. of items less than 20
2.       The below image showing source code for the page

Lookup column having more than 20 items:
If your lookup column is having more than 20 items, the source code automatically changed and complicated in IE browser. In FireFox and other browsers the lookup column with more than 20 items still gets same HTML source i.e. <Select tag .  The images are shown in below:
1.       The below image showing no. of items greater than 20
2.       The below image showing source code for the page
When lookup column has more than 20 items in IE it gets generated INPUT text box with an image. The input field on the other hand having lots of event handler like onfocusout, onkeypress, onkeydown. One of the attributes of input tag is also “Choices” which contains all the ids and values of lookup separated by “|” character. The image next to Input field has an “Onclick” event that triggers the select and selected value pass to the attribute “Value”.