Monday, January 11, 2010

SharePoint Event Handler Tutorial 2: Download Document to Local Folder

Someone recently asked how to add SharePoint event handler code which will download a file from a document library to a local NTFS folder every time a document is uploaded into a SharePoint document library.
The solution is actually very easy.
1-Create a new Event Handler on a SharePoint Document Library (ItemEventReceiver). To do this, please follow my tutorial available here.
2-Ensure that your basic event handler as per the turorial in step 1 is working and that you can debug (step through) the code.
3-Now you are ready to add the code to save a document to a local folder. Extend the tutorial code in the following way:
    3.1-Add the following using statements: (remember this is C#)
           using System.IO;

           using System.Text;
    3.2-Add the following code inside the ItemAdded event so that the complete event look like below:
public override void ItemAdded(SPItemEventProperties properties)

{
StringBuilder strWFBuilderCheck = new StringBuilder();
strWFBuilderCheck.Append(@"C:\TestFolder\");
strWFBuilderCheck.Append(DateTime.Now.Date.Year.ToString());
strWFBuilderCheck.Append(DateTime.Now.Date.Month.ToString());
strWFBuilderCheck.Append(DateTime.Now.Date.Day.ToString());
strWFBuilderCheck.Append("_");
strWFBuilderCheck.Append(DateTime.Now.TimeOfDay.Hours.ToString());
strWFBuilderCheck.Append(DateTime.Now.TimeOfDay.Minutes.ToString());
strWFBuilderCheck.Append(DateTime.Now.TimeOfDay.Seconds.ToString());
strWFBuilderCheck.Append("_");
strWFBuilderCheck.Append(properties.ListItem.File.Name);

string WorkfFileLocationcheck;
WorkfFileLocationcheck = strWFBuilderCheck.ToString();


BinaryWriter binWritercheck = new BinaryWriter(File.Open(WorkfFileLocationcheck, FileMode.Create));
byte[] itembytescheck = properties.ListItem.File.OpenBinary();
string itemstringcheck = Encoding.ASCII.GetString(itembytescheck);
System.Text.ASCIIEncoding encodingcheck = new System.Text.ASCIIEncoding();
byte[] bytescheck = encodingcheck.GetBytes(itemstringcheck);


binWritercheck.Write(itembytescheck);


binWritercheck.Close();
}


Build your solution
Deploy the solution
Create a local folder called "C:\TestFolder\"
Refresh your SharePoint website
Add a breakpoint to the code
Attach the code to a process (w3wp)
Go to a document library and upload a document...you will see the document is also written to a local folder.
The example will also work when uploading multiple documents as well as when you open a document library in explorer view and then copy and paste (or drag) files into the folder.
 
Shout if you need any help !!
 
Enjoy !!
 

2 comments:

Mylsamy said...

Hi,

If i upload the document from my local machine,that document is downloading in the server path. It's not downloading in my local machine. I have copied the same above code. Is anything missing from my side. ?

Anonymous said...

Johan, what can I look for if I followed your instructions ver bateum and the file is not being downloaded. Please advise, as your blog is the closest thing I need in completing my project.
thx

Post a Comment