Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

c# code to add document to document library

Status
Not open for further replies.

kennedyeoin

Programmer
Jul 16, 2006
15
GB
Hi,

I am trying to write c# code to add a new document to a document library. My current code is

SPDocumentLibrary contracts = (SPDocumentLibrary)mySite.Lists["Contracts"];

SPList contracts = mySite.Lists["Contracts"];

SPListItem newContract = contracts.Items.Add();

newContract.Update();

this throws an error saying to add an item to a file library use spFileCollection.Add.

I have tried using spFileCollection but it seems to be for uploading a document, I want to create a new doucment

Any help on this would be greatly appreciated.

Thanks
 
You'll probably have to create the document first, using the Office/Word object model, then upload the "new" document to the library.

Phil Hegedusich
Senior Programmer/Analyst
IIMAK
-----------
I'll have the roast duck with the mango salsa.
 
Addendum:

Or, just use the Office model and the .SaveAs action can point to the Web folder identity of the document library.

Phil Hegedusich
Senior Programmer/Analyst
IIMAK
-----------
I'll have the roast duck with the mango salsa.
 
Hi Addendum,

Thanks very much,

Could you please give me an example of code that would implement this. I am unable to see how you point to where the document should be saved with the SaveAs action.

Thanks again
 
You should be able to see your document library as a Web folder in your LAN. You should be able to treat it as any other network location in the SaveAs action. If you can't see the Web folder, the client needs the Web Client service activated and permissions to the doc library.

Phil Hegedusich
Senior Programmer/Analyst
IIMAK
-----------
I'll have the roast duck with the mango salsa.
 
Hi Phil,

Using save as works perfect thanks, one last question,

I have a document template set up for this library.
Is there a way that I can have this newly created document use the document template that I have set for my Library?
 
In case anybody else has the same problem. The save As like Phil said is perfect for adding a new document to a document library.

If you want this document to have a certain temoplate then firstly use the open method to open the template then use the save as to save it.

Thanks very much for your help on this one Phil,

Cheers
 
Just remember that you must have the Web Client service activated on the running machine to use the Web folders. Older clients may not be able to do this.

Phil Hegedusich
Senior Programmer/Analyst
IIMAK
-----------
I'll have the roast duck with the mango salsa.
 
Hi again Phil, You said that you must have the Web Client service activated. How do you do this?

Thanks again
 
Let me be a bit more specific with my problem.
I have written code for creating a new contract in a windows application to open a word document and then save it as a new name. This workd perfect.

However I tried to put my code into a workflow and when I try to run the code it breaks trying to open the file saying unable to find file.

Any help would be appreciated
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top