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.
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
- Add the following references by right clicking on the WebpartUploadFiles project and clicking on “Add Reference”: Microsoft.SharePoint and Microsoft.SharePoint.WebControls.
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.
- Use the
Render method: 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. Create a
#region File Information
Build the project to get WebpartUploadFiles.dll.
Deploying the Web Part to SharePoint: Now we need to deploy the Web Part in the SharePoint Server. To let SharePoint know that the assembly is safe, we have to add it to the safe control list in the web.config of the SharePoint web. We need the public key token from the DLL for this. For this, follow the step mentioned below.
Build the project. Once built successfully, go to Properties by right clicking the WebpartUploadFiles project and clicking on Properties. Open the project properties and go to the ‘Signing’ section. Create a new key and build the project. A Strong Key is created in the project as shown:
Now drag and drop the following compiled assembly into the GAC (by default, c:\windows\assembly). As shown below:
Open the properties of the DLL to get the public key token by right clicking on WebpartUploadFiles in the GAC.
Now register the assembly as a safe control in web.config.
Namespace="WebpartUploadFiles" TypeName="*” Safe="True" />
Note: Now go to the command prompt and do RESETIIS (restart IIS server).
In SharePoint 2007 Server, once a WebpartUploadFiles.dll is registered in the web.config, SharePoint can automatically detect the WebPart from those DLLs. With the site settings page, we can now automatically populate the WebPart gallery. In SharePoint, go to he HOME site and click 'Site Actions' -> 'Site Settings' -> and, then under ‘Modify All Site Settings’:
Under ‘Galleries’, click ‘Web Parts’. Then click ‘New’.
Now, the new WebPart(s) will appear in the list. If not, try an IISRESET and refresh the page.
Click on New.
Select the WebPart as shown below:
Click on Populate Gallery.
Now, we can go to the page in SharePoint where you want to add the WebPart. On the SharePoint site, click ‘Site Actions’ -> ‘Edit Page’ -> click on ‘Add a WebPart’ -> select the WebPart to add, and click ‘Add’.
The WebPart is added on the page where the
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:
Each time you click on the Shared Documents link, the WebPart executes and all files present in the path \\BLRRCS\Reports\ which is mentioned in the code get uploaded to the Shared Documents library.
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.
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 file formats can be set to *.PDF ,*.doc , *.txt etc …
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.