‘use strict‘; var context = SP.ClientContext.get_current(); var user = context.get_web().get_currentUser(); var web; (function () { // This code runs when the DOM is ready and creates a context object which is // needed to use the SharePoint object model $(document).ready(function () { getUserName(); }); // This function prepares, loads, and then executes a SharePoint query to get // the current users information function getUserName() { context.load(user); context.executeQueryAsync(onGetUserNameSuccess, onGetUserNameFail); } // This function is executed if the above call is successful // It replaces the contents of the ‘message‘ element with the user name function onGetUserNameSuccess() { $(‘#message‘).text(‘Hello ‘ + user.get_title()); } // This function is executed if the above call fails function onGetUserNameFail(sender, args) { alert(‘Failed to get user name. Error:‘ + args.get_message()); } sharePointReady(); })(); var hostUrl; function getParameterByName(name) { name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]"); var regexS = "[\\?&]" + name + "=([^&#]*)"; alert(regexS); var regex = new RegExp(regexS); var results = regex.exec(window.location.search); if(results === null) return ""; else return decodeURIComponent(results[1].replace(/\+/g, " ")); } function sharePointReady(){ context=new SP.ClientContext.get_current(); web=context.get_web(); hostUrl=getParameterByName(‘SPHostUrl‘); alert(window.location.search+":"+hostUrl) //createList(); //createItems(); //updateListItem(); //getItems(); //deleteItems(); } function deleteItems(){ var listContext=new SP.AppContextSite(context,hostUrl); var list=listContext.get_web().get_lists().getByTitle(‘My Sample List‘); var listItem=list.getItemById(3); listItem.deleteObject(); //listItem.update();//列已经不存在,加了出错 context.executeQueryAsync(function(){ alert(‘Delete Succeful!‘) },function(sender,args){ alert(args.get_message()); }); } var items; function getItems(){ var listContext=new SP.AppContextSite(context,hostUrl); var list=listContext.get_web().get_lists().getByTitle(‘My Sample List‘); var camlQuery=new SP.CamlQuery(); var query="<View><Query><Where><Eq><FieldRef Name=‘Title‘/><Value Type=‘Text‘>创建5</Value></Eq></Where></Query><RowLimit>5</RowLimit></View>"; //var query="<View><ViewFields><FieldRef Name=‘Title‘ /></ViewFields><Query><Where><Eq><FieldRef Name=‘Title‘ /><Value Type=‘Text‘>创建5</Value></Eq></Where></Query></View>"; camlQuery.set_viewXml(query); items=list.getItems(camlQuery); context.load(items); context.executeQueryAsync(onGetListItemsSuccess,onFailed); } function onGetListItemsSuccess(){ var listItemEnumerator = items.getEnumerator(); while (listItemEnumerator.moveNext()) { var oListItem = listItemEnumerator.get_current(); alert(oListItem.get_item(‘Title‘)); } } function updateListItem(){ var listContext=new SP.AppContextSite(context,hostUrl); var list=listContext.get_web().get_lists().getByTitle(‘My Sample List‘); var listItem=list.getItemById(1); listItem.set_item(‘Title‘,‘我的第一次更新‘); listItem.update(); context.executeQueryAsync(onCreateListItemsSuccess,onFailed); } var newList; function createList(){//新建列表 var hostContext=new SP.AppContextSite(context,hostUrl); web=hostContext.get_web(); var newListInfo=new SP.ListCreationInformation(); newListInfo.set_title(‘My Sample List‘); newListInfo.set_templateType(SP.ListTemplateType.genericList); newList=web.get_lists().add(newListInfo); context.load(newList); context.executeQueryAsync(onCreateListSucceeded,onFailed); } function createItems(){//新建数据 var listContext=new SP.AppContextSite(context,hostUrl); var list=listContext.get_web().get_lists().getByTitle("My Sample List"); for(var i=0;i<10;i++){ var itemCreateInfo=new SP.ListItemCreationInformation(); var newListItem=list.addItem(itemCreateInfo); newListItem.set_item("Title",‘创建‘+i); newListItem.update(); context.load(newListItem); } context.executeQueryAsync(onCreateListItemsSuccess,onFailed); } function onCreateListItemsSuccess(){ alert(‘New ListItems created‘); } function onCreateListSucceeded(){ alert(‘New List create哈哈‘+newList.get_title()); } function onFailed(sender,args){ alert(‘Failed.Error‘+args.get_message()); }
时间: 2024-11-06 02:02:26