Hide some fields for SharePoint list item when creating, updating, viewing

Some times we don't want some fileds dispaly to end user when creating, updating, viewing SharePoint custom list item.

And this field we don't want to show is a cacalute column from other column, e.g. you want to get alias from person and group filed.

But in the default sharepoint cacalute column expression, you can't use the person and group filed.

Fortunately, when you input the person and group filed, there will be a hide filed to store the alias for the person or group.

So we can use the Javacript in the PreSaveAction function of the list form to get the alias and populate it into the hided alias filed, then the sharepoint list form will save it when you clicking saving.

1.  include the Jquery library before writing the follow code

 <script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.2.min.js"></script>

for how use Jquery in SharePoint 2010, please check http://blogs.msdn.com/b/yojoshi/archive/2010/06/17/using-jquery-with-sharepoint-2010.aspx

 

2. Hide the table row for the alias field

var aliasInput;
 $(document).ready(function () {    
               
         
       aliasInput = $("input[title='Alias']")[0]; 
       var parent_Alias = aliasInput.parentNode;
       var parent_TR_Alias =  getParentNode_TR(parent_Alias );
       
       parent_Alias.removeChild(aliasInput);
       if(parent_TR_Alias)
       {
           parent_TR_Alias.style.display = "none";
       }
       
       // aliasInput.style.display = "none";
       //  aliasInput.disabled = true;
    });

function getParentNode_TR(parent )
{
     while (parent && parent.tagName != 'TR') {
                    parent = parent.parentNode;
                }
     return parent;
}


3.  Get the alias value from person or group field and populate it to aliasInput.value

function PreSaveAction() {
 // Get the alias and save it to the alias column
    var aliasSpan = $(".ms-entity-resolved").filter(function () {
        return $(this)[0].tagName == "SPAN"
    })[0];  

   var aliasValue = aliasSpan.attributes["title"].nodeValue;
   aliasInput.value = aliasValue;
   aliasInput.disabled = false;
........

}


 

你可能感兴趣的:(Hide some fields for SharePoint list item when creating, updating, viewing)