23 March 2010

Webpart :File Upload with Folder Drill Down

Development Steps:
Custom webPart is created for specific document library which populates folders and sub-folder ina dropdown control showing the folder name and file control to upload file to the selected folder/sub-folder.

1. Control initialization is carried in the CreateChildControls()
Note: since the control needs to pertain existing values in on page I referred I have enabled drop Down values true for
-----Code-----
<DropDown_Control>.AutoPostBack = true;
<DropDown_Control>.EnableViewState = true;
-----------

2. Loading the Parent drop-down with all the parent Folders

-----Code-----
using(SPWeb myWeb = mysite.OpenWeb())
{
//before selecting the Folder
ListItem listItem = new ListItem();
listItem.Text = "Select Folder";
listItem.Value = "";
<DropDown_Control1>.Items.Add(listItem);
myWeb.AllowUnsafeUpdates =true;
SPFolder mylibrary = myWeb.Folders[<Document Library Name>.ToString()];
SPFolderCollection AllFolders = mylibrary.SubFolders;
foreach (SPFolder folderin AllFolders)
{
listItem = new ListItem();
listItem.Text = folder.Name.ToString();
listItem.Value = folder.Name.ToString();
<DropDown Control_1>.Items.Add(listItem);
}
}

-----------

3. Loading the Child DropDown with-in the selected parent folders

-----Code-----
void<DropDown Control_1>_SelectedIndexChanged(object
sender, EventArgs e)
{
//The code is much similar the mentioned above except few minor variations
using(myWeb = mysite.OpenWeb())
{
ListItem listItem = newListItem();
listItem.Text = "Select Sub-Folder";
listItem.Value = "";
<DropDown Control_2>.Items.Add(listItem);
myWeb.AllowUnsafeUpdates = true;
string_MainFolder = [<Document Library Name>.ToString();
SPFolder Mainlibrary = myWeb.Folders[_MainFolder];
SPFolder mylibrary = Mainlibrary.SubFolders[<DropDown Control_1>.SelectedValue.ToString()];
SPFolderCollection AllFolders = mylibrary.SubFolders;
foreach (SPFolder folder in AllFolders)
{
listItem = newListItem();
listItem.Text = folder.Name.ToString();
listItem.Value = folder.Name.ToString();
<DropDown Control_2>.Items.Add(listItem);
}
}
}
-----------

4. I have used a file upload control to get the local file to be uploaded

5. To upload my local file in the specified folder location selected on my Button control <Button_Control_Click Event>

-----Code-----

void < Button_Control>_Click(object sender,
EventArgs
e)
{
if(<FileUpload_Control>.PostedFile !=null)
{
if(<FileUpload_Control>.PostedFile.ContentLength > 0)
{
System.IO.Stream strm = <FileUpload_Control>.PostedFile.InputStream;

byte[] FileContent = new byte[ Convert.ToInt32(<FileUpload_Control>.PostedFile.ContentLength)];

strm.Read(FileContent, 0, Convert.ToInt32(<FileUpload_Control>.PostedFile.ContentLength));

strm.Close();
// Open site where document library is created.
SPWeb myWeb = mysite.OpenWeb();

// Get the folder that should store the document In this case, there's a document library called "<Document Library Name>" within the Root Web of the Site Collection
SPFolder MainDocLib = myWeb.Folders[<Document Library Name>.ToString()];

// Within the "<Document Library Name>" library, add the document into its Parent Folder
SPFolder parent = MainDocLib.SubFolders[<DropDown Control_1>.SelectedValue.ToString()];

// Within the "<Document Library Name>" library, add the document into a Parent folder's Sub-Folder
SPFolder child =parent.SubFolders[<DropDown Control_2>.SelectedValue.ToString()];

// Upload document

myWeb.AllowUnsafeUpdates = true;

SPFile spfile = child.Files.Add(System.IO.Path.GetFileName(<FileUpload_Control >.PostedFile.FileName), FileContent,true);

child.Update();
myWeb.Dispose();

<Status_Control>.Text = "File Successfully Uploaded! @"+ child.Url.ToString();
}
}
else
{
<Status_Control>.Text = "Sorry! File Not Found!";
}
}
-----------

Bingo!



Special Thanks: Dhawal Mehta

1 comment:

  1. I'm more than happy to find this website. I need to to thank you for your time just for this fantastic read!! I definitely enjoyed every bit of it and i also have you saved as a favorite to look at new things in your web site.
    Also visit my web page :: dem direct email marketing

    ReplyDelete

Thanks for your valuable comments

Rate Now: