SharePoint Online 開發篇:App Part替代Content web part

Blog鏈接:https://blog.51cto.com/13969817

Webpart基本上是每個Portal項目中必不可少的,但是對於SharePoint 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 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());

爲了提高效率,首先我們需要分析某個區域的信息顯示能否通過SharePoint的OOB的Webpart來實現,而不是需要每個Webpart都要自己開發。如果確實OOB的Webpart無法實現,我們在考慮使用App Part來實現。

對於app part上面中需要的js和css儘量使用CDN的方式引入,因爲如果一個app包中包含多個app part並且在同一個頁面上添加這些app part,會導致請求過多有些app site中css和js無法加載。使用CDN的方式能減少部分請求,也可以避免部分這樣的問題。

爲了避免多個App Part使用一個App Site,如果同一頁面放了很多這樣的app part會導致對這個app site的請求資源過多,帶來意想不到的問題,最好每個app part單獨開發一個app.
儘量減少請求次數,對於不必須要的請求我們可以通過App part的屬性來替代,例如:一個完整功能的app part內部可能需要點擊item跳轉到view item頁面,那麼這個URL可以通過屬性來配置,而不是通過REST API單獨請求一次。

比如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中的,這樣便於移植。

希望本文總結對大家日後開發有所幫助。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章