Copy file to Sharepoint-share fails unless user connects to Sharepoint server for the first time
Asked Answered
K

1

6

I have a strange behaviour when I try to use a C# program to copy local files to a Sharepoint Server using UNC paths provided by Sharepoint to access the file system. First of all, my user has all the privileges required to access that specific Sharepoint-folder.

This is what my operation basically looks like:

string targetSharepointPath = @"\\team.mycompany.com@SSL\DavWWWRoot\team\wmscompanydep\Software Releases\MyToolConfig"
System.IO.File.Copy(sourcePath, targetSharepointPath, false);

This fails with an error "The network path was not found."

As soon as I copy the path above and paste it into WIndows File Explorer (not internet explorer, this is simply an UNC path), everything works.

So my assumption is, that in the background, Windows explorer does a little bit more. But what? I do not have to enter any credentials, the targetSharepointPath simply works in explorer, and as soon as this was entered once, it also works in my C# program. Until I restart my system, I have to repeat that step. Why, and how can I achieve that programatically? I work with UNC paths on "normal" Windows servers a lot, and once the user has rights I do not need any additional authentication.

Kuhlman answered 21/6, 2016 at 10:52 Comment(0)
T
7

To connect to a Sharepoint you need a windows service called WebClient.

When you open that link from the Explorer, it will make sure that the service is started. That might be the reason why you're able to access the Sharepoint from your app after you opened the link in Explorer.

You can make sure that your clients have that service started automatically to achieve it.

Or you can try starting the service from you code in this way. (You might need admin privileges for this)

using (ServiceController service= new ServiceController("WebClient"))
    {

        if (service.Status == ServiceControllerStatus.Stopped)
        {

            service.Start();
            service.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 0, 15));
            //Check status here by calling service.Status and proceed with your code.
        }
        else
        {
          //proceed with your code as the service is up and running
        }
    }
Trautman answered 21/6, 2016 at 11:54 Comment(2)
I used this several times now, and it does the job, good answer, thank you!Kuhlman
This was exactly what I needed. I was getting a network path not found error. One note on the code above is that it needs a reference to System.ServiceProcess.dll. msdn.microsoft.com/en-us/library/…Nine

© 2022 - 2024 — McMap. All rights reserved.