Explore the Experience in SharePoint 2013

Tuesday, December 16, 2014

How to Hide Controls in SharePoint New Form Based on User using ECMA Script.

No comments

Hi Guys,

Thanks  for visiting our blog.

In Share Point we have If condition "If this user is member of this share point group" We can use this condition to check any user belongs to particular group or not.


Scenario:
Check user belongs to a SharePoint Group and Hide some controls in New/Edit Forms on SharePoint Custom List.

                        

Solution:
The following Script which will help you in checking if the current logged in SharePoint user belongs to a SharePoint user group and based on it hide some controls in New/Edit  forms on Custom List.

Step 1: Navigate to your SharePoint 2013 site.

Step 2: From this page select Site Actions | Edit Page:

Edit the page, go to the "Insert" tab in the Ribbon and click the "Web Part" option. In the "Web Parts" picker area, go to the "Media and Content" category, select the "Script Editor" Web Part and press the "Add button".

Step 3: Once the Web Part is inserted into the page, you will see an "EDIT SNIPPET" link; click it. You can insert the HTML and/or JavaScript as in the following:

Script

<script src="/sites/JohnHancock/JS/jquery-1.4.2.min.js"></script><script type="text/javascript">

ExecuteOrDelayUntilScriptLoaded(disableControls, "sp.js");

var clientContext = null;
        var web = null;
        var users ;
        var oList;
        var oListNew;

function disableControls()
{
 clientContext = new SP.ClientContext();
var groupCollection = clientContext.get_web().get_siteGroups();
var group = groupCollection.getById(4);//the SharePoint usergroup
users = group.get_users();
clientContext.load(group);
clientContext.load(users);
currentUser = clientContext.get_web().get_currentUser();
clientContext.load(currentUser);
clientContext.executeQueryAsync(Function.createDelegate(this,
this.onQuerySucceeded), Function.createDelegate(this,
this.onQueryFailed));
RefreshCommandUI();
}
function onQuerySucceeded()
{
if(this.users.get_count() >0)
{
var UserExistInGroup = false;
for(var i=0; i < users.get_count(); i++)
{
//alert(users.itemAt(i).get_loginName());
//alert(this.currentUser.get_loginName());

if(users.itemAt(i).get_loginName() == this.currentUser.get_loginName())
{
UserExistInGroup = true;
break;
}
}
}
if (UserExistInGroup)
{
$('nobr:contains("Approver")').closest('tr').show();
}
else
{
 $('nobr:contains("Approver")').closest('tr').hide();
}
}
function onQueryFailed(sender, args)
{

}
</script>
 


This is how we use to  hide /unhide the columns  in SharePoint Out of box forms.

No comments :

Post a Comment