Ravi Kumar

Ravi Kumar

  • 1.2k
  • 192
  • 20.3k

ECMA Script

Aug 17 2015 3:22 AM
Dear all,
 
I have scenario like to hide a SharePoint library column based on current logged in user
 
Steps  for conditions
1)  If user is creator of document then status column need to be hide.
2)  If document approved status then status column need to be hide .
3)  If user belongs to specific group based group need to hide status column
 
The below code for single hard coded users and hiding please help me out to achieve for group of users.
 Many thanks in advance.
 
 The working script for hard coded  users like "ceo", "snraccountant", "finassociate" etc is as in below script for your ref.
 
 
<script type="text/javascript">





function getUrlVars()
{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}</script><script language="ecmascript" type="text/ecmascript">




ExecuteOrDelayUntilScriptLoaded(getFieldUserValue, "sp.js")

        var listItem;
        var list;
        var clientContext;
        var currentUser ;
        var text;
         var resFin;
var resChief;
var resSnr;
         
        function getFieldUserValue() {
 
            this.clientContext = SP.ClientContext.get_current();
            if (this.clientContext != undefined && clientContext != null) {
                var webSite = clientContext.get_web();
                  
                currentUser = webSite .get_currentUser();
                this.list = webSite.get_lists().getByTitle("Payment");
                var itemId = parseInt(getUrlVars()["ID"]);

resFin= $("select[title='Finance Manager Approval Status']").val();
resChief= $("select[title='Chief Accountant Confirmation Status']").val();
resSnr= $("select[title='Senior Accountant Confirmation Status']").val();

                this.listItem = list.getItemById(itemId);
                clientContext.load(this.listItem);
                clientContext.load(currentUser);
                this.clientContext.executeQueryAsync(Function.createDelegate(this, this.OnLoadSuccess), Function.createDelegate(this, this.OnLoadFailed));       
            }
        }
 
        function OnLoadSuccess(sender, args) {
            var fieldUserValueCreatedBy = this.listItem.get_item("Author");
            var loginuser = this.listItem.get_item("Editor").value;
            
             var account = currentUser.get_loginName();
             var currentUserAccount = account.substring(account.indexOf("|") + 11); 
          
            if(fieldUserValueCreatedBy.get_lookupValue()==currentUserAccount)
            {
               $('td.ms-formlabel:contains("Chief Accountant Confirmation Status")').parent().hide();
               $('td.ms-formlabel:contains("Finance Manager Approval Status")').parent().hide();
                $('td.ms-formlabel:contains("Senior Accountant Confirmation Status")').parent().hide();
             }
      
if('snraccountant' == currentUserAccount )
            {
               $('td.ms-formlabel:contains("Chief Accountant Confirmation Status")').parent().hide();
               $('td.ms-formlabel:contains("Finance Manager Approval Status")').parent().hide();
               }

            if('financemanager'== currentUserAccount)
            {
               $('td.ms-formlabel:contains("Chief Accountant Confirmation Status")').parent().hide();
                $('td.ms-formlabel:contains("Senior Accountant Confirmation Status")').parent().hide();
               }

           if('finuser1'== currentUserAccount)
            {
              $('td.ms-formlabel:contains("Finance Manager Approval Status")').parent().hide();
               $('td.ms-formlabel:contains("Chief Accountant Confirmation Status")').parent().hide();
              $('td.ms-formlabel:contains("Senior Accountant Confirmation Status")').parent().hide();
               }
  
if('ceo' == currentUserAccount)
            {
              $('td.ms-formlabel:contains("Finance Manager Approval Status")').parent().hide();
               $('td.ms-formlabel:contains("Chief Accountant Confirmation Status")').parent().hide();
              $('td.ms-formlabel:contains("Senior Accountant Confirmation Status")').parent().hide();
               }
               if(resFin == "Approved")
{
$("nobr:contains('Finance Manager Approval Status')").parent('h3').parent('td').parent('tr').hide();
}

               if(resChief == "Approved")
{
$("nobr:contains('Chief Accountant Confirmation Status')").parent('h3').parent('td').parent('tr').hide();
}

               if(resSnr == "Approved")
{
$("nobr:contains('Senior Accountant Confirmation Status')").parent('h3').parent('td').parent('tr').hide();
}

}

        function OnLoadFailed(sender, args) {
            alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
        }

       </script>
 

Answers (4)