SharePoint Online 开发篇:App Part替代Content web part

网友投稿 269 2022-10-03

SharePoint Online 开发篇:App Part替代Content web part

Blog链接:Online中开发webpart并不是很随意,已经不能像On-Permise环境中可以随意开发visual webpart了,我们需要根据具体的功能进行具体的分析。

比如:Visual Studio在App.js中创建一个带有hello-worldish脚本的应用程序。该脚本检索当前用户的显示名,并将其放入默认App页面default .aspx的中。

Sample Code:

'use strict'; var context = SP.ClientContext.get_current();var user = context.get_web().get_currentUser(); // 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 informationfunction 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 namefunction onGetUserNameSuccess() $('#message').text('Hello ' + user.get_title());} // This function is executed if the above call failsfunction onGetUserNameFail(sender, args) { alert('Failed to get user name. Error:' + args.get_message());

为了提高效率,首先我们需要分析某个区域的信息显示能否通过SharePoint的OOB的Webpart来实现,而不是需要每个Webpart都要自己开发。如果确实OOB的Webpart无法实现,我们在考虑使用App Part来实现。

对于app part上面中需要的js和css尽量使用CDN的方式引入,因为如果一个app包中包含多个app part并且在同一个页面上添加这些app part,会导致请求过多有些app site中css和js无法加载。使用CDN的方式能减少部分请求,也可以避免部分这样的问题。

比如SharePoint Host的App Part 借助SharePoint REST API 或者JSOM来获取数据,并且画出我们想要的效果,但是这种方式我们过于依赖于SharePoint站点,也就是我们需要用到的css和js都在SharePoint站点中存在,如果我们把这个Webpart移植到另一个站点中,这个站点也必须部署相应的js和css文件,依赖性比较强。这里我们要求使用SharePoint Host App的app part来代替,因为app part实际上是一个Iframe,我们开发的App part 中使用到的js和css都是独立存在app site中的,这样便于移植。

希望本文总结对大家日后开发有所帮助。

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:SharePoint Online 开发篇:SharePoint Hosted Apps获取用户ID
下一篇:springboot vue接口测试前后端树节点编辑删除功能
相关文章

 发表评论

暂时没有评论,来抢沙发吧~