Multiple Attachment in Document Library
_Indonesian: (source code)_ Didalam Sharepoint 2003, kita mengenal Document Library. Document Library pada dasarnya adalah List yang “dihubungkan” dengan folder, sehingga Document Library dapat digunakan untuk menyimpan file. Cara penyimpanan file di Document Library mirip dengan proses penyimpanan attachment di List, dan file di Document Library akan berkaitan langsung dengan satu record di List. Hal ini mengakibatkan, file-file di Document Library tidak bisa memiliki attachment (dokumen pendukung). Lantas, apa yang harus dilakukan agar file-file di Document Library tersebut dapat memiliki attachment? _English (source code):_ In SharePoint 2003, we knew about Document Library. A Document Library basically is a List which was “connected” to a folder, therefore Document Library is a file repository in SharePoint 2003 model. Uploading file to Document Library works similliar with attaching file to a List. A file in Document Library will always have a correspond record in a List. That’s why, files in Document Library can not have any attachment. Then what should we do, to make our Document Library supports attachment? Strategi (see also English version) Pertama-tama kita harus menyusun strategi yang akan digunakan.
- Agar file-file di Document Library (DL) dapat mendukung file attachment, maka harus ada Document Library lain (sebut saja AttachDL) sebagai tempat penyimpanan file attachment.
- Kita harus menambahkan context menu di DL untuk melakukan upload, dan view attachment.
- Untuk mendukung multi file attachment, maka file-file yang disimpan di dalam AttachDL harus disimpan didalam folder-folder sesuai dengan nama file induknya.
Proses membuat AttachDL dan menambahkan context menu tidak akan saya bahas disini. Pembahasan akan dititik-beratkan pada implementasi no. 3, yaitu upload file dan membuat folder-folder sesuai dengan nama file induknya. Implementasi Pada saat user akan menambahkan dokumen pendukung, maka program kita harus secara otomatis mampu membuat folder sesuai dengan yang diharapkan.
Untuk itu, kita membutuhkan bantuan web service standar Sharepoint 2003 yaitu:
- Document Workspace Service (*/_vti_bin/dws.asmx)
- Site Data Service (*/_vti_bin/sitedata.asmx)
Masing-masing akan digunakan untuk membuat folder (dws) dan mengecek struktur folder yang telah ada di Sharepoint. Upload Attachment Proses upload diawali dengan mengecek komposisi folder file induk. Struktur folder tersebut disusun kedalam array agar mudah untuk diiterasi.
function AddAttachment(ctx) { var listUrlDir = ctx.listUrlDir; if(ctx.listUrlDir.substring(0,5)== “Lists”) listUrlDir = ctx.listUrlDir.substring(6); (….more….) CheckFolderCallback(); }
Proses dilanjukan dengan mengeksekusi CheckFolderCallback(); CheckFolderCallback() adalah fungsi yang akan menjalankan web service DWS (untuk membuat folder) dan Sitedata (untuk mengecek struktur folder) secara bergantian.
function CheckFolderCallback() { if(!xmlHttp || xmlHttp.readyState == 4) { if(xmlHttp) { if(xmlHttp.statusText != “OK”) { CreateFolder(); } (….more…) // send request SendHttpRequest(checkFolderWebServiceUrl, checkFolderWebServiceMethod, requestBody, true, CheckFolderCallback,siteNamespace); arrayFolderIndex++; } }
Setelah folder selesai dipersiapkan, maka user akan diarahkan ke URL untuk melakukan upload. Url untuk upload dapat diperoleh dengan menggunakan fungsi dasar yang ada di ows.js
uploadAction = “STSNavigate(‘” + ctx.HttpRoot + “/” + UPLOAD_LIBRARY + “/” + UPLOAD_PAGE + “?RootFolder=”+ escapeProperly(rootFolder) +”&Source=” + GetSource() + “&Mainlib=” + listUrlDir + “/” + currentItemID+“‘)”;
Di ilustrasi terlihat bahwa, halaman upload sudah mengarah ke folder yang bersesuaian dengan file yang dikehendaki. List Attachment Menampilkan attachment suatu file di Document Library, dilakukan dengan mengarahkan user ke AttachDL dan mengganti nilai variable RootFolder agar sesuai dengan lokasi file yang dikehendaki.
var strAction = “STSNavigate(‘” + ctx.HttpRoot + “/” + UPLOAD_LIBRARY + “/” + UPLOAD_LISTPAGE +“?RootFolder=”+escapeProperly(rootFolder)+”&Source=” + GetSource() + “‘)”;
Keterbatasan Teknik ini memiliki keterbatasan terutama pada tingkat kedalaman folder untuk file asli (di DL). Seperti yang telah didokumentasikan oleh Microsoft, sebuah Document Library memiliki keterbatasan pada tingkat kedalaman folder (N). Jadi kalau kita membuat folder secara bertingkat pada sebuah Document Library, maka angka N ini tidak bisa dilewati. Teknik diatas akan membuat folder tambahan di AttachDL sebanyak 2, masing-masing untuk nama Document Library Asli dan hirarki folder / file. Sehingga, tingkat kedalaman folder asli di DL tidak boleh lebih dari N-2. Pengembangan Metoda diatas dapat pula dikembangkan untuk List, atau dengan merubah sedikit strategi yang digunakan, maka tingkat kedalaman folder sebanyak N di Document Library asli masih dapat tercapai.
-–o0o—
Strategy (versi Bahasa Indonesia) First, I will explain the strategy to achieve the goal.
- We need another Document Library (named it AttachDL) as attachment repository for the original Document Library (DL).
- A new context menu is needed in DL so we can upload and view attachment easily.
- To support multi file attachments, then all files in AttachDL must be placed in separate folder - based on corelated file name in DL.
I will not explain how to make AttachDL or adding context menu in Document Library list. I will focus on the folder creation, and file uploading (no. 3). Implementation When the users click “Upload Attachment” , just before uploading page appear, our scripts must prepare for folder hierarchy.
Therefore we need standard Sharepoint web services:
- Document Workspace Service (*/_vti_bin/dws.asmx)
- Site Data Service (*/_vti_bin/sitedata.asmx)
Each services will take part to in folder creation (dws) and site structure management (sitedata). Upload Attachment Before we upload file, the folder must be exists in Document Library. We check folder hierarchy and prepare them in an array.
function AddAttachment(ctx) { var listUrlDir = ctx.listUrlDir; if(ctx.listUrlDir.substring(0,5)== “Lists”) listUrlDir = ctx.listUrlDir.substring(6); (….more….) CheckFolderCallback(); }
Next, script continue to execute CheckFolderCallback(); CheckFolderCallback() is a function which will issue command to DWS (to create folder) and Sitedata (to check folder schema).
function CheckFolderCallback() { if(!xmlHttp || xmlHttp.readyState == 4) { if(xmlHttp) { if(xmlHttp.statusText != “OK”) { CreateFolder(); } (….more…) // send request SendHttpRequest(checkFolderWebServiceUrl, checkFolderWebServiceMethod, requestBody, true, CheckFolderCallback,siteNamespace); arrayFolderIndex++; } }
After all folder hierarchy has been prepared, then users will be redirected to upload URL. The upload URL can be constructed using standard function in ows.js.
uploadAction = “STSNavigate(‘” + ctx.HttpRoot + “/” + UPLOAD_LIBRARY + “/” + UPLOAD_PAGE + “?RootFolder=”+ escapeProperly(rootFolder) +”&Source=” + GetSource() + “&Mainlib=” + listUrlDir + “/” + currentItemID+“‘)”;
As you can see in the ilustration above, the upload page ends in associated folder with the main file. List Attachment To show all attachment of a file in Document Library, we do as easy as guiding users to AttachDL and replacing appropriate value for RootFolder variable.
var strAction = “STSNavigate(‘” + ctx.HttpRoot + “/” + UPLOAD_LIBRARY + “/” + UPLOAD_LISTPAGE +“?RootFolder=”+escapeProperly(rootFolder)+”&Source=” + GetSource() + “‘)”;
Minus This technique has folder depth shortage in DL. As it has been documented by Microsoft, a Document Library has maximum level of folder creation up to N levels. So if we make a folder hierarchy in Document Library, then we can’t go deeper then N. Our methods create additional folder level in AttachDL, each for the original Document Title and folder/file hierarchy. Hence, we will lost 2 level - or shortly speaking the original DL can not hold more then N-2 levels. Improvement You can also use that method for List, or you can also modify the folder hierarchy strategy to achieve N level folder depth.