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.

  1. 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.
  2. Kita harus menambahkan context menu di DL untuk melakukan upload, dan view attachment.
  3. 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.

ContextMenu

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+”‘)”;

UploadAttachment

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() + “‘)”;

ListAttachment

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.

  1. We need another Document Library (named it AttachDL) as attachment repository for the original Document Library (DL).
  2. A new context menu is needed in DL so we can upload and view attachment easily.
  3. 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.

ContextMenu

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+”‘)”;

UploadAttachment

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() + “‘)”;

ListAttachment

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.

About

Riwut Libinuko, experienced Architect. Currently working with Credit Agricole CIB, responsible for SharePoint platform in Asia Pasific. . Certification: SharePoint 2013, SharePoint 2010, SharePoint 2007, ITiL v3

Posted in Microsoft Sharepoint, Tips and Tricks

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: