Sharepoint WebPart for Programmatically Uploading Files to the Shared Documents Library





4.00/5 (1 vote)
This article describes the step by step processes to create a Web Part using C# for SharePoint Server 2007, and to programmatically upload a collection of files to the Document Library in SharePoint Server 2007.
Introduction
This article describes the step by step processes to create a Web Part using C# for SharePoint Server 2007, and to programmatically upload a collection of files to the Document Library in SharePoint Server 2007.
“Document Libraries, also called shared documents, are collections of files that can be shared with team members on Web based Microsoft Windows SharePoint Services”. By default, your team website comes with a built-in document library named Shared Documents, which is listed on the quick launch bar as well as on the Documents and Lists page. As shown below:
The files in Shared Documents are accessible for users who have permissions to read.
Background
It is required that the set of files present in a local or network computer need to be programmatically uploaded to the Shared Documents library of a SharePoint site by anyone who can access it from any part of the world. This can be done by creating a Share Point WebPart in Visual Studio .NET 2005 using C#. The steps to create a Web Part are:
- Start VS.NET 2005 and create a new project (Class Library).
- Rename the class as
WebpartUploadFiles
. - Add the following references by right clicking on the WebpartUploadFiles project and clicking on “Add Reference”: Microsoft.SharePoint and Microsoft.SharePoint.WebControls.
- Use the
Render
method: TheWebpart
base class seals theRender
method ofSystem.Web.UI.Control
because the Web Part infrastructure needs to control rendering the contents of a Web Part. For this reason, custom Web Parts must override theRender
method of theWebpart
base class. Create aUploadFiles()
method.
Note: If SharePoint Server 2007 is not installed on the local machine, then copy Microsoft.SharePoint.dll from the folder where it is installed to a folder on the computer where you will be developing the Web Parts.
//The Complete Web part Code :
#region File Information
///<summary>
/// ----------------------------------------------------------------------
/// Namespace : WebpartUploadFiles
///
/// Purpose : To Create Webpart to programmatically upload Files
// to DocumentLibrary
///
/// Change History
/// ----------------------------------------------------------------------
/// Date Edit Author Comment
/// -------------+--------+-----------------------------------------------
/// 08-Jan-2007 [100] Rajesh_Sajjanar Create Webpart
/// -------------+--------+-----------------------------------------------
///</summary>
#endregion
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing; //For Color
using Microsoft.SharePoint; // For all sharepoint related API’s
using Microsoft.SharePoint.WebControls; //For sharepoint webpage Controls
using System.Web.UI.WebControls; //For System.Web.UI.WebControls;
using System.IO; //For File Handling
namespace WebpartUploadFiles
{
//Inherits the Webparts class
public class WebpartUploadFiles :
System.Web.UI.WebControls.WebParts.WebPart
{
#region Decalration
private Label lblResults;
FileInfo[] Finfo;
SPSite objSite;
SPWeb objWeb;
#endregion
/* The WebPart base class seals the Render method of System.Web.UI.Control
because the Web Part infrastructure needs to control rendering the contents
of a Web Part for this reason, custom Web Parts must override
the Render method of the WebPart base class */
protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
base.Render(writer);
}
//Ovveride the method to create the objects for the Web Part's controls.
protected override void CreateChildControls()
{
base.CreateChildControls();
UploadFiles();
}
#region Method For UpLoadFiles
private void UploadFiles()
{
try
{
DirectoryInfo Dinfo = new DirectoryInfo(@"\\BLRRCS\Reports");
// BLRRCS -path from where the file eeds to be uploaded
Finfo = Dinfo.GetFiles("*.*");
//Can be set to *.pdf , *.txt ,*.doc etc
this.lblResults = new Label();
Controls.Add(lblResults);
objSite = SPControl.GetContextSite(Context);
objWeb = objSite.OpenWeb();
objWeb.AllowUnsafeUpdates = true;
SPList objList = objWeb.Lists["Shared Documents"];
SPDocumentLibrary objLib = null;
objLib = (SPDocumentLibrary)objList;
SPFolder rFolder = objLib.RootFolder;
SPFile objFile = null;
byte[] byteArr = null;
foreach (FileInfo f in Finfo)
{
byteArr =
File.ReadAllBytes(@"\\BLRRCS\Reports\" + f.Name + " ");
objFile = rFolder.Files.Add(rFolder.Url + "/" +
f.Name + "", byteArr, true);
}
objFile.Update();
lblResults.Text =
"Files are Uploaded to Shared Document Library"
lblResults.Font.Bold = true;
this.lblResults.ForeColor = System.Drawing.Color.Blue;
}
catch (SPException s)
{
s.ToString();
}
finally
{
objWeb.Dispose();
}
#endregion
}
}
}
<SafeControl
Assembly="WebpartUploadFiles, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=bb6a94ca67a100c4"
Namespace="WebpartUploadFiles" TypeName="*” Safe="True" />
Note: Now go to the command prompt and do RESETIIS (restart IIS server).
CreateChildControls()
method is called. The WebPart WebpartUploadFiles
executes immediately, and displays a message saying "Document is created and Uploaded to RCS Library", as shown below:Improvements to the Web Part
- The path mentioned in the code can also be read from an XML file, and then it can be made flexible to use any local computer path.
- The Web Part works only for the local folder path and uploads all the files present in the local computer folder path.
- The file formats can be set to *.PDF ,*.doc , *.txt etc …
If network files need to be uploaded, then create a batch file as shown below and add the batch file to a Windows scheduled task. Enter the Copy command in Notepad, as shown below, and save it as a .Bat file.
COPY "Network file path" "Local Computer Folder path"
Add the Run.Bat file to a Windows Schedule Task (a Windows Service can also be used), and set the day, time interval when to run the batch file.
Whenever the Windows scheduled task runs this Run.Bat, the files get transferred from the network path \\BLRRCS\Report to the local computer path C:\UploadFiles., thus making network files available locally, and whenever the Share Point Server Shared Documents library menu is clicked, the files will be automatically uploaded to the Shared Documents library.
The below screen shows the uploaded files to the Shared Documents library in SharePoint Server.
I hope the content presented in this article is useful. For any queries, please drop a mail at Rajesh_Sajjanar@hotmail.com.