Sharepoint Forum

Ask Question   UnAnswered
Home » Forum » Sharepoint       RSS Feeds

Creating file programatically, and uploading to doc lib?

  Asked By: Sarojini    Date: Feb 20    Category: Sharepoint    Views: 4953

I am trying to create a file in memory and upload the file to a
document library.

I am able to read an existing text file from a doc lib, but can't
figure out how to create and upload new text file.

This is how i read the file:

SPFile inputFile = properties.ListItem.File;
Stream inputStream =

string HandledMessage = null;
StreamReader fileReader = new StreamReader(inputStream);

string input;
while ((input = fileReader.ReadLine()) != null)
HandledMessage = HandledMessage + input + "/n" ;
properties.ErrorMessage = HandledMessage;

Anyone know how to create a new file and upload?



5 Answers Found

Answer #1    Answered By: Rebecca Lewis     Answered On: Feb 20

Take a look at the wsssdk.chm...........

Answer #2    Answered By: Emily Clark     Answered On: Feb 20

Here is the private method that I use to add images from the 12 Hive to
an Image Library:

private static void AddImageToList(SPList list, string  imageName)
// Add an image from the 12 Hive to the
string imagePath = @"C:\Program Files\Common Files\Microsoft
Shared\web server extensions\12\TEMPLATE\IMAGES\";

FileStream stream = File.OpenRead(imagePath + imageName);

byte[] content = new byte[stream.Length];
stream.Read(content, 0, (int)stream.Length);

list.RootFolder.Files.Add(list.RootFolder.Url +
"/" + imageName, content, true);
// Do nothing if image is not found

Answer #3    Answered By: Alycia Everett     Answered On: Feb 20

The issue I am having is more that the file  does not exist. I want
to create  the file (in memory  if possible as production enviroment
trust level does give me access to file system), and then upload  it
to a document library. The upload part is straight forward. I just
can't figure out how to create the file in memory and then get a
handle on it to pass to sharepoint for upload. More of a C# question
than sharepoint.

Will wss_minimal give me read/write to the 12 hive? if so i could
write the file out to there, upload from there to doc  library, and
call some clean up to delete the temp file.

Answer #4    Answered By: Kaila Hahn     Answered On: Feb 20

I am doing this within an Outlook Addin that stores the *.msg file  from
outlook - I create  a temp dir somewhere on the harddrive where the user
account can access the file. Then I push the file to the Sharepoint
document library, then clean up the file (delete the file & directories)
from the harddrive. This has worked well on several product implementation
without issue.

Answer #5    Answered By: Ada Sosa     Answered On: Feb 20

The Add method (list.RootFolder.Files.Add) is passed a stream object.
So, you can create  a stream object, fill it using a stream writer and
then pass it to the Add method.

Storage on disk is not required.

Didn't find what you were looking for? Find more on Creating file programatically, and uploading to doc lib? Or get search suggestion and latest updates.