Pages

Thursday, November 15, 2012

How to set the destination folder in OOB Upload page!

During a sharepoint site customization, I made a web part that display the documents that the customer looking for. If the user has the permission to update the document an icon will be visible to accomplish this task; this icon call a javascript function that open the oob update page.

Problem:

For each document I can have an upload icon. Every of those documents are saved in a specific folder (don't ask me why!!!). The customer asked me that the upload page load automatically the folder where the file will be uploaded
upload-autopolulate
In this way if the document is in the folder /FolderA/SubFolderB, I will press the icon and I will have /FolderA/SubFolderB in my Destaination Folder textbox.
I have looking around on the net if anyone got a similar problem, but I didn’t find a response.

Solution:

Armed with a good time and two of my friends: notepad2 and JetBrains dotPeek, I did a bit of analysis.
First of all I opened the page
%SHAREPOINTFOLDER%\TEMPLATE\LAYOUTS\upload.aspx
by the notepad2 to understand which is the dll of its code behind:
<%@ Page Language="C#" DynamicMasterPageFile="~masterurl/default.master"  Inherits="Microsoft.SharePoint.ApplicationPages.UploadPage"%>
<%@ Import Namespace="Microsoft.SharePoint.ApplicationPages" %>
you can find this dll in the folder _app_bin of your site (example: C:\inetpub\wwwroot\wss\VirtualDirectories\26295\_app_bin). I opened the file Microsoft.SharePoint.ApplicationPages.dll with JetBrains dotPeek and I found what I was looking for:
protected virtual string CurrentFolderServerRelativeUrl
{
  get
  {
    if (this.m_folderUrl == null)
    {
      string str = this.Request.QueryString["RootFolder"];
      this.m_folderUrl = string.IsNullOrEmpty(str) ? this.CurrentList.RootFolderUrl : str;
    }
    return this.m_folderUrl;
  }
}
this property is used later for the following reason:
stringBuilder.Append("&RootFolder=");
stringBuilder.Append(SPHttpUtility.UrlKeyValueEncode(this.CurrentFolderServerRelativeUrl));
...
this.Destination.Text = SPHttpUtility.HtmlEncode(this.CurrentFolderServerRelativeUrl);
...
private SPFile UploadFile(out string leafName, out SPVirusCheckStatus checkStatus, out string virusMessage)
{
  HttpPostedFile postedFile = this.InputFile.PostedFile;
  leafName = UploadPage.GetLeafName(postedFile.FileName);
  SPFile file = this.Web.GetFile(this.CurrentFolderServerRelativeUrl + "/" + leafName);
  bool flag1 = false;
the variable Destination is an hidden field used by sharepoint and the key RootFolder is used to get which folder I want specify.
Okie dokie, so I need a javascript method like this:
function uploadDialog(folder) {
    var id = String(this.list.get_id());
    this.url = '/_layouts/Upload.aspx?List={' + id.toUpperCase() + '}&RootFolder=' + folder;

    optionProcedure = {
        width: 800,
        height: 500,
        url: this.url,
        title: 'Upload doc'
    };

    SP.UI.ModalDialog.showModalDialog(optionProcedure);
}

where the id is the ListId loaded before and the RootFolder is where I want upload the new file. I will call this method in this way:

<a href="#" onclick="javascript:uploadDialog('/Lists/MyDocs/FolderA/SubFolderB'); return false;">
<img src="/_layouts/images/xxx.Intranet.WebParts/procedura_upload.png" title="Open form" alt="Open form" style="border: 0px;"></a>


in this way I will have the Folder of my Destination folder populated with /FolderA/SubFolderB

No comments:

Post a Comment