vendredi 30 janvier 2015

Why does .get_title() called from an (SPOnline) App-Web return an incorrect Localized DisplayName?



Can someone please explain me where I go wrong with the code (all the way the bottom)


On the same non-english Subsite get_title() returns different results when run directly in the site compared to when run from an App.


Steps to reproduce:


Run code in a teamsite



  1. Create a non-english Locale subsite I used German in my example, its not my own language but can read it

  2. Run the script below in Chrome Snippets


This produces the results I expect, get_title() gets me the localized DisplayName



Run code from an App-web



  1. [install an (english¿) App with read rights in]

  2. Now open the App in that same subsite (now Host-web)

  3. Run the script below in Chrome Snippets the script is now accessing the subsite through the App-web


Now get_title() returns me the DisplayName as if the site was an English Locale; extracting the DisplayName from the SchemaXml clearly shows the correct DisplayName.


It looks to me like get_title() returns the Locale DisplayName of the App Web instead of the Host-Web.. (I have not tested what happens with an App which is not in english.. couldn't find one)



Test code


Code to be run as Chrome Snippets (or any way you want it)


try-catch is used to try App code first; if it fails it defaults to the current ClientContext



var listName='Tasks';
var reportDIV=document.getElementById('DeltaPlaceHolderMain');
var listTitle='<h1>'+listName+'</h1>';
reportDIV.innerHTML=listTitle;
var context,web;
JSRequest.EnsureSetup();
try{
context = new SP.ClientContext(decodeURIComponent(JSRequest.QueryString["SPAppWebUrl"]));
var appContextSite = new SP.AppContextSite(context, decodeURIComponent(JSRequest.QueryString["SPHostUrl"]));
web = appContextSite.get_web();
} catch(e){
context = new SP.ClientContext.get_current();
web=context.get_web();
}
listTitle+='<h2>context.get_url() = '+context.get_url()+'</h2>';
var fields=web.get_lists().getByTitle( listName ).get_fields();
context.load(fields);
context.executeQueryAsync(
function(){
var fieldEnumerator = fields.getEnumerator();
var rows=0,table='<tr><td><b>get_internalName()</b></td><td><b>get_title()</b></td><td><b>DisplayName from get_schemaXml()</b></td></tr>';
while (fieldEnumerator.moveNext()) {
var field=fieldEnumerator.get_current();
var DisplayName = field.get_schemaXml().split('DisplayName="')[1].split('"')[0];
var Title=field.get_title();
Title = DisplayName!=Title ? '<b style="color:red">'+Title+'</b>' : Title;
var cols = []; cols.push( field.get_internalName() , Title , DisplayName );
table+='<tr style="background-color:#E0E7F'+(rows++%2?1:10)+'"><td>'+cols.join('</td><td>')+'</td></tr>';
}
reportDIV.innerHTML=listTitle+'<table width=100%>'+table+'</table>';
}
,
function( sender,args){ alert(args.get_message())}
);







0 commentaires:

Enregistrer un commentaire