My question is regarind adding a new item to document library programmatically.

Basically, when I add an item to document library it goes as follows: In a document library i click add new and i choose one of the templates basically soemthing like is it a work document or excel document. In this first window I just give the path to the file (using browse button) and i click next.

Then i'm redirected to the next dialog, with a lof of fields which i have to fill in and click ok.

My question now, how to do the same programmatically?

I think it will be a bit different ...

Any ideas?

2 Answers 2


So as @diptarag said there are post of this all over the place, but I guess I can take a crack at giving you what you want.

Add is pretty simple, if you have a valid SPWeb object you can do...

SPFile file = web.Files.Add(<URL to File>,<bytes>,<Overwrite>);

So you build the exact path to where you are placing your file. So if you are placing your file in Share Docs you can do http://yoursite.com/Shared Documents/myfile.txt. There the file will be named myfile.txt and placed in shared docs. So now the file should be returned as a SPFile object from the add call. To set a content type you must get the content type from the web you are on.

SPContentType contentType = web.ContentType[<Content Type Name>];

Then you can set the content type ID of the list item to that content type's ID. You Also need to get the SPItem of the SPFile.

SPListItem item = file.ListItemAllFields;
item["Content Type ID"] = contentType.Id;

Finally to set a metadata field(Column), do this...

item[<Field Name>] = <Value>;
//Set More Fields....

And that is it. Remember the field names must be valid and the content type you use must be on the list you are adding the document to.


There are already many online posts regarding this, try this stackoverflow post, let me know if you face any issue

  • yeah i know this, but it is not exactly what i need, i have to set also a lot of extra information as it has a different content type ....
    – Shkipper
    Nov 29, 2012 at 12:24
  • have you tried any sample code? can you post it?
    – Diptarag
    Nov 29, 2012 at 12:30
  • And i have document library why are they downloading to spfolder?
    – Shkipper
    Nov 29, 2012 at 12:32
  • To add files you need to access the spfolder object, you cant do it with splistitem but to add various fields you need SPListItem, so you can use spfile.Item to get the SPListItem associated with the file and fill in other custom fields, but you are saying something about different content type that i am not getting
    – Diptarag
    Nov 29, 2012 at 12:41
  • For example if i add a docx then i have some like 2 extra fields to fill in and if i upload a txt file than i have to fill in 5 fields, because they are assosiated with different content types. Because in the document library when i click new i have to choose between those content types so to speak
    – Shkipper
    Nov 29, 2012 at 12:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.