Click here to Skip to main content
15,877,618 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: (untagged)
I am trying to upload XML files from a VM server to a Sharepoint url(I am trying to upload under shared documents in a library )

Note: I have access to the VM server, I have access to upload files manually on the SharePoint site.
but I need this to be automated, so that as soon as a new XML file arrives on the VM server, the file should get uploaded on the SharePoint

I have developed a web application page using visual studio 2010
Followed the step below

1.Creating an ASP.NET Web application in Microsoft Visual Studio 2010
2.Importing the System.IO namespace and the System.Net namespace
3.Creating the file upload form
4.Adding the upload code to the solution

I diligently followed the MSDN link below:
http://msdn.microsoft.com/en-us/library/dd902097(v=office.12).aspx[^]

However whenever I try to upload a file[tried uploading XML, doc, pdf(my requirment to upload XML)]
I get the following error
"Object reference not set to an instance of an object"

Questions:
1.Please review the code and I appreciate any help for resolving the error.
2.After resolving this error, could any one please tell me what security measures I need to take for secure file upload.




XML
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Fileupload.aspx.cs" Inherits="shera.Fileupload" %>
<%@ Import Namespace="System.Net" %>
<%@ Import Namespace="System.IO" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script runat="server">

public void Button1_Click(object sender, EventArgs e)
{
    // This is a source path, which can be fixed if we create this project in console application,
    //currently it can be browsed from the webpage
    // The path mentioned in the "string uploadedFilePath" should be the VM server's path where XML files are stored
    string uploadedFilePath = @"C:\\Users\\Documents\\project files\\XML samples";
    // This is the destination where the file need to be sent.
    // Note :  I have access to this url for file upload
       string sharePointListPath =" Sharepoint url(I am trying to upload under shared documents in a library )"
       

    if (FileUpload1.HasFile)
        try
        {
           // save the file path and file name
            FileUpload1.SaveAs(
                uploadedFilePath + FileUpload1.FileName);

            // naming conventions for the file which is getting uploaded

            Label1.Text = "File name: " +
                 FileUpload1.PostedFile.FileName + "<br>" +
                 FileUpload1.PostedFile.ContentLength + " bytes<br>" +
                 "Content type: " +
                 FileUpload1.PostedFile.ContentType;

            // Upload the file to the sharepoint server following the upload pattern as follows:
            UploadFileToSharePoint(
                uploadedFilePath + FileUpload1.FileName, // file path and file name
                sharePointListPath + FileUpload1.FileName);  //destination path i.e sharepoint shared documents
        }
        catch (Exception ex)
        {
            Label1.Text = "ERROR: " + ex.Message.ToString();
        }
    else
    {
        // print this message if upload button is clicked and the file is not selected to upload
        Label1.Text = "You have not specified a file.";
    }
}

// method created for file upload and and path of the source
public void UploadFileToSharePoint(string UploadedFilePath,
    string SharePointPath)
{
    // Initially provide a null response from the the destination response URL
    WebResponse response = null;

    try
    {

        /*HTTP defines nine methods indicating the desired action to be performed
        on the identified resource.This procces is execute by creating a PUT Web request to upload the file.*/

        // The destination URL is requested to make a path for the file transfer by creating an instance
        WebRequest request = WebRequest.Create(SharePointPath);

        // If required by the server, set the credentials.

        /* DefaultCredentials represents the system credentials for the current security context
        in which the application is running.usually the Windows credentials (user name, password, and domain) of the user
        running the application. For ASP.NET applications, the default credentials are the user credentials
        of the logged-in user, or the user being impersonated*/

        request.Credentials = CredentialCache.DefaultCredentials;
        request.Method = "PUT";

        /*A buffer size has been allocated to transfer file contents
        which can manupulated as per the number and file size which is getting uploaded */
        byte[] buffer = new byte[1024];

        // Write the contents of the local file to the request stream.
        using (Stream stream = request.GetRequestStream())

        //Initializes a new instance of the FileStream class with the specified path, creation mode, and read/write permission.
        using (FileStream fsWorkbook = File.Open(UploadedFilePath, FileMode.Open, FileAccess.Read))
        {
            int i = fsWorkbook.Read(buffer, 0, buffer.Length);


            while (i > 0)
            {
                stream.Write(buffer, 0, i);
                i = fsWorkbook.Read(buffer, 0, buffer.Length);
            }
        }

        // Make the PUT request.
        response = request.GetResponse();
    }
    catch (Exception ex)
    {
        throw ex;
    }
    finally
    {
        response.Close();
    }
}

</script>
</head>
<body>
    <form id="form1" runat="server">
<div>
<asp:FileUpload ID="FileUpload1" runat="server" Width = "500px" />
<br />
<br />
<br />
<asp:Button ID="UploadButton" runat="server" OnClick="Button1_Click"
    Text="Upload File" />
<br />
<asp:Label ID="Label1" runat="server"></asp:Label>
</div>
</form>
</body>
</html>
Posted
Updated 21-Jul-11 5:13am
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900