We are exploring SharePoint 2007 from last few days. Here is the code for adding new document to document library using C#. I have created a page with a file upload control and a button. When user clicks on button the selected document will be uploaded to specific document library. You need to add reference to Microsoft.SharePoint DLL to make this code running.
if (FileUpload1.PostedFile != null)
{
if(FileUpload1.PostedFile.ContentLength > 0)
{
System.IO.Stream strm = FileUpload1.PostedFile.InputStream;
byte[] byt = new byte[
Convert.ToInt32 (FileUpload1.PostedFile.ContentLength)];
strm.Read(byt, 0, Convert.ToInt32
(FileUpload1.PostedFile.ContentLength));
strm.Close();
// Open site where document library is created.
using (SPSite objSite = new SPSite(“http://server:port/”))
{
using (SPWeb objWeb = objSite.OpenWeb())
{
SPFolder mylibrary = objWeb.Folders[“Name of Document Library”];
// Set AllowUnsafeUpdates = true to avoid security error
objWeb.AllowUnsafeUpdates = true;
mylibrary.Files.Add(System.IO.Path.GetFileName
(FileUpload1.PostedFile.FileName), byt);
}
}
}
}
Above code first reads the uploaded file in Stream and than uploads it on document library.
Happy Programming !!!
Because of your work, so many people can work together, so many people have discover each other than you open a new world of communication between people.
Dear Friends,
Thanks for your code. I need more code for using sharepoint with c#, Pls help me..
Thanks
Gnanavel B
Wow……you can read MSDN too (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/spptsdk/html/tsptUploadLocalFile_SV01038710.asp)
Who’s code are you going to plagarise next??
#3 – can’t see the link u’ve posted — but I saw it on this page source:
http://msdn.microsoft.com/library/default.asp?url=/
library/en-us/spptsdk/html/
tsptUploadLocalFile_SV01038710.asp
you have saved me a lot of time. AGAIN. thanks, jim
Thank you My friend for your code,
but i have a little problem…
after adding this document, how can i add details for specific columns in this Document Library, for example the title or the name of this Document???
doin such things with M-API is always an easy task, but this approach compells server to have licensed sharepoint.
can you do it using web services… so that all servers can use it ?
I have created some code to basically do the same thing but I’m having an issue applying this app on a client computers/non server comps using 2003 server. It is giving me an error stating the sharepoint.dll can not be found. Can you help me out with that? Again it works on the server that I created it on but not Client PC’s.
Gaston,
Is Sharepoint Server installed on your machine? Is Sharepoint Server is installed on client’s machine? If you are using object model instead of webservice model for programming then sharepoint must be installed on machine on which programming is running.
Hope this will help you.
The code give by you is nice……………………..
Nice work done
Thanks
Hi
I am Raju .
I want asp.net c# code for creating custom pages by user
like sites.Google.com
And the fields?¿ if I want upload a image jpg and the values of fields?
I want to create PDF output file .. how can i create PDF output in C# Windows Applications .Net 2.0.
Hi Chirag,
I want to uplod modified document to new version of that document…in coding…
Thankx
I have 3 subsists in top level site ex.Subsites-> Admin,Student,teacher
And I have 3 domain users also ex domainname\admin1, domainname\Student1, domainname\Teacher1
If I have a Document library in Admin site, and want to use in Teacher and student. So how can I do it.
Problem:- Admin and Teacher can upload document but Student can download only.
1 Document library accessing multiple sites with download and upload features depends upon credentials
So please give me solution thank you
I want to upload multiple documents to a document library. Suppose using file upload control, I want to point a folder and all the files in the folder should be upload to a doc library.
Is it possible for this scenario.
Please give the solution for this
thanks
HI Folks,
Thanks a lot….
when ever i need the code on sharepoint i wil post plz help me…
Hi,
Can i add Document library in Sharepoint 2007 using C# code?
Thnx in advance
You explained a way. But you forgot to mention how to add it’s metadata [column information] along with document. Here is the post, which explains everything.
http://praveenbattula.blogspot.com/2009/05/adding-list-item-entry-to-document.html
And along with that, explains the problems which comes while adding column data with HashTable and how we can solve it here.
http://praveenbattula.blogspot.com/2009/06/exception-only-string-int-and-datetime.html
thanks
Hi,
In the above code you adding the documents into the Document Library but i want to know
how to create a new document library through C# coding?
Niek – doesn’t matter if chiragrdarji wrote the code or not, the fact is chiragrdarji provided us a way to get the answer. Trust me when you’re in a hurry you just care about the answer.
keep posting
hi,
I need to run word macro on every new file uploaded on my site. The macros are stored currently in a local file.
I can use your provided code for uploading file but running macro i don’t know how to do it. The result of this macro will be stored in sharepoint site list. Can we write code to update sharepoint list?
please tell me how to work around this? I can create a web part if required but i don’t know what all is required for it?
plese tell me.
I need to upload documents into SharePoint Documnet Library using javascript. How to do it?? Can any one help me out??
Hi,
pls help me ,very urgent
how can add all field values(like title,Name,documentID columns),for the same newly uploaded file in document Library
Thank you for the very useful code example!
You can just use:
folder.Files.Add(System.IO.Path.GetFileName(Attachment.PostedFile.FileName), Attachment.PostedFile.InputStream);
No need to struggle with Stream objects. Cheers!
Thanks Chirag it works for me as well.
-Dhawal
Could you let me know how to retreive the documents (doc/pdf) from sharepoint document library?
Сервис Failo-Obmennik.Ru программа обменник и музыка хостинг файлов, хранение файлов. Бесплатный файлообменник и хостинг файлов. Место под хранение файлов не ограничено. Скорость передачи файлов до 1 Гб/сек. Быстрое скачивание файлов в несколько потоков. Загрузка через браузер, по FTP и нескольких файлов сразу. Храните файлы в нашем файлообменнике. файлообменник, бесплатные файлообменники, обмен файлами, передача файлов, файловый обменник, хранение файлов. Сайт failo-obmennik.ru
Hello All,
First thanks for your helping us with this useful code, actually I need to create doc library with scanner, so u can scan and automatic upload the scanned doc to the library, so plz if u got any solution inform me.
Regards
hi all,
gr8 work , i wannt to copy versions of document library item programmatically
Thanks for your help! I hope this works in WSS 2.0 (sharepoint 2003) as well
Why do you all have white tape on your fingers???
עבודה בחול
IOPAI73948IOPP
Thanks for news.
Yeah! It works!
Don´t forget impersonation and to allowUnsafeUpdates…
Thanks!
i created on document library in sharepoint 2007 .this document library content many file . how to downloadthis file throght the sharepoint 2007
hi friend
i want to parse Resume doc file such as name contact no date of birth, key skill and email id and store the data into database i open the doc file and read the file one text box after then what i can do parsing the resume. plz help me
hi friend
it is c# windows application
i want to parse Resume doc file such as name contact no date of birth, key skill and email id and store the data into database i open the doc file and read the file one text box after then what i can do parsing the resume. plz help me
what the hell is this??
shankar.narayanan@tcs.com
Hi ,
Thanks for nice post.
I want to add user information to document library using c# coding, I mean i created two columns in document library as UploadedBy, Emailid. So i want to save these details to document library using c# coding along with document. Can you tell me any solution for this?
Regards,
Rajesh
Chirag,
I want to upload XML files from a VM server to SharePoint server. this has to be automated(I am thinking of running an exe in windows scheduler), so every time a new file comes on to the VM, that should be uploaded on SharePoint.
I need the SharePoint to be updated, if any changes occur in the same file on VM.
I just have a rough idea for it, could you please help me with the code, I desperately need it.
Helpful info. Lucky me I found your web site by chance, and I’m stunned why this coincidence did not came about in advance! I bookmarked it.
An interesting discussion is definitely worth comment.
I think that you should publish more on this subject, it may not be a taboo subject but usually folks don’t discuss these topics. To the next! Many thanks!!
Can anyone here please suggest a good title for my weblog associated
with running / marathon?
I always wondered about things like this, thanks
How to Add .msg file to Document Library in SharePoint using C#? Please Help Me.
Thank you
Howdy just wanted to give you a quick heads up. The words in your post seem
to be running off the screen in Internet explorer.
I’m not sure if this is a formatting issue or something to do with web browser compatibility but I thought I’d post to let you
know. The design and style look great though! Hope you get the issue
resolved soon. Thanks
This is a good tip particularly to those fresh to the blogosphere.
Short but very precise info Many thanks for sharing
this one. A must read post!
Asking questions are in fact nice thing if you are not understanding something
totally, but this post gives good understanding yet.
What’s up, after reading this remarkable piece of writing i am too delighted to share my familiarity here with mates.
I want to get one of these, but my mother won’t
let me!, annoying
It’s fantastic that you are getting thoughts from this piece of writing as well as from our dialogue made at this time.
Hmm is anyone else having problems with the pictures on this blog loading?
I’m trying to determine if its a problem on my end or if it’s the blog.
Any responses would be greatly appreciated.
I believe what you posted was actually very reasonable.
But, think on this, suppose you added a little content? I ain’t suggesting your content isn’t solid, but suppose
you added a post title that makes people want more?
I mean Add new document to Document Library in SharePoint using C# Tech Treasure
is kinda plain. You could glance at Yahoo’s front page and see how they create news titles to grab viewers to click. You might add a related video or a related picture or two to grab readers excited about everything’ve written.
In my opinion, it could bring your website a little livelier.
Hey! This post couldn’t be written any better! Reading through this post reminds me of my previous room mate! He always kept talking about this. I will forward this article to him. Pretty sure he will have a good read. Thank you for sharing!
Hi there just wanted to give you a quick heads up and let
you know a few of the pictures aren’t loading correctly. I’m not sure why but I think its a linking issue.
I’ve tried it in two different browsers and both show the same outcome.
Hi! Do you know if they make any plugins to safeguard against hackers?
I’m kinda paranoid about losing everything I’ve worked hard on. Any suggestions?
Hmm is anyone else experiencing problems with the pictures on this blog loading?
I’m trying to figure out if its a problem on my
end or if it’s the blog. Any responses would be greatly appreciated.
Good write-up. I absolutely appreciate this website. Keep writing!
Spot on with this write-up, I truly believe that this web site needs far more attention.
I’ll probably be returning to read more, thanks for the info!
If you wish for to take a good deal from this piece of writing then you have
to apply such strategies to your won website.