Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Uploading a file to SharePoint

Posted on 2013-05-16
5
Medium Priority
?
308 Views
Last Modified: 2014-09-30
I have a SQL database where I export one the tables to XML and then use the XML files as Data Source on a few list forms.

I was able to use C# to pull and create the XML from the SQL table, but I'm having issues uploading the file through code.

I've tried Powershell and C# using WebDav. Tried the SharePoint Client Object as well as SharePoint web services.

Unfortunately, I do not have access to SharePoint to make any changes, so I'm stuck putting it together on the client side. I'm the owner of the SharePoint site i'm trying to upload it too.

I've received a 405 Method Not Allowed with WebDav and "The request failed with an empty" response using Web Services. I tried using SPWeb, but I received an invalid url error.

I think one of the problems may be that night site is a subsite with this url and probably security related as well. My office has proxy and web filtering. They use Blue Coat.

http://server.com/sites/subsite/mySite

Any other suggestions on what other methods I can try?
0
Comment
Question by:ajl7519
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
5 Comments
 
LVL 15

Expert Comment

by:colly92002
ID: 39174225
You can do a HTTP PUT command to upload to Sharepoint.
Example here (MOSS version but still relevent with later versions):
http://msdn.microsoft.com/en-us/library/dd902097%28v=office.12%29.aspx

This wont set meta data but you can use the the lists web service for this.
0
 
LVL 1

Author Comment

by:ajl7519
ID: 39175252
Thanks for the reply. Still getting the 405 error. I'm wonding if I need to do via ASP and have it Open IE and run the code when IE loads the page. Just guessing here. My code below modified from the page in a Console project. Was getting a DirectoryNotFound exception so I used a file path instead.

namespace UploadToSharePoint
{
    class Program
    {
        static void Main(string[] args)
        {
            UploadFileToSharePoint();
        }
        static void UploadFileToSharePoint()
        {
            WebResponse response = null;
            string UploadedFilePath = @"C:\Reports\test.xml";
            string SharePointPath = "https://server/list/test.xml";
            try
            {
                // Create a PUT Web request to upload the file.
                WebRequest request = WebRequest.Create(SharePointPath);

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

                // Allocate a 1 KB buffer to transfer the file contents.
                // You can adjust the buffer size as needed, depending on
                // the number and size of files being uploaded.
                byte[] buffer = new byte[1024];

                // Write the contents of the local file to the
                // request stream.
                using (Stream stream = request.GetRequestStream())
                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 (DirectoryNotFoundException e)
            {
               //Result will be: Could not find a part of the path "C:\incorrect\path".
                Console.WriteLine(e.Message); 
                //Result will be: C:\incorrect\path
                Console.WriteLine(e.Message.Replace("Could not find a part of the path \"","").Replace("\".","")); 
            }
            finally
            {
                response.Close();
            }
        }

    }
}

Open in new window

0
 
LVL 1

Accepted Solution

by:
ajl7519 earned 0 total points
ID: 39175646
I found the issue using fiddler. I need to post login credentials (twice actually). Worked for the first one, but not the second.
0

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
When using a search centre, I'm going to show you how to configure Sharepoint's search to only return results from the current site collection. Very useful when using Office 365 with multiple site collections.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question