Joeygurl’s Trial & Errors with Possible Solutions











I’ve been having this dilemna on how to programmatically add a document (in my case an infopath form) to a SharePoint document library. I’ve seriously searched all over google using all the jargons I could think of regarding this matter. It would’ve been easier if my development computer was a Windows Server 2003 OS because it would be as easy as using the “SharePoint.dll” classes that came with WSS. But since I was given XP OS to work with, I had to find ways to make things happen magically at work. Without the power of “SharePoint.dll”, I had to remotely connect to the SharePoint development server and utilize the built-in Web Services it came with to manipulate the SharePoint Site.

So after aeons of research, I finally came across this guy – Namwar’s blog:

http://sharepointinsight.wordpress.com/2009/01/10/programmatically-upload-a-file-to-document-library/

In case, he decides to cancel his blog site, I’ll paste the solution here:

Following is a utility function which you can use to upload a file programmatically in SharePoint document library. It has two parameters. First is the source file path and second is the target document library path.

Following is an example call to this function:

UploadFileToDocumentLibrary(@”C:\test.txt”, @”http://home-vs/Shared Documents/textfile.txt”);

and here is the function

public static bool UploadFileToDocumentLibrary(string sourceFilePath, string targetDocumentLibraryPath)
{
//Flag to indicate whether file was uploaded successfuly or not
bool isUploaded = true;
try
{
// Create a PUT Web request to upload the file.
WebRequest request = WebRequest.Create(targetDocumentLibraryPath);
//Set credentials of the current security context
request.Credentials = CredentialCache.DefaultCredentials;
request.Method = “PUT”;
// Create buffer to transfer file
byte[] fileBuffer = new byte[1024];
// Write the contents of the local file to the request stream.
using (Stream stream = request.GetRequestStream())
{
//Load the content from local file to stream
using (FileStream fsWorkbook = File.Open(sourceFilePath, FileMode.Open, FileAccess.Read))
{
//Get the start point
int startBuffer = fsWorkbook.Read(fileBuffer, 0, fileBuffer.Length);
for (int i = startBuffer; i > 0; i = fsWorkbook.Read(fileBuffer, 0, fileBuffer.Length))
{
stream.Write(fileBuffer, 0, i);
}
}
stream.Close();
}
// Perform the PUT request
WebResponse response = request.GetResponse();
//Close response
response.Close();
}
catch (Exception ex)
{
//Set the flag to indiacte failure in uploading
isUploaded = false;
}
//Return the final upload status
return isUploaded;
}

I actually had to re-define some lines because in my case, I had to grab the sourceFile from a URL and not from a local directory. He used “FileStream” which worked for files locally so I basically had to use “Stream” and WebClient. You’ll need to add the namespace “System.Net” so you can use WebClient. Below is my updated code:

public static bool UploadFileToDocumentLibrary(string sourceFilePath, string targetDocumentLibraryPath)
{
//Flag to indicate whether file was uploaded successfuly or not
bool isUploaded = true;
try
{
// Create a PUT Web request to upload the file.
WebRequest request = WebRequest.Create(targetDocumentLibraryPath);
//Set credentials of the current security context
request.Credentials = CredentialCache.DefaultCredentials;
request.Method = “PUT”;
// Create buffer to transfer file
byte[] fileBuffer = new byte[1024];
// Write the contents of the local file to the request stream.
using (Stream stream = request.GetRequestStream())
{
//Load the content from local file to stream
WebClient wc = new WebClient();
wc.Credentials = new NetworkCredential(UserName, Password, Domain);
using (Stream fsWorkbook = wc.OpenRead(sourceFilePath))
{
//Get the start point
int startBuffer = fsWorkbook.Read(fileBuffer, 0, fileBuffer.Length);
for (int i = startBuffer; i > 0; i = fsWorkbook.Read(fileBuffer, 0, fileBuffer.Length))
{
stream.Write(fileBuffer, 0, i);
}
}
stream.Close();
}
// Perform the PUT request
WebResponse response = request.GetResponse();
//Close response
response.Close();
}
catch (Exception ex)
{
//Set the flag to indiacte failure in uploading
isUploaded = false;
}
//Return the final upload status
return isUploaded;
}

I hope this helps you guys! :-)



Leave a Reply

et cetera