1

I have a piece of code in C# that would let me grab an Excel file in SharePoint and into a local folder.

Although I can access the file using my Windows login but when I try to run the package it gives me the following error:

The remote server returned an error: (403) Forbidden.

This is the piece of code that I am using :

        //  downloads the file from SharePoint or a file system location to a local folder
        Dts.TaskResult = (int)ScriptResults.Success;

        try
        {
            //  obtain location of local folder from variable
            DirectoryInfo dir = new DirectoryInfo(Dts.Variables["User::ImportFolder"].Value.ToString());
            if (dir.Exists)
            {
                //  Create the filename for local storage using
                //  the GUID from SharePoint as this will be unique.
                FileInfo file = new FileInfo((dir.FullName + ("\\"
                                + (Dts.Variables["User::WorkbookGUID"].Value.ToString() + Dts.Variables["User::Extension"].Value.ToString()))));
                if (!file.Exists)
                {
                    //  get the path of the file we need to download
                    string fileUrl = Dts.Variables["User::EncodedAbsUrl"].Value.ToString();
                    if ((fileUrl.Length != 0))
                    {
                        //  download the file from SharePoint or Archive file system to local folder
                        WebClient client = new WebClient();
                        if ((fileUrl.Substring(0, 4).ToLower() == "http"))
                        {
                            // download the file from SharePoint
                            client.Credentials = System.Net.CredentialCache.DefaultCredentials;
                            client.DownloadFile(fileUrl, file.FullName);
                        }
                        else
                        {
                            //  copy file from remote file system
                            System.IO.File.Copy(fileUrl, file.FullName);
                        }

                    }
                    else
                    {
                        throw new ApplicationException("EncodedAbsUrl variable does not contain a value!");
                    }

                }

            }
            else
            {
                throw new ApplicationException("ImportFolder does not exist!");
            }

        }
        catch (Exception ex)
        {
            Dts.Events.FireError(0, String.Empty, ex.Message, String.Empty, 0);
            Dts.TaskResult = (int)ScriptResults.Failure;
        }

        Dts.TaskResult = (int)ScriptResults.Success;
Hadi
  • 36,233
  • 13
  • 65
  • 124
Unbound
  • 177
  • 2
  • 14
  • If you pass the credentiels by code it works?? `client.Credentials = new SharePointOnlineCredentials("yourlogin@yoursite.onmicrosoft.com", passWord);`. In addition, check that the file path is built correctly – Hadi Mar 20 '19 at 12:36
  • Possible duplicate of [WebClient 403 Forbidden](https://stackoverflow.com/questions/3272067/webclient-403-forbidden) – Hadi Mar 20 '19 at 12:47
  • Also check the following link: https://sharepoint.stackexchange.com/questions/80961/the-remote-server-returned-an-error-403-forbidden – Hadi Mar 20 '19 at 12:55
  • I have installed SharePoint SDK from below and tried the "SharePointOnlineCredentials" but it still throws the same error. Code Tried: SecureString SecurePassword = new SecureString(); foreach (char c in password.ToCharArray()) SecurePassword.AppendChar(c); client.Credentials = new Microsoft.SharePoint.Client.SharePointOnlineCredentials(username, SecurePassword); client.DownloadFile(fileUrl, file.FullName); Link for SDK: https://www.microsoft.com/en-us/download/details.aspx?id=35585 – Unbound Mar 21 '19 at 08:40
  • Just wanted to share what I have also found out. You will need EncodedAbsUrl of the Excel File and getting that could be tricky. The dll Microsoft.Sharepoint gives you classes like SPList, SPListItem and SPFile which could be required to retrieve EncodedAbsUrl . The downside to that is the dll is only foudn at the server end and not the developer end. You would need to install Sharepoint Server in your machine to get the server-side dll. The client side dll called Microsoft.Sharepoint.Client would give you SharePointOnlineCredentials but that will not be enough. – Unbound Mar 21 '19 at 15:25

0 Answers0