Friday, March 2, 2012

Article 2: Lock control on Form (NewForm, EditForm...) use JQuery and ECMASCRIPT


Step 1: Create new List name is CallCenter include Field:
Status: Choice (Spending, Holding, Finish)
Agent: Person or Group
PhoneNumber: Single line of text
Step 2: Add some new Item
Step 3: Open Sharepoint Designer 2010 and choose List CallCenter
Step 4: Open File EditForm.aspx  and choose Advanced Mode
Step 5: Choose webpart Edit
Step 6: Code javascript using ECMAScript below tag close table
Step 7: Include script JQuery below
<script type="text/javascript" src="../../JqueryLibrary/jquery-1.4.2.js"></script>
Code javascript
<script type="text/javascript" src="../../JqueryLibrary/jquery-1.4.2.js"></script>
<script type="text/javascript">
ExecuteOrDelayUntilScriptLoaded(GetWebUserData, "sp.js");
        var context = null;
        var web = null;
        var currentUser = null;
                        //Get Current User
        function GetWebUserData() {     
            context = new SP.ClientContext.get_current();
            web = context.get_web();
            currentUser = web.get_currentUser();
            currentUser.retrieve();
            context.load(web);
            context.executeQueryAsync(Function.createDelegate(this, this.onSuccessMethod), Function.createDelegate(this, this.onFailureMethod));
        }
        function onSuccessMethod(sender, args) {
            var currentUser= web.get_currentUser();
            //alert("LoginName: "+currentUser.get_loginName()+ "DisplayName: "+currentUser.get_title());
            GetIDOfEmployeeFormListBaseOnLoginName(currentUser.get_loginName());
                                    // how to get employeeID user profile property?
        }
        function onFailureMethod(sender, args) {
            alert('Unable to find your user information.');
        }
        //Filter Item with Current user login
                        function GetIDOfEmployeeFormListBaseOnLoginName(loginName) {
                                var ctx = new SP.ClientContext.get_current();
                                var list = ctx.get_web().get_lists().getByTitle('Employees');
                                var query = new SP.CamlQuery();
                                query.set_viewXml('<View Scope=\'All Items\'><Query><Where><Eq><FieldRef Name=\'UserName\' /><Value Type=\'User\'>'+currentUser.get_title()+'</Value></Eq></Where></Query></View>');
                                selectedEmployee = list.getItems(query);
                                //Display 2 Field ID,UserName
                                ctx.load(selectedEmployee,"Include(ID,UserName)");
                                ctx.executeQueryAsync(getEmployeeWithQuerySuccess, getEmployeeWithQueryFailure);
                            }
        function getEmployeeWithQuerySuccess(sender, args) {
                                var listEnumerator = selectedEmployee.getEnumerator();
                                while (listEnumerator.moveNext()) {
                                    //alert(listEnumerator.get_current().get_item("ID"));
                                    if(web.get_currentUser().get_title()=="Do Quoc Hung")
                                    {
                                                            //Block Control                          
                                                            $("input[Title='PhoneNumber']").attr("disabled", true);
                                    }                                  
                                }
                            }
                        function getEmployeeWithQueryFailure(sender, args) {
                                alert('Failed to get list items. \nError: ' + args.get_message() + '\nStackTrace: ' + args.get_stackTrace());
                            }
</script>
Step 8: Open List CallCenter and Edit Item
Step 9: Appear Form EditForm.aspx, you see Title Login is Do Quoc Hung and condition is valid => bock control PhoneNumber
Step 10: Finish

0 comments:

Post a Comment