Javascript client sharepoint object model -- ECMA

function checkForEnter(event) { 
        if (event.keyCode == 13) { //按下回车
            var videocomments = $("#newcomments").val(); 
            if (videocomments != '') { 
                $("#divToDisplay").show("slow"); 
                $("#divToDisplay").html(''); 
                var addnewcomment; 
                getWebUserData(); 
                var videotitle = $("input[id$=txtVideoTitle]").val(); 
                var KvideoId = $("input[id$=txtVideoid]").val(); 
                var taskList = web.get_lists().getByTitle('Cosmo Video Comments'); 
                context.load(taskList); 
                var CommentItemInfo = new SP.ListItemCreationInformation(); 
                var query = new SP.CamlQuery(); 
                var queryxml = "<View><Query><Where><Eq><FieldRef Name='MediaID' /><Value Type='Text'>" + KvideoId + "</Value></Eq></Where></Query></View>"; 
                query.set_viewXml(queryxml); 
                var collItem = taskList.getItems(query); 
                context.load(collItem); 
                context.executeQueryAsync(onQuerySucceeded, onQueryFailed); 
 
                function onQuerySucceeded(sender, args) { 
                    var itemCount = collItem.get_count(); 
                    var listEnumerator = collItem.getEnumerator(); 
                    if (itemCount == 0) { 
 
                        var myDate = new Date(); 
                        var CommentItemInfo = new SP.ListItemCreationInformation(); 
                        addnewcomment = taskList.addItem(CommentItemInfo); 
                        addnewcomment.set_item('Title', videotitle); 
                        addnewcomment.set_item('MediaID', KvideoId); 
                        var month = myDate.getMonth() + 1; 
                        com = videocomments + "\n\r\n" + username + "\n\r\n" + month + '/' + myDate.getDate() + '/' + myDate.getFullYear() + '\r\n\r'; 
                        addnewcomment.set_item('Comments', com); 
                        $("#newcomments").val(''); 
                        addnewcomment.update(); 
                        context.load(addnewcomment); 
                        context.executeQueryAsync(addTaskSuccess, addTaskFailure); 
                        function addTaskSuccess(sender, args) { 
                            GetListData(); 
                        } 
                        function addTaskFailure(sender, args) { 
                            alert('Failed to add new task. \nError: ' + args.get_message() + '\nStackTrace: ' + args.get_stackTrace()); 
                        } 
                    } 
                    else { 
                        var myDate = new Date(); 
                        var month = myDate.getMonth() + 1; 
                        while (listEnumerator.moveNext()) { 
                            var oListItem = listEnumerator.get_current(); 
                            var com = oListItem.get_item('Comments').toString(); 
                            com = com + videocomments + "\n\r\n" + username + "\n\r\n" + month + '/' + myDate.getDate() + '/' + myDate.getFullYear() + '\r\n\r'; 
                            oListItem.set_item('Comments', com); 
                            $("#newcomments").val(''); 
                            oListItem.update(); 
                            context.load(oListItem); 
                            context.executeQueryAsync(updateTaskSuccess, updateTaskFailure); 
                            function updateTaskSuccess(sender, args) { 
                                GetListData(); 
                            } 
                            function updateTaskFailure(sender, args) { 
                                alert('Failed to update task. \nError: ' + args.get_message() + '\nStackTrace: ' + args.get_stackTrace()); 
                            } 
                        } 
                    } 
 
                } 
                function onQueryFailed(sender, args) { 
                    alert('Request failed' + args.get_message() + '\n' + arg.get_stackTrace()); 
                } 
            } 
            else { 
                alert('Please enter comments'); 
            } 
            return false; 
        } 
    } 

  

posted @ 2014-03-12 15:09  batter152  阅读(216)  评论(0编辑  收藏  举报