Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 363
Erik Avatar asked Jun 21 '16 10:06

Erik


People also ask

Why can't I share a file on SharePoint?

Check the access request settings This issue might occur if the access request settings of the SharePoint site don't allow members to share files and folders. To change the settings, follow the steps at Change access requests settings.

How do I fix a failed SharePoint upload?

The fix for this error is relatively simple once one knows the cause. We need to clear out the cached documents in Microsoft Upload Center, and then tell it to always delete files from the cache when they are closed. In Windows 10: Type Upload in the Cortana search bar (bottom left, right beside the start menu button)


1 Answers

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
        }
    }
like image 134
Vishnu Prasad V Avatar answered Oct 05 '22 01:10

Vishnu Prasad V