Copyright © 2015 NTT DATA INTRAMART CORPORATION
目次
1. 改訂情報 2. はじめに 2.1. 本書の目的 2.2. 対象読者 2.3. サンプルコードについて 2.4. 本書の構成 3. Office 365 連携 プログラミング概要 3.1. Office 365 連携 の流れ4. SharePoint Online のファイラを作成する (JavaEE開発モデル) 4.1. SharePoint Online 上にディレクトリを新規作成する 4.2. SharePoint Online 上にファイルをアップロードする 4.3. SharePoint Online 上のファイルをダウンロードする 4.4. SharePoint Online 上のファイルの詳細情報を表示する 4.5. SharePoint Online 上のファイルを削除する
4.6. SharePoint Online 上のドキュメントをiframeで表示する 4.7. SharePoint Online のファイラを作成する 5. SharePoint Online のファイラを作成する (スクリプト開発モデル) 5.1. SharePoint Online 上にディレクトリを新規作成する 5.2. SharePoint Online 上にファイルをアップロードする 5.3. SharePoint Online 上のファイルをダウンロードする 5.4. SharePoint Online 上のファイルの詳細情報を表示する 5.5. SharePoint Online 上のファイルを削除する
5.6. SharePoint Online 上のドキュメントをiframeで表示する 5.7. SharePoint Online のファイラを作成する
改訂情報
変更年月日 変更年月日 変更内容変更内容 2015-08-01 初版 2015-12-01 第2版 下記を追加・変更しました 「SharePoint Online のファイラを作成する (スクリプト開発モデル) 」を追加 2017-08-01 第3版 下記を変更しました。「SharePoint Online のファイラを作成する (JavaEE開発モデル) 」にOffice 365 連携 の低レベルAPIを新たに提供されたOneDrive APIに変更
はじめに
本書の目的
本書では intra-mart Accel Platform における Office 365 連携機能 を用いたプログラミング方法について説明します。
対象読者
次の開発者を対象としています。
intra-mart Accel Platform で Office 365 上のリソースを利用したアプリケーションを作成したい開発者
サンプルコードについて
本書に掲載されているサンプルコードは可読性を重視しており、性能面や保守性といった観点において必ずしも適切な実装では ありません。 サンプルコードを参考に開発する場合は、上記について十分に注意してください。 なお、ドキュメントの流れに合わせてサンプルを実装し、実際に動作確認を行う場合の前提条件は以下の通りです。 Office 365 連携 セットアップガイド を参考に Office 365 連携 の環境を作成してください。本書の構成
本書は以下のような構成です。 Office 365 連携 プログラミング概要SharePoint Online のファイラを作成する (JavaEE開発モデル) SharePoint Online のファイラを作成する (スクリプト開発モデル)
Office 365 連携 プログラミング概要
Office 365 連携 の流れ
Office 365 連携機能のAPIは、Office 365 のWebAPIをラップする形で提供しています。
Office 365 のWebAPIの実行に必要なアクセストークンは、 intra-mart Accel Platform (Office 365 連携)が OAuth2.0 の 仕様に則って、自動的に取得・更新します。
これにより開発者は OAuth2.0 関連の実装が不要となり、Office 365 連携機能の APIを利用したビジネスロジックの実装に集中 できます。
開発者は以下の図の 7 と 10 に 必要な処理や画面を実装することで、Office 365 連携機能を利用したプログラムを作成できま す。
Office 365 連携のフロー図連携のフロー図
1. ユーザが「個人設定」-「外部連携アプリケーション」画面で Office 365 連携 のプロバイダ を「許可」します。 2. intra-mart Accel Platform (Office 365 連携)は ユーザを Microsoft Azure Active Directory (以降、Microsoft
Azure AD とする)にリダイレクトさせます。
3. ユーザは Microsoft Azure AD にサインインを行い認証し、必要な場合は許諾を行います。
4. Microsoft Azure AD は intra-mart Accel Platform (Office 365 連携)の リダイレクトURI に認可コードを返します。 5. intra-mart Accel Platform (Office 365 連携API) は、Microsoft Azure AD に認可コードを提示し、アクセストーク
ンを要求します。
6. Microsoft Azure AD は、アクセストークンとリフレッシュトークンを返却します。(アクセストークンの有効期限が切れ た場合、リフレッシュトークンを利用し新しいアクセストークンに更新します。)
7. ユーザが SharePoint Online のファイル操作などの Office 365 連携機能のAPIを利用した画面をリクエストします。 8. intra-mart Accel Platform (Office 365 連携API)は、アクセストークンを利用し Office 365 の Web API を実行しま
す。
9. Office 365 の Web API はリクエストされたデータを返します。
コラム
Microsoft Azure AD のアクセストークンの取得フローについての詳細は以下のページを参照してください。 https://msdn.microsoft.com/en-US/library/azure/dn645542.aspx (English)
SharePoint Online のファイラを作成する (JavaEE開発モデル)
Office 365 連携機能 では、Office 365 の SharePoint Online のドキュメントにアクセスするための以下の API を提供しま す。
Office 365 のOneDrive REST APIをラップした低レベルAPI
jp.co.intra_mart.foundation.office365.api.v2_0.onedrive.operation.OneDriveOperation intra-mart Accel Platform の Storageインタフェースを継承した高レベルAPI
jp.co.intra_mart.foundation.office365.service.storage.SharePointStorage
ここでは、 Office 365 連携機能 の SharePointStorage APIを利用して実際にプログラムを作成する過程を説明します。 本項では、例として JavaEE開発モデル を利用しています。 また、サンプルコードは可読性を重視しているため、JSP内で全ての処理を行っています。 本項に記載のサンプルを組み合わせると 以下のような SharePoint Online のファイラが完成します。 SharePoint Online のファイラの完成イメージのファイラの完成イメージ
項目
SharePoint Online 上にディレクトリを新規作成する SharePoint Online 上にファイルをアップロードする SharePoint Online 上のファイルをダウンロードする SharePoint Online 上のファイルの詳細情報を表示する SharePoint Online 上のファイルを削除するSharePoint Online 上のドキュメントをiframeで表示する SharePoint Online のファイラを作成する
注意
Office 365 連携 のAPIの詳細については以下を参照してください。 「 APIドキュメント - SharePointStorage 」 「APIドキュメント - OneDriveOperation 」 Office 365 連携 のタグライブラリの詳細については以下を参照してください。 「 タグライブラリ - im-office365:excel 」 「 タグライブラリ - im-office365:powerPoint 」 「 タグライブラリ - im-office365:word 」SharePoint Online 上にディレクトリを新規作成する
SharePoint Online 上にディレクトリを新規作成する実装のサンプルです。 リクエストパラメータとして受け取った プロバイダID、ターゲットのディレクトリパス、ディレクトリの名称 を元に新規ディレ クトリを作成します。 実装例 実装例 SharePointStorage#makeDirectories() を利用します。 %CONTEXT_PATH%/sample/office365/filer/create_dir.jsp1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@ taglib prefix="imui" uri="http://www.intra-mart.co.jp/taglib/imui"%> <%@ page import="java.io.IOException"%>
<%@ page import="jp.co.intra_mart.common.platform.log.Logger"%> <%@ page import="jp.co.intra_mart.foundation.context.Contexts"%>
<%@ page import="jp.co.intra_mart.foundation.context.model.AccountContext"%>
<%@ page import="jp.co.intra_mart.foundation.office365.service.storage.SharePointStorage"%> <%
String userCd = Contexts.get(AccountContext.class).getUserCd();
String providerId = request.getParameter("providerId");
String currentDirPath =
request.getParameter("targetDirPath")!=null? request.getParameter("targetDirPath"):"/";
String createDirName =
request.getParameter("createDirName")!=null? request.getParameter("createDirName"):null;
String dirPath = currentDirPath + createDirName;
String successMsg =null;
String errorMsg =null;
if(createDirName ==null){
errorMsg ="ディレクトリ名が指定されていません。";
}else{
SharePointStorage storage =new SharePointStorage(userCd, providerId, dirPath);
try{
//ディレクトリの作成
boolean result = storage.makeDirectories();
if(result){ successMsg ="ディレクトリを作成しました。<br/><br/>"+ dirPath; }else{ errorMsg ="ディレクトリを作成できませんでした。<br/><br/>"+ dirPath; } }catch(IOException e){ errorMsg = buildErrorMessage("ディレクトリ作成時にエラーが発生しました。", e); } } %> <imui:head>
<title>Office365 - SharePointStorage API サンプル - ディレクトリ作成</title> <scripttype="text/javascript">
$(document).ready(function() { if(<%=successMsg !=null%>){
imuiShowSuccessMessage('<%=successMsg%>'); }
if(<%=errorMsg !=null%>){
imuiShowErrorMessage('<%=errorMsg%>'); } }); </script> </imui:head> <divclass="imui-title">
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
<spanclass="im-ui-icon-common-16-back"></span>一覧に戻る </a>
</li>
</ul>
</div> </div>
<form method="post"action="sample/office365/filer/index.jsp"id="backToIndex">
<inputtype="hidden"name="targetDirPath"value="<%=currentDirPath%>"/> </form>
<%!
privatestatic Logger logger = Logger.getLogger("sample.office365.filer.create_dir");
private String buildErrorMessage(String errorMessage, Exception errorDetail){
logger.error(errorDetail.getMessage(), errorDetail);
return errorMessage +"<br/><br/>"+ errorDetail.getMessage();
} %>
注意
Microsoft社が提供する Office 365 のWebAPIの動作仕様に起因し以下の制限があります。
SharePointStorage#makeDirectories() および OneDriveOperation#createFolderByPath() を利用しディレ クトリを作成する場合、ルートディレクトリ直下に存在するディレクトリ名と同名のディレクトリを任意のディレ クトリ配下に作成することはできません。
SharePoint Online 上にファイルをアップロードする
SharePoint Online 上にファイルをアップロードする実装のサンプルです。 マルチパートフォームデータとして受け取った プロバイダID、ターゲットのディレクトリパス、ファイルを元にファイルをアッ プロードします。 実装例 実装例 SharePointStorage#create() を利用します。 %CONTEXT_PATH%/sample/office365/filer/upload.jsp1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@ page import="java.io.InputStream"%>
<%@ page import="java.io.OutputStream"%>
<%@ page import="jp.co.intra_mart.foundation.context.Contexts"%>
<%@ page import="jp.co.intra_mart.foundation.context.model.AccountContext"%> <%@ page import="jp.co.intra_mart.common.aid.javaee.http.MultipartFormData"%> <%@ page import="jp.co.intra_mart.common.aid.javaee.http.MultipartFormData.Entity"%> <%@ page import="jp.co.intra_mart.foundation.office365.service.storage.SharePointStorage"%> <%@ page import="jp.co.intra_mart.common.aid.jdk.java.io.IOUtil"%>
<%
String userCd = Contexts.get(AccountContext.class).getUserCd();
//Web Application Server によってはレスポンスにテーマが含まれてしまうため notheme を指定
request.setAttribute("imui-theme-builder-module","notheme");
response.setContentType("json");
MultipartFormData data =new MultipartFormData(request);
String providerId = data.getEntity("providerId").getContent();
String targetDirPath =new String(data.getEntity("targetDirPath").getBytes(),"utf-8");
Entity localFileEntity = data.getEntity("local_file");
String fileName = localFileEntity.getFileName();
SharePointStorage storage =new SharePointStorage(userCd, providerId, targetDirPath +"/"+ fileName);
try(OutputStream outputStream = storage.create();
InputStream inputStream = localFileEntity.getInputStream()
){
IOUtil.transfer(inputStream, outputStream);
}
response.setContentType("json"); %>
[{
"name":"<%= localFileEntity.getFileName()%>", "size":<%= localFileEntity.getContentLength()%>, "error":"" }]
SharePoint Online 上のファイルをダウンロードする
SharePoint Online 上のファイルをダウンロードする実装のサンプルです。 リクエストパラメータとして受け取った プロバイダID、ターゲットのファイルパス、ファイルの名称を元にファイルをダウン ロードします。 実装例 実装例 SharePointStorage#open() を利用します。 %CONTEXT_PATH%/sample/office365/filer/download.jsp1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@ page import="java.io.InputStream"%>
<%@ page import="java.io.OutputStream"%> <%@ page import="javax.activation.FileTypeMap"%>
<%@ page import="jp.co.intra_mart.common.aid.jdk.java.io.IOUtil"%> <%@ page import="jp.co.intra_mart.foundation.context.Contexts"%>
<%@ page import="jp.co.intra_mart.foundation.context.model.AccountContext"%>
<%@ page import="jp.co.intra_mart.foundation.office365.service.storage.SharePointStorage"%> <%@ page import="jp.co.intra_mart.foundation.http.ResponseUtil"%>
<%
String userCd = Contexts.get(AccountContext.class).getUserCd();
String providerId = request.getParameter("providerId");
String targetFilePath = request.getParameter("targetFilePath");
String targetFileName = request.getParameter("targetFileName");
response.resetBuffer();
response.setCharacterEncoding(null);
String mimeType = FileTypeMap.getDefaultFileTypeMap().getContentType(targetFileName);
response.setContentType(mimeType);
String encodedFileName = ResponseUtil.encodeFileName(request,"UTF-8", targetFileName);
response.setHeader("Content-Disposition","attachment; "+ encodedFileName);
SharePointStorage sharePointStorage =new SharePointStorage(userCd, providerId, targetFilePath);
try(InputStream inputStream = sharePointStorage.open();
OutputStream outputStream = response.getOutputStream();
){
IOUtil.transfer(inputStream, outputStream);
} %>
SharePoint Online 上のファイルの詳細情報を表示する
SharePoint Online 上のファイルの詳細情報を表示する実装のサンプルです。 リクエストパラメータとして受け取った プロバイダID、ターゲットのファイルパスをファイルの詳細情報を表示します。 実装例 実装例 %CONTEXT_PATH%/sample/office365/filer/show_detail.jsp1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@ taglib prefix="imui" uri="http://www.intra-mart.co.jp/taglib/imui"%>
<%@ taglib prefix="imtag" uri="http://www.intra-mart.co.jp/taglib/core/standard"%> <%@ taglib prefix="i18n" uri="http://www.intra-mart.co.jp/taglib/im-i18n"%> <%@ page import="jp.co.intra_mart.foundation.context.Contexts"%>
<%@ page import="jp.co.intra_mart.foundation.context.model.AccountContext"%>
<%@ page import="jp.co.intra_mart.foundation.office365.service.storage.SharePointStorage"%> <%
String userCd = Contexts.get(AccountContext.class).getUserCd();
String providerId = request.getParameter("providerId");
String targetPathForShowDetail = request.getParameter("targetPathForShowDetail");
String errorMsg ="";
if(targetPathForShowDetail ==null){
errorMsg ="ファイル、または、ディレクトリが指定されていません。";
}
SharePointStorage storage =new SharePointStorage(userCd, providerId, targetPathForShowDetail); %>
<imui:head>
<title>Office365 - SharePointStorage API サンプル - 詳細表示</title> <scripttype="text/javascript">
$(document).ready(function() { if(<%= errorMsg !=null%>){
imuiShowErrorMessage('<%= errorMsg %>'); } }); </script> </imui:head> <divclass="imui-title">
<imtag:conditionvalidity="<%= Boolean.toString(errorMsg.isEmpty())%>">
<h1><%=storage.getCanonicalPath()%></h1> </imtag:condition> </div> <divclass="imui-form-container-narrow"> <divclass="imui-chapter-title"> <h2>詳細情報</h2> </div>
<imtag:conditionvalidity="<%= Boolean.toString(errorMsg.isEmpty())%>">
<tableclass="imui-table"> <tr> <thclass="wd-335px">正規化したパス</th> <td><%=storage.getCanonicalPath()%></td> </tr> <tr> <th>コンテンツをブラウザ上で確認可能なWeb URL</th> <td>
<ahref="<%=storage.getWebUrl()%>"><%=java.net.URLDecoder.decode(storage.getWebUrl(), "UTF-8")%></a>
</td>
</tr>
<tr>
<th>作成時刻</th>
<td><i18n:dateTimevalue="<%=new java.util.Date(storage.created())%>"pattern="yyyy/MM/dd hh:mm:ss"/></td> </tr> <tr> <th>作成ユーザID</th> <td><%=storage.createdUserId()%></td> </tr> <tr>
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 <tr> <th>作成ユーザ名</th> <td><%=storage.createdUserName()%></td> </tr> <tr> <th>最終更新時刻</th>
<td><i18n:dateTimevalue="<%=new java.util.Date(storage.lastModified())%>" pattern="yyyy/MM/dd hh:mm:ss"/></td> </tr> <tr> <th>最終更新ユーザID</th> <td><%=storage.lastModifiedUserId()%></td> </tr> <tr> <th>最終更新ユーザ名</th> <td><%=storage.lastModifiedUserName()%></td> </tr> <tr> <th>ファイルサイズ</th> <td><%=storage.length()%>バイト</td> </tr> </table> </imtag:condition> </div>
SharePoint Online 上のファイルを削除する
SharePoint Online 上のファイルを削除する実装のサンプルです。 リクエストパラメータとして受け取った プロバイダID、ターゲットのファイルパスを元にファイルを削除します。 実装例 実装例 SharePointStorage#remove() を利用します。 %CONTEXT_PATH%/sample/office365/filer/remove.jsp1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@ taglib prefix="imui" uri="http://www.intra-mart.co.jp/taglib/imui"%> <%@ page import="java.io.IOException"%>
<%@ page import="jp.co.intra_mart.common.platform.log.Logger"%> <%@ page import="jp.co.intra_mart.foundation.context.Contexts"%>
<%@ page import="jp.co.intra_mart.foundation.context.model.AccountContext"%>
<%@ page import="jp.co.intra_mart.foundation.office365.service.storage.SharePointStorage"%> <%
String userCd = Contexts.get(AccountContext.class).getUserCd();
String providerId = request.getParameter("providerId");
String targetPath = request.getParameter("targetPathForRemove");
String currentDirPath =
request.getParameter("targetDirPath")!=null? request.getParameter("targetDirPath"):"/";
String successMsg =null;
String errorMsg =null;
if(targetPath ==null){
errorMsg ="ファイル、または、ディレクトリが指定されていません。";
}else{
SharePointStorage storage =new SharePointStorage(userCd, providerId, targetPath);
try{
//ファイルまたはディレクトリの削除
boolean result = storage.remove(true);
if(result){ successMsg ="「"+ targetPath +"」を削除しました。"; }else{ errorMsg ="「"+ targetPath +"」を削除できませんでした。"; } }catch(IOException e){ errorMsg = buildErrorMessage("削除時にエラーが発生しました。", e); } } %> <imui:head>
<title>Office365 - SharePointStorage API サンプル - 削除</title> <scripttype="text/javascript">
$(document).ready(function() { if(<%=successMsg !=null%>){
imuiShowSuccessMessage('<%=successMsg%>'); }
if(<%=errorMsg !=null%>){
imuiShowErrorMessage('<%=errorMsg%>'); } }); </script> </imui:head> <divclass="imui-title"> <h1><%=targetPath%></h1> </div> <divclass="imui-toolbar-wrap">
66 67 68 69 70 71 72 73 74 75 76 77 78 79 </div> </div>
<form method="post"action="sample/office365/filer/index.jsp" id="backToIndex">
<inputtype="hidden"name="targetDirPath"value="<%=currentDirPath%>"/> </form>
<%!
privatestatic Logger logger = Logger.getLogger("sample.office365.filer.remove");
private String buildErrorMessage(String errorMessage, Exception errorDetail){
logger.error(errorDetail.getMessage(), errorDetail);
return errorMessage +"<br/><br/>"+ errorDetail.getMessage();
} %>
SharePoint Online 上のドキュメントをiframeで表示する
SharePoint Online 上のドキュメントをiframeで表示する実装のサンプルです。
リクエストパラメータとして受け取った プロバイダID、ターゲットのファイルパスを元にドキュメントをiframeで表示します。 実装例
実装例
im-office365 タグを利用します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@ taglib prefix="imtag" uri="http://www.intra-mart.co.jp/taglib/core/standard"%> <%@ taglib prefix="im-office365" uri="http://www.intra-mart.co.jp/taglib/im-office365"%> <%
String width ="640";
String height ="480";
String providerId = request.getParameter("providerId");
String targetPath = request.getParameter("targetPathForShowIframe");
String errorMsg =null;
if(targetPath ==null){
errorMsg ="ファイルが指定されていません。";
} %>
<imui:head>
<title>Office365 - iframe 表示</title> <scripttype="text/javascript">
$(document).ready(function() { if(<%=errorMsg !=null%>){
imuiShowErrorMessage('<%=errorMsg%>'); } }); </script> </imui:head> <divclass="imui-title"> <h1><%=targetPath%></h1> </div> <divid="imui-container-inner">
<imtag:conditionvalidity="<%=isWord(targetPath)%>">
<im-office365:wordproviderId="<%=providerId%>"path="<%=targetPath%>"
width="<%=width%>"height="<%=height%>"/>
</imtag:condition>
<imtag:conditionvalidity="<%=isExcel(targetPath)%>">
<im-office365:excelproviderId="<%=providerId%>"path="<%=targetPath%>"
width="<%=width%>"height="<%=height%>"/>
</imtag:condition>
<imtag:conditionvalidity="<%=isPowerPoint(targetPath)%>">
<im-office365:powerPointproviderId="<%=providerId%>"path="<%=targetPath%>"
width="<%=width%>"height="<%=height%>"/>
</imtag:condition> </div>
<%!
private String isWord(final String fileName){
return".docx".equals(getExtension(fileName))?"true":"false";
}
private String isExcel(final String fileName){
return".xlsx".equals(getExtension(fileName))?"true":"false";
}
private String isPowerPoint(final String fileName){
return".pptx".equals(getExtension(fileName))?"true":"false";
}
private String getExtension(String fileName){
finalint indexOfLastDot = fileName.lastIndexOf(".");
if(indexOfLastDot ==-1){
returnnull;
}
66 67 68
return fileName.substring(indexOfLastDot);
} %>
注意
ドキュメントを表示するにはユーザが Office 365 - SharePoint Online にログインしている必要があります。
SharePoint Online のファイラを作成する
前述の実装例を組み合わて SharePoint Online のファイラを作成します。
providerId は「 Office 365 連携 セットアップガイド 」で環境構築の際に設定した <im_office365_files_api.xml> のプロバ イダIDを指定してください。
実装例 実装例
SharePointStorage#directoriesStorages()、SharePointStorage#filesStorages() を利用します。 %CONTEXT_PATH%/sample/office365/filer/index.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@ taglib prefix="imui" uri="http://www.intra-mart.co.jp/taglib/imui"%>
<%@ taglib prefix="imtag" uri="http://www.intra-mart.co.jp/taglib/core/standard"%> <%@ page import="java.io.InputStream"%>
<%@ page import="java.io.IOException"%> <%@ page import="java.io.OutputStream"%> <%@ page import="java.util.ArrayList"%> <%@ page import="java.util.Collection"%>
<%@ page import="jp.co.intra_mart.common.platform.log.Logger"%> <%@ page import="jp.co.intra_mart.foundation.context.Contexts"%>
<%@ page import="jp.co.intra_mart.foundation.context.model.AccountContext"%>
<%@ page import="jp.co.intra_mart.foundation.oauth.client.service.OAuthPreconditionException"%> <%@ page import="jp.co.intra_mart.foundation.office365.service.storage.SharePointStorage"%> <%
String providerId ="Please_input_your_ID";
String userCd = Contexts.get(AccountContext.class).getUserCd();
String currentDirPath =
request.getParameter("targetDirPath")!=null?
addSlashToTheEnd(request.getParameter("targetDirPath")):"/";
String errorMsg =null;
SharePointStorage storage =new SharePointStorage(userCd, providerId, currentDirPath);
// 一覧取得
Collection<SharePointStorage> directoriesStorages =new ArrayList<SharePointStorage>();
Collection<SharePointStorage> filesStorages =new ArrayList<SharePointStorage>();
try{
directoriesStorages = storage.directoriesStorages(false);
filesStorages = storage.filesStorages(false);
}catch(IOException e){
errorMsg = buildErrorMessage("ディレクトリ、または、ファイル一覧の取得時にエラーが発生しました。", e);
}
// 親ディレクトリの存在チェック
String parentDir = storage.getParent();
String existsParentDir = parentDir !=null?"true":"false"; %>
<imui:head>
<title>Office365 - SharePointStorage API サンプル</title> <scriptsrc="csjs/im_window.js"></script>
<scripttype="text/javascript">
function moveDir(targetDirPath){
$('#targetDirPath').val('<%= currentDirPath %>' + targetDirPath); $('#moveDirForm').submit();
}
function downloadFile(targetFilePath){
$('#targetFilePath').val('<%= currentDirPath %>' + targetFilePath); $('#targetFileName').val(targetFilePath);
$('#downloadFileForm').submit(); }
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 } function showIframe(targetPath){ openNewWindow("office365_show_iframe").focus();
$('#targetPathForShowIframe').val('<%= currentDirPath %>' + targetPath); $('#showIframeForm').submit();
}
$(document).ready(function() { if(<%= errorMsg !=null%>){
imuiShowErrorMessage('<%= errorMsg %>'); } $('#toParent').click(function() { $('#targetDirPath').val('/' + '<%= parentDir %>'); $('#moveDirForm').submit(); }); }); </script> </imui:head> <divclass="imui-container"> <divclass="imui-title"> <h1><%= currentDirPath %></h1> </div> <divclass="imui-form-container-narrow"> <divclass="imui-chapter-title"> <h2>ファイル一覧</h2> </div>
<formmethod="post"action="sample/office365/filer/index.jsp">
<inputtype="text"name="targetDirPath"value="<%= currentDirPath %>"size="70"/>
<inputtype="submit"value="移動"class="imui-small-button"/>
</form> <br/> <tableclass="imui-table"> <tr> <th>名前</th> <th>詳細</th> <th>削除</th> <th>iframe表示</th> </tr>
<imtag:conditionvalidity="<%= existsParentDir %>">
<tr>
<td><ahref="javascript:void(0)"id="toParent">親ディレクトリへ移動</a></td>
<td></td> <td></td> <td></td> </tr> </imtag:condition> <!-- ディレクトリ一覧 -->
<imtag:repeatlist="<%= directoriesStorages %>"type="SharePointStorage"item="dirStorage" index="idx">
<tr>
<td>
<spanclass="im-ui-icon-common-16-folder"></span>
<ahref="javascript:void(0)"
onclick="moveDir('<%= dirStorage.getName()%>');"><%= dirStorage.getName()%></a>
</td>
<tdalign="center">
<ahref="javascript:void(0)"onclick="showDetail('<%= dirStorage.getName()%>');">
<spanclass="im-ui-icon-common-16-information"></span>
</a>
</td>
<tdalign="center">
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
<spanclass="im-ui-icon-common-16-trashbox"></span>
</a> </td> <td></td> </tr> </imtag:repeat> <!-- ファイル一覧 -->
<imtag:repeatlist="<%= filesStorages %>"type="SharePointStorage"item="fileStorage" index="idx">
<tr>
<td>
<spanclass="im-ui-icon-common-16-document"></span>
<ahref="javascript:void(0)"
onclick="downloadFile('<%= fileStorage.getName()%>');"><%= fileStorage.getName()%></a>
</td>
<tdalign="center">
<ahref="javascript:void(0)"onclick="showDetail('<%= fileStorage.getName()%>');">
<spanclass="im-ui-icon-common-16-information"></span>
</a>
<tdalign="center">
<ahref="javascript:void(0)"onclick="removeFileOrDir('<%= fileStorage.getName()%>');">
<spanclass="im-ui-icon-common-16-trashbox"></span>
</a>
</td>
<tdalign="center">
<imtag:conditionvalidity="<%= isWordExcelPowerPointFile(fileStorage.getName())%>">
<ahref="javascript:void(0)"onclick="showIframe('<%= fileStorage.getName()%>');">
<spanclass="im-ui-icon-common-16-preview"></span>
</a> </imtag:condition> </td> </tr> </imtag:repeat> </table> <br/> <divclass="imui-chapter-title"> <h2>ファイルアップロード</h2> </div>
<formid="uploadForm"action="sample/office365/filer/upload.jsp"enctype="multipart/form-data">
<imui:fileUploadouterFormId="uploadForm"/>
<inputtype="hidden"name="targetDirPath"value="<%= currentDirPath %>"/>
<inputtype="hidden"name="providerId"value="<%= providerId %>"/>
</form>
<divclass="imui-chapter-title">
<h2>ディレクトリ新規作成</h2>
</div>
<formmethod="POST"name="createDir"id="createDir"
action="sample/office365/filer/create_dir.jsp">
<inputtype="text"name="createDirName"/>
<inputtype="hidden"name="targetDirPath"value="<%= currentDirPath %>"/>
<inputtype="hidden"name="providerId"value="<%= providerId %>"/>
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 action="sample/office365/filer/download.jsp">
<inputtype="hidden"name="targetFilePath"id="targetFilePath"/>
<inputtype="hidden"name="targetFileName"id="targetFileName"/>
<inputtype="hidden"name="providerId"value="<%= providerId %>"/>
</form>
<formmethod="POST"name="removeFileOrDirForm"id="removeFileOrDirForm"
action="sample/office365/filer/remove.jsp">
<inputtype="hidden"name="targetPathForRemove"id="targetPathForRemove"/>
<inputtype="hidden"name="targetDirPath"id="targetDirPath"value="<%=currentDirPath%>"/>
<inputtype="hidden"name="providerId"value="<%= providerId %>"/>
</form>
<formmethod="POST"name="showDetailForm"id="showDetailForm"
action="sample/office365/filer/show_detail.jsp"target="office365_show_detail">
<inputtype="hidden"name="targetPathForShowDetail"id="targetPathForShowDetail"/>
<inputtype="hidden"name="targetDirPath"id="targetDirPath"value="<%=currentDirPath%>"/>
<inputtype="hidden"name="providerId"value="<%= providerId %>"/>
</form>
<formmethod="POST"name="showIframeForm"id="showIframeForm"
action="sample/office365/filer/show_iframe.jsp"target="office365_show_iframe">
<inputtype="hidden"name="targetPathForShowIframe"id="targetPathForShowIframe"/>
<inputtype="hidden"name="targetDirPath"id="targetDirPath"value="<%=currentDirPath%>"/>
<inputtype="hidden"name="providerId"value="<%= providerId %>"/>
</form>
</div> </div> <%!
privatestatic Logger logger = Logger.getLogger("sample.office365.filer.index");
private String buildErrorMessage(final String errorMessage,final Exception errorDetail){
logger.error(errorDetail.getMessage(), errorDetail);
if(errorDetail instanceof OAuthPreconditionException){
return"「外部連携アプリケーション」画面でアプリケーションを許可してください。";
}else{
return errorMessage +"<br/><br/>"+ errorDetail.getMessage();
}
}
private String isWordExcelPowerPointFile(final String fileName){
finalint indexOfLastDot = fileName.lastIndexOf(".");
if(indexOfLastDot ==-1){
return"false";
}
final String extension = fileName.substring(indexOfLastDot);
if(".docx".equals(extension)||".xlsx".equals(extension)||".pptx".equals(extension)){
return"true";
}else{
return"false";
}
}
private String addSlashToTheEnd(final String s){
finalint indexOfLastSlash = s.lastIndexOf("/");
if((indexOfLastSlash ==-1)||(indexOfLastSlash == s.length()-"/".length())){
return s; }else{ return s +"/"; } } %>
SharePoint Online のファイラを作成する (スクリプト開発モデル)
ここでは、 Office 365 連携機能 の SharePointStorage APIを利用して実際にプログラムを作成する過程を説明します。 本項では、例として スクリプト開発モデル を利用しています。 本項に記載のサンプルを組み合わせると 以下のような SharePoint Online のファイラが完成します。 SharePoint Online のファイラの完成イメージのファイラの完成イメージ項目
SharePoint Online 上にディレクトリを新規作成する SharePoint Online 上にファイルをアップロードする SharePoint Online 上のファイルをダウンロードする SharePoint Online 上のファイルの詳細情報を表示する SharePoint Online 上のファイルを削除するSharePoint Online 上のドキュメントをiframeで表示する SharePoint Online のファイラを作成する
ルーティングテーブルを作成する
注意
Office 365 連携 のAPIの詳細については以下を参照してください。 「 APIドキュメント - SharePointStorage 」
SharePoint Online 上にディレクトリを新規作成する
SharePoint Online 上にディレクトリを新規作成する実装のサンプルです。 リクエストパラメータとして受け取った プロバイダID、ターゲットのディレクトリパス、ディレクトリの名称 を元に新規ディレ クトリを作成します。 実装例 実装例 SharePointStorage#makeDirectories() を利用します。 %CONTEXT_PATH%/WEB-INF/jssp/src/sample/office365/filer/create_dir.js 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 var $currentDirPath; var $successMsg; var $errorMsg; var $dirPath; function init(request) {var userCd = Contexts.getAccountContext().userCd; var providerId = request.providerId;
$currentDirPath = request.targetDirPath !=null? request.targetDirPath :'/';
var createDirName = request.createDirName !=null? request.createDirName :null; $dirPath = $currentDirPath + createDirName;
if (createDirName ==null) {
$errorMsg ='ディレクトリ名が指定されていません。'; } else {
try{
var storage =new SharePointStorage(userCd, providerId, $dirPath); var result = storage.makeDirectories();
if(result){ $successMsg = 'ディレクトリを作成しました。<br><br>'+ $dirPath; }else{ $errorMsg ='ディレクトリを作成できませんでした。<br><br>'+ $dirPath; } }catch(e){ $errorMsg ='ディレクトリ作成時にエラーが発生しました。'+'<br><br>'+ e.errorMessage; } } } %CONTEXT_PATH%/WEB-INF/jssp/src/sample/office365/filer/create_dir.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 <script type="text/javascript">
$(document).ready(function() {
var successMsg ='<imart type="string" value=$successMsg />'; var errorMsg ='<imart type="string" value=$errorMsg />'; if (successMsg !='') { imuiShowSuccessMessage(successMsg); } elseif (errorMsg !='') { imuiShowErrorMessage(errorMsg); } }); </script> <imarttype="head">
<title>Office365 - SharePointStorage API サンプル - ディレクトリ作成</title> </imart>
<divclass="imui-title">
<h1>
<imarttype="string"value=$dirPath/>
</h1> </div> <divclass="imui-toolbar-wrap"> <divclass="imui-toolbar-inner"> <ulclass="imui-list-toolbar"> <li>
<ahref="javascript:$('#backToIndex').submit();"class="imui-toolbar-icon"title="一覧に戻る">
<spanclass="im-ui-icon-common-16-back"></span> 一覧に戻る </a>
</li>
</ul>
</div> </div>
<formid="backToIndex"method="post"action="sample/office365/filer">
<imarttype="hidden"targetDirPath=$currentDirPath/> </form>
注意
Microsoft社が提供する Office 365 のWebAPIの動作仕様に起因し以下の制限があります。
SharePointStorage#makeDirectories() を利用しディレクトリを作成する場合、ルートディレクトリ直下に存在 するディレクトリ名と同名のディレクトリを任意のディレクトリ配下に作成することはできません。
SharePointStorage#createAsBinary() を利用します。 画面遷移を伴わないため、プレゼンテーションページは不要です。 %CONTEXT_PATH%/WEB-INF/jssp/src/sample/office365/filer/upload.js 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 function init(request) {
var userCd = Contexts.getAccountContext().userCd; var upfile = request.getParameter('local_file'); var providerId = request.providerId;
var currentDirPath = request.targetDirPath; var response = Web.getHTTPResponse(); response.setContentType('json');
request.setAttribute('imui-theme-builder-module', 'notheme'); var targetFilePath = currentDirPath + upfile.getFileName(); upfile.openValueAsBinary(function(reader) {
var storage =new SharePointStorage(userCd, providerId, targetFilePath); storage.createAsBinary(function(writer) {
reader.transferTo(writer); response.sendMessageBodyString(ImJson.toJSONString([{ 'name':upfile.getFileName(), 'size':upfile.getLength() }])); }); }); }
SharePoint Online 上のファイルをダウンロードする
SharePoint Online 上のファイルをダウンロードする実装のサンプルです。 リクエストパラメータとして受け取った プロバイダID、ターゲットのファイルパス、ファイルの名称を元にファイルをダウン ロードします。 実装例 実装例 Module.download.send() を利用します。 画面遷移を伴わないため、プレゼンテーションページは不要です。 %CONTEXT_PATH%/WEB-INF/jssp/src/sample/office365/filer/download.js 1 2 3 4 5 6 7 function init(request) {var userCd = Contexts.getAccountContext().userCd; var providerId = request.providerId;
var targetFilePath = request.targetFilePath;
var storage =new SharePointStorage(userCd, providerId, targetFilePath); Module.download.send(storage, storage.getName()); }
SharePoint Online 上のファイルの詳細情報を表示する
SharePoint Online 上のファイルの詳細情報を表示する実装のサンプルです。 リクエストパラメータとして受け取った プロバイダID、ターゲットのファイルパスをファイルの詳細情報を表示します。 実装例 実装例 %CONTEXT_PATH%/WEB-INF/jssp/src/sample/office365/filer/show_detail.js1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 var $errorMsg; var $targetDirPathForShowDetail; var $detail ={}; function init(request) {
var userCd = Contexts.getAccountContext().userCd; var providerId = request.providerId;
$targetDirPathForShowDetail = request.targetDirPathForShowDetail; if ($targetDirPathForShowDetail ==null) {
$errorMsg ='ファイル、または、ディレクトリが指定されていません。'; } else {
var storage =new SharePointStorage(userCd, providerId, $targetDirPathForShowDetail);
$detail.canonicalPath = storage.getCanonicalPath(); $detail.webUrl = storage.getWebUrl();
$detail.decodedWebUrl =decodeURIComponent(storage.getWebUrl()); $detail.created =newDate(storage.created());
$detail.createdUserId = storage.createdUserId();
$detail.createdUserName = storage.createdUserName(); $detail.lastModified =newDate(storage.lastModified()); $detail.lastModifiedUserId = storage.lastModifiedUserId(); $detail.lastModifiedUserName = storage.lastModifiedUserName(); $detail.length = storage.length(); } } %CONTEXT_PATH%/WEB-INF/jssp/src/sample/office365/filer/show_detail.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 <script type="text/javascript">
$(document).ready(function() {
var errorMsg ='<imart type="string" value=$errorMsg />'; if (errorMsg !='') { imuiShowErrorMessage(errorMsg); } }); </script> <imarttype="head">
<title>Office365 - SharePointStorage API サンプル - 詳細表示</title> </imart>
<divclass="imui-title">
<h1>
<imarttype="string"value=$targetDirPathForShowDetail/>
</h1> </div> <divclass="imui-form-container-narrow"> <divclass="imui-chapter-title"> <h2>詳細情報</h2> </div> <tableclass="imui-table"> <tr> <th>正規化したパス</th> <td>
<imarttype="string"value=$detail.canonicalPath/>
</td>
</tr>
<tr>
<th>コンテンツをブラウザ上で確認可能なWeb URL</th>
<td>
<imarttype="tag"tagname="a"href=$detail.webUrl>
<imarttype="string"value=$detail.decodedWebUrl/></imart>
</td>
</tr>
<tr>
<th>作成時刻</th>
<td>
<imarttype="dateTime"value=$detail.createdpattern="yyyy/MM/dd hh:mm:ss"/>
</td>
</tr>
<tr>
<th>作成ユーザID</th>
<td>
<imarttype="string"value=$detail.createdUserId/>
</td>
</tr>
<tr>
<th>作成ユーザ名</th>
<td>
<imarttype="string"value=$detail.createdUserName/>
</td>
</tr>
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
<imarttype="string"value=$detail.lastModifiedUserId/>
</td>
</tr>
<tr>
<th>最終更新ユーザ名</th>
<td>
<imarttype="string"value=$detail.lastModifiedUserName/>
</td>
</tr>
<tr>
<th>ファイルサイズ</th>
<td>
<imarttype="string"value=$detail.length/> バイト </td> </tr> </table> </div>
SharePoint Online 上のファイルを削除する
SharePoint Online 上のファイルを削除する実装のサンプルです。 リクエストパラメータとして受け取った プロバイダID、ターゲットのファイルパスを元にファイルを削除します。 実装例 実装例 SharePointStorage#remove() を利用します。 %CONTEXT_PATH%/WEB-INF/jssp/src/sample/office365/filer/remove.js 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 var $successMsg; var $errorMsg; var $targetPath; var $currentDirPath; function init(request) {var userCd = Contexts.getAccountContext().userCd; var providerId = request.providerId;
$currentDirPath = request.targetDirPath !=null? request.targetDirPath :'/';
$targetPath = request.targetPathForRemove !=null? request.targetPathForRemove :null; if ($targetPath ==null) {
$errorMsg ='ファイル、または、ディレクトリが指定されていません。'; } else {
try {
var storage =new SharePointStorage(userCd, providerId, $targetPath); var result = storage.remove(true);
if (result) { $successMsg ='「'+ $targetPath +'」を削除しました。'; } else { $errorMsg ='「'+ $targetPath +'」を削除できませんでした。'; } } catch (e) { $errorMsg ='削除時にエラーが発生しました。'+'<br><br>'+ e.errorMessage; } } } %CONTEXT_PATH%/WEB-INF/jssp/src/sample/office365/filer/remove.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 <script type="text/javascript">
$(document).ready(function() {
var successMsg ='<imart type="string" value=$successMsg />'; var errorMsg ='<imart type="string" value=$errorMsg />'; if (successMsg !='') { imuiShowSuccessMessage(successMsg); } elseif (errorMsg !='') { imuiShowErrorMessage(errorMsg); } }); </script> <imarttype="head">
<title>Office365 - SharePointStorage API サンプル - 削除</title> </imart>
<divclass="imui-title">
<h1>
<imarttype="string"value=$targetPath/>
</h1> </div> <divclass="imui-toolbar-wrap"> <divclass="imui-toolbar-inner"> <ulclass="imui-list-toolbar"> <li>
<ahref="javascript:$('#backToIndex').submit();"class="imui-toolbar-icon"title="一覧に戻る">
<spanclass="im-ui-icon-common-16-back"></span> 一覧に戻る </a>
</li>
</ul>
</div> </div>
<formmethod="post"action="sample/office365/filer"id="backToIndex">
<imarttype="hidden"targetDirPath=$currentDirPath/> </form>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 var $errorMsg; var $targetPathForShowIframe; var $isWordFile; var $isExcelFile; var $isPowerPointFile; var $providerId function init(request) {
var userCd = Contexts.getAccountContext().userCd; $providerId = request.providerId;
$targetPathForShowIframe = request.targetPathForShowIframe; if ($targetPathForShowIframe ==null) {
$errorMsg ='ファイルが指定されていません。'; } else {
var storage =new SharePointStorage(userCd, $providerId, $targetPathForShowIframe); $isWordFile = isWord(storage.getName()); $isExcelFile = isExcel(storage.getName()); $isPowerPointFile = isPowerPoint(storage.getName()); } } function isWord(fileName) {
return getExtension(fileName) =='.docx'?true:false; }
function isExcel(fileName) {
return getExtension(fileName) =='.xlsx'?true:false; }
function isPowerPoint(fileName) {
return getExtension(fileName) =='.pptx'?true:false; }
function getExtension(fileName) {
var indexOfLastDot = fileName.lastIndexOf('.'); if (indexOfLastDot ==-1) {
returnnull; }
return fileName.substring(indexOfLastDot); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 <script type="text/javascript">
$(document).ready(function() {
var errorMsg ='<imart type="string" value=$errorMsg />'; if(errorMsg !=''){ imuiShowErrorMessage(errorMsg); } }); </script> <imarttype="head">
<title>Office365 - iframe 表示</title> </imart>
<divclass="imui-title">
<h1>
<imarttype="string"value=$targetPathForShowIframe/>
</h1> </div>
<divid="imui-container-inner">
<imarttype="condition"validity=$isWordFile>
<imarttype="imOffice365Word"providerId=$providerIdpath=$targetPathForShowIframe
width="640"height="480"/>
</imart>
<imarttype="condition"validity=$isExcelFile>
<imarttype="imOffice365Excel"providerId=$providerIdpath=$targetPathForShowIframe
width="640"height="480"/>
</imart>
<imarttype="condition"validity=$isPowerPointFile>
<imarttype="imOffice365PowerPoint"providerId=$providerIdpath=$targetPathForShowIframe
width="640"height="480"/>
</imart> </div>
SharePoint Online のファイラを作成する
前述の実装例を組み合わて SharePoint Online のファイラを作成します。
providerId は「 Office 365 連携 セットアップガイド 」で環境構築の際に設定した <im_office365_files_api.xml> のプロバ イダIDを指定してください。
実装例 実装例
SharePointStorage#directoriesStorages()、SharePointStorage#filesStorages() を利用します。 %CONTEXT_PATH%/WEB-INF/jssp/src/sample/office365/filer/index.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
var $providerId ='dhimart_1st.onmicrosoft.com';
var $userCd = Contexts.getAccountContext().userCd;
var $directoriesStorages; var $filesDetails=[]; var $currentDirPath; var $errorMsg; var $parentDir; var $existsParentDir; function init(request){
$currentDirPath = request.targetDirPath !=null? addSlashToTheEnd(request.targetDirPath) :'/'; try{
var storage = new SharePointStorage($userCd, $providerId, $currentDirPath); $directoriesStorages = storage.directoriesStorages(false);
var filesStorages = storage.filesStorages(false); for (var i =0 ; i<filesStorages.length ; i++){ var fileDetail = { fileName : filesStorages[i].getName() , filePath : filesStorages[i].getPath() , isWordExcelPowerPointFile:isWordExcelPowerPointFile(filesStorages[i].getName()) }; $filesDetails[i] = fileDetail; } $parentDir = storage.getParent(); if($parentDir ==null){ $existsParentDir =false; }else{ $existsParentDir =true; }
}catch(e if e.errorCode =='oauth.precondition'){
$errorMsg ='「外部連携アプリケーション」画面でアプリケーションを許可してください。'; }catch(e){ $errorMsg ='ディレクトリ、または、ファイル一覧の取得時にエラーが発生しました。' +'<br><br>'+ e.errorMessage; } } function isWordExcelPowerPointFile(fileName){ var indexOfLastDot = fileName.lastIndexOf('.'); if (indexOfLastDot ==-1) {
returnfalse; }
var extension = fileName.substring(indexOfLastDot);
if (extension =='.docx'|| extension =='.xlsx'|| extension =='.pptx') { returntrue; } else { returnfalse; } } function addSlashToTheEnd(s) {
var indexOfLastSlash = s.lastIndexOf('/');
if((indexOfLastSlash ==-1) || s.substr((s.length -1)) =='/'){ return s; } else { return s +'/'; } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 <script type="text/javascript"> function moveDir(targetDirPath) {
$('#targetDirPathForMove').val('/'+ targetDirPath); $('#moveDirForm').submit(); } function downloadFile(targetFilePath) { $('#targetFilePath').val(targetFilePath); $('#downloadFileForm').submit(); } function removeFileOrDir(targetPath) { $('#targetPathForRemove').val(targetPath); $('#removeFileOrDirForm').submit(); } function showDetail(targetDirPath) { $('#targetDirPathForShowDetail').val(targetDirPath); $('#showDetailForm').submit(); } function showIframe(targetFilePath) { $('#targetPathForShowIframe').val(targetFilePath); $('#showIframeForm').submit(); }
$(document).ready(function() {
var errorMsg ='<imart type="string" value=$errorMsg />'; if(errorMsg !=''){
imuiShowErrorMessage(errorMsg); }
});
$(function($) {
$('#toParent').click(function() { $('#targetDirPathForMove').val(
'/'+'<imart type="string" value=$parentDir />'); $('#moveDirForm').submit();
}); });
</script>
<imarttype="head">
<title>Office365 - SharePointStorage API サンプル</title> </imart>
<divclass="imui-title">
<h1>
<imarttype="string"value=$currentDirPath/>
</h1> </div> <divclass="imui-form-container-narrow"> <divclass="imui-chapter-title"> <h2>ファイル一覧</h2> </div>
<formmethod="post"action="sample/office365/filer">
<imarttype="imuiTextbox"name="targetDirPath"value=$currentDirPathsize="70"/>
<inputtype="submit"value="移動"class="imui-small-button"/>
</form>