javaScript non si attiverà a meno che gli utenti non eseguano un aggiornamento forzato (ctrl F5) per la pagina
-
29-09-2020 - |
Domanda
Ho aggiunto il seguente script al mio layout di pagina EnterpriseWiki.aspx all'interno di una raccolta di siti wiki aziendale: -
<script type="text/javascript">
_spBodyOnLoadFunctions.push(function(){
ExecuteOrDelayUntilScriptLoaded(showStatusAlert, "sp.js");
})
function showStatusAlert(){
var ctx = SP.ClientContext.get_current();
var web = ctx.get_web();
var list = web.get_lists().getById(_spPageContextInfo.pageListId);
var item = list.getItemById(_spPageContextInfo.pageItemId);
ctx.load(list);
ctx.load(item);
ctx.executeQueryAsync(
function(){
var status = item.get_item('_ModerationStatus');
// 2 - rejected
if(status == 1){
statusDesc = SP.UI.Status.addStatus("Warning, this page has been rejected, Please view the page history to check for the latest approved version.!");
SP.UI.Status.setStatusPriColor(statusDesc, 'red');
}
}
, function(err){
});
}
</script>
</asp:Content>
lo script verificherà se lo stato della pagina è stato rifiutato e visualizzerà un avviso rosso.ora quando l'utente fa clic sul collegamento della pagina non vedrà l'avviso rosso, come segue:-
ma quando aggiorna la pagina sarà in grado di vedere l'avviso come segue:-
qualcuno può consigliare cosa sta causando la mancata esecuzione dello script al caricamento della prima pagina?
Soluzione
Penso che il problema sia il valore che il tuo recupero non sta ottenendo al momento giusto la prima volta.Ecco perché nel secondo postback otterrebbe il valore dell'oggetto corretto e verrà caricato correttamente nel contesto.hai provato a chiamare il metodo dall'evento di caricamento della pagina?quindi in fondo al codice fai semplicemente showStatusAlert();
.
o
utilizzare invece questo metodo, carica l'ID dell'elemento dell'elenco al caricamento del contesto
http://hameersaleem.blogspot.co.uk/2012/09/getting-page-field-value-in-custom.html
quindi sembrerebbe qualcosa sulla falsariga di:
<script type="text/javascript">
var pageItem; //variable for list item of page being displayed (which uses this page layout)
var pageFieldNameVar = '_ModerationStatus'; //variable for page field to be retrieved
var context; // This variable will contain SharePoint Client Context
function showStatusAlert(pageFieldName){
//this is the part that you need that you dont have in reloading the page item on first initial load
if (pageItem == null)
setPageContext();
//explicitly requesting to load the field Name for the page item
context.load(pageItem, pageFieldName);
//making the actual request
context.executeQueryAsync(Function.createDelegate(this, function () { onPageFieldSuccess(pageFieldName); }), Function.createDelegate(this, function () { onPageFieldFailed(pageFieldName); }));
}
//sets the pageItem global variable to establish the context for current page
function setPageContext() {
//_spPageContextInfo is defined in every SharePoint page and has pageListId and pageItemId
//properties populated in publishing pages
var pageListId = _spPageContextInfo.pageListId;
var pageItemId = _spPageContextInfo.pageItemId;
var ctx = SP.ClientContext.get_current();
var web = ctx.get_web();
//getting the list item for the current page
var webLists = web.get_lists();
var pageList = webLists.getById(pageListId);
pageItem = pageList.getItemById(pageItemId);
}
//this function is called automatically when request made by getPageField function is successful
function onPageFieldSuccess(pageFieldName) {
//retreiving the page field value from page list item
var fieldVal = pageItem.get_item(pageFieldName);
// 2 - rejected
if(fieldVal == 1){
statusDesc = SP.UI.Status.addStatus("Warning, this page has been rejected, Please view the page history to check for the latest approved version.!");
SP.UI.Status.setStatusPriColor(statusDesc, 'red');
}
}
//this function is called automatically when request made by getPageField function is unsuccessful
function onPageFieldFailed(pageFieldNameVar) {
//do something to display error
}
//calling only getPageField function. This will eventually call other functions eventually rendering the links
_spBodyOnLoadFunctions.push(function(){
ExecuteOrDelayUntilScriptLoaded(showStatusAlert(pageFieldNameVar)
, "sp.js");
})
</script>
</asp:Content>