Wednesday, August 1, 2007

Windows service to publish files programmatically to MOSS 2007

I recently wrote, tested and deployed a .NET 2.0 Windows service to import files into MOSS 2007 sites and document libraries. This service watches a file share (using the FileSystemWatcher class) on another server and moves the file into a MOSS 2007 document library. Here are the salient features:

1. Watches the file share and all folders under that share. Picks up the files as they are created/changed on this share and imports them into MOSS 2007. Deletes the files from the file system after the import. The users are responsible for ensuring that the

2. Accounts for the long copy operations, when the file may not have finished copying when the service goes to pick it up. The way to do that is to open the file in FileShare.None mode and catch the appropriate exception. Wait for a given period of time (few seconds) and retry. Do this the appropriate number of times based on the requirements.

3. Does a clean sweep of the system everytime the service starts to account for the files that it might have missed since the last time it was stopped.

4. Logs the files published/errors to the event log. This helps to track the health of the service - understand what is going right/wrong.

A few things to keep in mind:

1. In the installer class, do not hardcode the password of the account under which the service will run. Do not even read the credentials from a config file. I asked our production environment admins to run the service under a moss service account with enough privileges.

2. Do not do an infinite loop on the number of retries when you cannot get exclusive access to the file. If somebody opens the file and goes home for the day, your service is going to be spinning its wheels trying to get access to that file all night.

3. Review the business case for this requirement. If all that is required is to index these files, you could use MOSS Search to index file shares pretty easily. Also review the business users understand the capabilities of your service, so they dont tax it by creating bogus directories and putting thousands of files in them.

All in all, this service really helps the business because they can automatically import thousands of files into MOSS just by copying them into a file share. I cannot publish the code because the company "owns" that, but hit me up if you need help/advice on how to do this and Ill be glad to guide you.


Luminus said...

Faraz Hi,
I'm working on a project that kinda mirrors what you've done here. I've got the Windows Service working, the FileSystemWatcher detects all actions on the file types I'm watching.

It's doing all the stuff I need it to. The File Deletion Routine is working. All that's left is for me to be able to upload files into the Sharepoint Document Library

I'd really appreciate your help on this one 'cos I'm practically stumped.

You can email me at me+faraz [at] olumidealabi [dot] com

Bruce said...

I ran across your post on your FileWatcher service and we are looking at doing the exact same thing for an image storage project. I'll pass the info in your blog post on to the developers, too bad about the company "owned" code, I hate to see our guys "re-invent" the same wheel. Thanks for the great post.

Faraz said...

Hi Bruce,
Drop me your or your developers email and I will help them out. I spoke with some folks and it should be fine.

David.Jacobus said...

I would Love to see the code! I see lots of uses for this kind of application!

David.Jacobus said...

Hi, I would love to see the code! I can see lots of uses for this kind of application.

Phil said...

I'm trying to create a CAB file for an Infopath 2007 template and publish it to MOSS2007 using a shell script. Can you offer any advice on how to get this file back into my SharePoint library.

Anonymous said...

Any chance I could get a look at the code for your FileWatcher service?

Many thanks in advance

Charlie Smith

Anonymous said...

Faraz, found your blog and am looking at delivering similar (perhaps identical) functionality. Tried to send you email but it bounces. Likewise wondering if your code is something that can be shared? And, if not, any thoughts on how long to write from scratch?

My email is rlw[at]ti[dot]com. Thanks and have a great day.


Thanks and have a great day.

DavidLewis27 said...

I am looking to do almost the same thing except I am not deleting my file once I copy if over from my file share to a document Library. Any help in this matter would be greatly appreciated.

Thanks in advance

Faraz said...

Hey David,
Drop me your email address and I think I can dig up the code.

DavidLewis27 said... thanks I really appreciate it

DavidLewis27 said...

if you are unable to find it just let me know. Thanks

Faraz said...

I haven't been able to find it thus far. Ill keep looking!

DavidLewis27 said...

If you cant find it may be you could just give me some pointers on what to do to code this correctly

Seshubabu said...

Dear All,

As per my analysis I am planning to do same stuff with some changes.

Using .Net windows service I have to do below operations

1.Uploading the documents into sharepoint documents library

2. Updating the custom columns in the same sharepoint document library w.r.t to each uploaded document.

Hope you understands my task. Could you please guide me to start above tasks from the scratch.

My email :

Your help is highly appreciated..


Asim said...

Hi Faraz,

I am trying to achieve a similar functionality --- uploading files from a folder to specific subfolders within a sharepoint doc library.

Coud you give me some direction/ access to your code.


Anonymous said...

Hi Faraz,

I am trying to achieve a similar functionality --- uploading files from a folder to specific subfolders within a sharepoint doc library.

Coud you give me some direction/ access to your code.


Dutzu said...

Hy...please help...I have done something similar....only I monitor not only a local or network shared folder, but I can also monitor an FTP account or an e-mail account and get files attached to e-mails.....

But I can't seem to get it when it comes to uploading to a Sharepoint Document Library....cand you please give me the code to upload a file to a document library....It would be great if it were in a project...

Could you please make and give me a small "proof of concept" like project that uploads a file to a document library???

James Rhodes said...

After an entire day of Google searches, it appears you are the only one to have written this and wrote about it!

Would love to talk to you about this item, but I am more of a admin than a scripter.. :)

Tessa said...

As the previous comment says - after a day of hunting this is the only post that I can find about this. This is exactly what I need to do and I have no clue where to start. Any help would be appreciated. tessahanekom[at] g m a i l [dot]com

Anonymous said...

directive manger gland registering shampoo alert designed latin woodwind excellently herbaceous
servimundos melifermuly