Ben 7 May 2007

I'm a Newbie to sharepoint so bear with me.

I'm using active server pages (forms) which talk to a backend process written in c# to then create document libraries on share point.

We've then managed to find and hack some code that allows us to insert a word document into a folder within the document library we've just created.

Here's a snippet of the code that does this last bit I mentioned:

string templateName = "xyz.doc";

SPDocumentLibrary doclib = null;

SPFile template = null;

doclib = (SPDocumentLibrary)web.Lists[guid];

SPFolder formsFolder = doclib.RootFolder.SubFolders["Forms"];

byte[] doc = System.IO.File.ReadAllBytes(string.Format(@"c:\{0}", templateName));

template = formsFolder.Files.Add(templateName, doc);

It inserts the document called xyz.doc into the subfolder called Forms within a newly created document library. To view the file within sharepoint I click on documents then click on the newly created document library, then click on the actions menu and select the option "open with windows explorer". I then double click on the Forms folder and I can see the x.doc file amoung other files.

What we'd like to do is see x.doc within the root of the newly created document library. In other words I click on the documents link then click on the newly created document library we should be able to see it immediately.

We've tried changing the line

SPFolder formsFolder = doclib.RootFolder.SubFolders["Forms"];


SPFolder formsFolder = doclib.RootFolder;

but we just don't see the file xyz.doc at all.

We've looked on the web for a while for code snippets that would do the job. None are working for us.

I'm finding it alittle difficult to find the documentation and/or examples to do what we want to do in sharepoint, so accompanying code snippets are appreciated.



Re: SharePoint - Development and Programming Document placement within Sharepoint

Rich McGuire

I have gotten it to work using code very similar (my code is given below), the only major difference is that I copy the template for the document library into a new file and insert it into the document library rather than from the hard drive as you are doing. Is the xyz.doc being created at all or are you having an error thrown perhaps you aren't making an Update() call which you should be Anyways here is the code which I have used:

Code Snippet

SPDocumentLibrary myDocLib = (SPDocumentLibrary)myWeb.Lists["Name of document library"];
SPFolder myFolder = myDocLib.RootFolder;
SPFile myFile = myWeb.GetFile(myDocLib.DocumentTemplateUrl);
byte[] binFile = myFile.OpenBinary();

SPFile report = myFolder.Files.Add(fileName, binFile, true);

Hope this helps point you in the right direction.

Re: SharePoint - Development and Programming Document placement within Sharepoint

Ben 7 May 2007

Hi Rich,

Firstly wanted to say thanks.

I transplated the code and essentially it does the same thing. (The difference between my code and your code was as you said , one sourced the file from the harddrive the other had sourced the file from document templates.)

The both were able to write to the document libraries Forms and Documents subfolders... BUT not the root of the new document library.

When I mean doesn't work, I mean that no error occurs, I just can't see the word document that I placed at the root of the new document library.

I tried the Update call but unfortunately it didn't work either.

Heres a link the our site with just a document library but the word document doesn't appear where I'd like it:

Link A

Heres a link to our site with the word doucment appearing in the correct place.

Link B

Help would be appreciated!!! Smile

Re: SharePoint - Development and Programming Document placement within Sharepoint

Ben 7 May 2007

Incase anybody is interested the missing piece of the puzzle was the line

formsFolder.Files.Add(NewTemplateName, doc, true).CheckIn("");

I didn't realise I had to add code to "Check in" the document after I had added the file to sharepoint.

formsfolder is set to the document root ie

SPFolder formsFolder = doclib.RootFolder;

The assistance was much appreciated. Smile