The web part consists of following elements:
- a TextBox for Specifying the folder Name.
- asp:FileUpload to fetch the file to be uploaded.
- an asp:Button named Upload to upload the file. all the code needs to be written in its _click() event.
The web part has following elements
<table>
<tr>
<td>Enter the Name of Document Library:-
</td>
<td title="Enter the Name of Document Library if left blank then Default Document Liberary">
<asp:TextBox ID="txtLibraryName" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>Enter the Date of Document:-
</td>
<td title="Enter the Date of Document if left blank then random Date">
<asp:TextBox ID="txtDate" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>Browse the File:-
</td>
<td title="Browse the File if not then no file will be uploaded only a folder will be created">
<asp:FileUpload ID="FileUpload" runat="server" />
</td>
</tr>
</table>
<br />
<asp:Button ID="btnFileUpload" runat="server" Text='Upload "File" to a "Folder"' OnClick="btnFileUpload_Click" />
I have done the above program in SharePoint SSOM
`
protected void btnFileUpload_Click(object sender, EventArgs e)
{
//Web initilization
SPSite site = new SPSite("http://spappdc:33333/sites/Access");
SPWeb web = site.OpenWeb();
web.AllowUnsafeUpdates = true;
//Check Date TextBox
CheckLibrary(web);
web.AllowUnsafeUpdates = false;
}
`
`
public void CheckLibrary(SPWeb web)
{
//Check Library TextBox
if (txtLibraryName.Text == "")
{
//Get Default Document Library
SPList documentLibrary = web.Lists.TryGetList("Documents");
//Check Month Folder Exists
CheckFolderExists(web, documentLibrary);
}
else
{
//Check if Library exists
if (web.Lists.TryGetList(txtLibraryName.Text) == null)
{
//Create New Library
Guid documentLibraryID = web.Lists.Add(txtLibraryName.Text, "", SPListTemplateType.DocumentLibrary);
SPList documentLibrary = web.Lists[documentLibraryID];
//Check Month Folder Exists
CheckFolderExists(web, documentLibrary);
}
else
{
//Get the Library
SPList documentLibrary = web.Lists.TryGetList(txtLibraryName.Text);
//Check Month Folder Exists
CheckFolderExists(web, documentLibrary);
}
}
}
`
`
//Get Date
private string FolderName()
{
Random randomNo = new Random();
int month = txtDate.Text == "" ? 1 : int.Parse(txtDate.Text.Split('/')[1]) == 12 ? 1 : int.Parse(txtDate.Text.Split('/')[1]) + 1;
int year = txtDate.Text == "" ? 1990 : month == 1 ? int.Parse(txtDate.Text.Split('/')[2]) + 1 : int.Parse(txtDate.Text.Split('/')[2]);
string folderName;
txtDate.Text = "01/" + month.ToString() + "/" + year.ToString();
string monthName = string.Empty;
switch (month)
{
case 1:
monthName = "Jan";
break;
case 2:
monthName = "Feb";
break;
case 3:
monthName = "Mar";
break;
case 4:
monthName = "Apr";
break;
case 5:
monthName = "May";
break;
case 6:
monthName = "Jun";
break;
case 7:
monthName = "Jul";
break;
case 8:
monthName = "Aug";
break;
case 9:
monthName = "Sep";
break;
case 10:
monthName = "Oct";
break;
case 11:
monthName = "Nov";
break;
case 12:
monthName = "Dec";
break;
}
return folderName = monthName + "-" + year;
}
`
`
//Check Month Folder Exists
public void CheckFolderExists(SPWeb web, SPList documentLibrary)
{
string folderName = FolderName();
if (web.GetFolder(documentLibrary.RootFolder.Url + "/" + folderName).Exists)
{
//Folder Exists Upload File
SPFolder libraryMonthFolder = web.GetFolder(documentLibrary.RootFolder.Url + "/" + folderName);
UploadFile(libraryMonthFolder, documentLibrary);
}
else
//Create Folder And Upload
CreateNewFolderAndUpload(documentLibrary, folderName);
documentLibrary.Update();
}
`
`
//Create Folder And Upload
public void CreateNewFolderAndUpload(SPList documentLibrary, string folderName)
{
SPFolder libraryMonthFolder = documentLibrary.RootFolder.SubFolders.Add(documentLibrary.RootFolder.Url + "/" + folderName);
libraryMonthFolder.Update();
UploadFile(libraryMonthFolder, documentLibrary);
}
`
`
//Upload File In Folder
private void UploadFile(SPFolder libraryMonthFolder, SPList documentLibrary)
{
//upload only if file is browsed
if (FileUpload.HasFile)
{
libraryMonthFolder.Files.Add(libraryMonthFolder.Url + "/" + FileUpload.FileName, FileUpload.FileBytes, true);
libraryMonthFolder.Update();
}
}
`