Live nutty. Just occasionally. Just once in a while. . . And see what happens. It brightens up the day! – Leo Buscaglia
Monthly Archives: November 2010
“Many persons have an idea that one cannot be in business and lead an upright life, whereas the truth is that no one succeeds in business to any great extent, who misleads or misrepresents”
John Wanamaker
They say that nobody is perfect. Then they tell you practice makes perfect. I wish they’d make up their minds.
— Wilt Chamberlain
“Good teams become great ones when the members trust each other enough to surrender the Me for the We.” – Phil Jackson
All mankind is divided into three classes: those that are immovable, those that are movable, and those that move.
Benjamin Franklin
Some people dream of success… while others wake up and work hard at it. ~Author Unknown
If you want to make enemies, try to change something. ~Woodrow Wilson
Holding anger is a poison. It eats you from inside. We think that hating is a weapon that attacks the person who harmed us. But hatred is a curved blade. And the harms we do, we do to ourselves
-Mitch Albom
SharePointクロスサイトのリスト表示をJQueryで行う
いきなりSharePointの話題なのであるが、同時にかなりマニアックである。
仕事場では4年前からSharePointを使っており自分の部署のアドミンなどをなぜか押し付けられ、とはいうもののねっこのところはしっかりITのかたがたが管理しているわけで、仏様の手のひらにのった孫悟空のようなカスタマイズしかできないわけではあるが、それはさておき
SharePointの不満のひとつにクロスサイトで使っているリストパーツを表示できない、というのがある。 同じサイトコレクションの下であればリストの共有もできる、ということだが、我がITチームはそのようには設定してくれなかった。 もちろん、それなりの理由が4年前にはあったわけで愚痴は言うまい(←言ってる).
Christophe Humbert氏が主催している Path to SharePoint ではこれに対する 解決法が載っている. 彼のサイトにある online tools のひとつが吐き出すJavaScriptを CEWPに落とし込んであげるだけで、他のサイトからのリストやカレンダーの表示が可能になる。 このスクリプトは JQueryを使ってターゲットページのリストを抜き出し、中身をCEWPのあるところに表示するような仕掛けになっている。
この “Cross site list Snapshot tool” 、使ってみると快適に作動する。 ただし、この方法で抜き出されたリストはそのままではTextToHTML script が使えない。
この TextToHTML というのもChristopheさんの作なのだが、リスト表示の中身を計算式HTMLを生成したコラムをスクリプトを使って処理してカラーテキストや背景色を変えたり、果てはバーグラフやKPIに使うような赤・青・黄の信号灯を表示したりそのほかにも色々できる便利ものである。 もし、SharePointを使っていて、まだ見たことがなければ百聞は一見にしかず、 これとか,これ, あるいは こんな表示もできる、ということで、SharePointの表現力が一気に高まる。 同じ作者の Easy Tabs なども使ってサイトの設計を行えば、苦労せずに同僚から尊敬のまなざしで見られること間違いなしである。
ではなぜ、この二つのツールが組み合わせではうまくうごかないかというと TextToHTML はページがロードしている最中に実行されてしまうが、そのときにはまだ Cross site list snapshot のスクリプトは他のページを読みこんでいる最中でリストの表示が間に合わないのである。
やはりおなじようなことを彼のブログで質問した人がいて、 Christopheさんが言うにはJQueryのcallback functionを使えば動くはず、ということであった。.
つまりJQuerryの load function に textToHTML scriptをコールバックさせて、順序正しくスクリプトを実行させればよいわけ。
早速試してみて、うまくいったので、ここに書いておく。 2010年の11月1日時点でPathToSharePointサイトにおいてあるスクリプトで作動を確認済み。
まずは TextToHTML のほうを修正する。. このスクリプトをChristopheさんのサイトからダウンロードしたら、テキストエディターを使って以下の部分を修正する。 (ここでは “light version”を使用)
// Calendar views var regexpA = new RegExp("\\s*<([a-zA-Z]*)(.|\\s)*/\\1?>\\s*"); TextToHTML(document.getElementsByTagName("a"),regexpA); // List views var regexpTD = new RegExp("^\\s*<([a-zA-Z]*)(.|\\s)*/\\1?>\\s*$"); TextToHTML(document.getElementsByTagName("TD"),regexpTD);
上の部分を下のように修正する。基本的には実行コードをファンクションとして定義する。
// Start of modifications //Calendar views
function calendarT2H() { var regexpA = new RegExp("\\s*<([a-zA-Z]*)(.|\\s)*/\\1?>\\s*"); TextToHTML(document.getElementsByTagName("a"),regexpA); } // List views
function listT2H(){ var regexpTD = new RegExp("^\\s*<([a-zA-Z]*)(.|\\s)*/\\1?>\\s*$"); TextToHTML(document.getElementsByTagName("TD"),regexpTD);
}
//maintains the original functionality of the script.
// Omit below two lines if no other part of the page uses TexttoHtML
calendarT2H(); listT2H();
//end of modifications
で、この修正されたスクリプトを TextToHTMLとして保存
次に Path To SharePoint’s Tool のCross-Site-List-Snapshotページ を使って、自分の使いたいリストのスクリプトを作成。
このスクリプトをtext editorで開き、以下の文字列を探す。
..(“href”,$(this).attr(“href”)+s+”Source=”+location.href);});});};LoadView…
ここの
“location.href);});});};”
この部分を
“location.href);},listT2H());});};”
このように変更してコールバックを追加。 [ listT2H() はリスト表示の場合。カレンダーの場合は calendarT2H() に変更.]
[おまけ]JQuery load() 関数の使いかた。
Load (url,parameters,callback)
ここで url は参照されるURL, parameters は Javascript object で、サーバーにおくる値を プロパティーとして保持。callback はAjaxが終了した時点でjQueryが呼び出す関数。SnapshotのコードではurlがターゲットリストのあるページのURL. parametersはクリストフさんが処方したマジックパラメーターで、これでリスト成分をぬきだしている。ただし、オリジナルスクリプトではコールバックが空白になっている。 (実は昔JQueryの参考書を読んだときに、このCallback機能の具体的な使い方がわからなかったのだが今回の経験で必要性を実感した次第)
このスクリプトをセーブする.
われわれの環境はSharePoint2007で、一番綺麗にCEWPを使ってスクリプトを実行させる方法は、ソースとして書き込むのではなく、スクリプトファイルをリンクすることだと思っているので、サイト内に “webpart”というドキュメントライブラリーを作成し、ここに二つのスクリプトファイルをアップロードする。
リストを表示したい Web Part ZoneにはふたつのCEWPを埋め込むことになる。
WepPart 1 – CEWP linked to ../webpart/CrossSiteListSnapshot-modified.txt
WebPart 2 – CEWP (Hidden) linked to ../webpart/TexttoHTML-modified.txt
これで、他のサイトのリストやカレンダーが色つきカラーバー付きで表示されるようになる。
めでたしめでたし!
Showing a list from another SharePoint Site
Showing a list from another SharePoint site (or cross-site listing)
One of the frustration about developing SharePoint page is that you can not use a list view from another Site. If two sites are under the same Site collection, then it will be possible but our IT people created each division’s site as independent site and not as a site collection (and there was a good reason for doing this way, 4 years ago. I am aware of that too.)
Christophe Humbert from Path to SharePoint has a solution to this. One of his online tools will generate a JavaScript snippet that you can drop into CEWP to show a list or calendar from another site page. He uses JQuery to extract a list out of the target page and insert where the CEWP is located.
This “Cross site list Snapshot tool” works pretty well. However, the list “extracted” in this manner can not take advantage of TextToHTML script.
The TextToHTML is another gem from Christophe that uses calculated columns to display colored text, background or even bar charts and also traffic signals (as KPI). If you have not seen his solution, take a look at this,this and this. Along with Easy Tabs (another Christophe’s creation) my colleagues in the office think I am a SharePoint wizard (far from the truth)
The reason the combination does not work is because TextToHTML will execute as soon as the browser read the script. Unfortunately at that time, the Cross site list snapshot script is still busy working on extracting the list so TexttoHTML does not see the list.
In his response to one of the comments in his blog, Christophe mentioned a use of callback function that is available for JQuery so that two scripts can talk to each other.
The idea is for the JQuerry load function to “call back” textToHTML script once the load function finishes its primary job.
I have experimented with this concept and came up with (my) solution so I am presenting it here. This works with scripts available from PathToSharePoint site as of November 1, 2010
First we have to modify the TextToHTML script. Download the script from Christophe’s site and look for the following section of the code. (I use “light version” of the script)
// Calendar views var regexpA = new RegExp("\\s*<([a-zA-Z]*)(.|\\s)*/\\1?>\\s*"); TextToHTML(document.getElementsByTagName("a"),regexpA); // List views var regexpTD = new RegExp("^\\s*<([a-zA-Z]*)(.|\\s)*/\\1?>\\s*$"); TextToHTML(document.getElementsByTagName("TD"),regexpTD);
Change the code above as follows. In essense, we are turning the executable codes to function declarations.
// Start of modifications //Calendar views
function calendarT2H() { var regexpA = new RegExp("\\s*<([a-zA-Z]*)(.|\\s)*/\\1?>\\s*"); TextToHTML(document.getElementsByTagName("a"),regexpA); } // List views
function listT2H(){ var regexpTD = new RegExp("^\\s*<([a-zA-Z]*)(.|\\s)*/\\1?>\\s*$"); TextToHTML(document.getElementsByTagName("TD"),regexpTD);
}
//maintains the original functionality of the script.
// Omit below two lines if no other part of the page uses TexttoHtML
calendarT2H(); listT2H();
//end of modifications
Save the above script as your TextToHTML.
Now generate your list snapshot script using Path To SharePoint’s Tool -Cross-Site-List-Snapshot page
Open the Script with text editor. Search for the following string toward the end of the script.
..(“href”,$(this).attr(“href”)+s+”Source=”+location.href);});});};LoadView…
Change
“location.href);});});};”
to
“location.href);},listT2H());});};”
What I did was to add a callback function parameter to JQuery’s load() function. [Replace listT2H() with calendarT2H() if the target is calendar.]
The JQuery load() function works like this
Load (url,parameters,callback)
Here, url is the URL of the resource being fetched, parameters is a Javascript object whose properties hold values to send to the server, and callback is a callback function that jQuery will call when the Ajax operation is complete. In the snapshot code, url is our list page url, parameters is a magic that Christophe is passing on to modify the list output. However,
as supplied by Christophe’s code generator, There is no callback.
Save the Script text.
I am using SharePoint2007 and I found the cleanest way to use CEWP for scripting is to link a file to CEWP, rather than entering source code directly in CEWP. I create “webpart” document library and upload both text files to the library.
So, in the above set up, A Web Part Zone has two webparts. Both are CEWP.
WepPart 1 – CEWP linked to ../webpart/CrossSiteListSnapshot-modified.txt
WebPart 2 – CEWP (Hidden) linked to ../webpart/TexttoHTML-modified.txt
The page now shows a list from another site with calculated columns correctly rendered.