Getting The remote server returned an error: (403) Forbidden. error while uploading file to share point document library using SSIS
Getting The remote server returned an error: (403) Forbidden. error while uploading file to share point document library using SSIS
我想使用 SSIS 将文件从本地目录上传到 Share Point 文档库。我正在使用脚本任务来这样做。虽然 运行 我得到的包低于错误:
The remote server returned an error: (403) Forbidden.
脚本任务代码如下:
public void Main()
{
// TODO: Add your code here
WebClient myWebClient;
string DestinationURL;
string LocalFileName;
bool FireAgain = true;
try
{
myWebClient = new WebClient();
myWebClient.Headers.Add("user-agent", "Only a test!");
LocalFileName = @"C:\Users\jay.desai\Desktop\LSRSQL01_ACXM_20201003.html";
DestinationURL = @"https://companyname.sharepoint.com/sites/DataServices/Shared%20Documents/Data%20Dictionaries/LSRSQL01_ACXM_20201003.html";
Console.WriteLine(LocalFileName);
myWebClient.Credentials = new NetworkCredential("jay.desai@companyname.com", "HelloWorld@2891", "NATSYS");
myWebClient.Proxy.Credentials = CredentialCache.DefaultNetworkCredentials;
//myWebClient.UseDefaultCredentials = true;
Dts.Events.FireInformation(0, String.Empty, String.Format("Uploading {0} to {1}", LocalFileName, DestinationURL), String.Empty, 0, ref FireAgain);
// upload the file
myWebClient.UploadFile(DestinationURL, "PUT", LocalFileName);
}
catch (Exception ex)
{
// Catch and handle error
Dts.Events.FireError(0, String.Empty, ex.Message, String.Empty, 0);
}
Dts.TaskResult = (int)ScriptResults.Success;
}
我可以打开同一个文件夹并使用浏览器上传文件。
已通过使用 CSOM 解决。为此,您首先需要安装 SharePoint Online 客户端组件 SDK。然后在项目中你必须引用 Microsoft.SharePoint.Client
和 Microsoft.SharePoint.Client.Runtime
。 Microsoft 引用是 here
在脚本任务中使用以下代码:
public void Main()
{
// TODO: Add your code here
ClientContext clientContext = new ClientContext("https://companyname.sharepoint.com");
SecureString passWord = new SecureString();
foreach (char c in "HelloWorld@1234".ToCharArray()) passWord.AppendChar(c);
clientContext.Credentials = new SharePointOnlineCredentials("jay.desai@company.com", passWord);
using(FileStream fileStream = new FileStream(@"C:\Users\jay.desai\Desktop\LSRSQL01_ACXM_20201003.html", FileMode.Open))
ClientOM.File.SaveBinaryDirect(clientContext, "/sites/DataServices/Shared Documents/Data Dictionaries/LSRSQL01_ACXM_20201003.html", fileStream, true);
Dts.TaskResult = (int)ScriptResults.Success;
}
我想使用 SSIS 将文件从本地目录上传到 Share Point 文档库。我正在使用脚本任务来这样做。虽然 运行 我得到的包低于错误:
The remote server returned an error: (403) Forbidden.
脚本任务代码如下:
public void Main()
{
// TODO: Add your code here
WebClient myWebClient;
string DestinationURL;
string LocalFileName;
bool FireAgain = true;
try
{
myWebClient = new WebClient();
myWebClient.Headers.Add("user-agent", "Only a test!");
LocalFileName = @"C:\Users\jay.desai\Desktop\LSRSQL01_ACXM_20201003.html";
DestinationURL = @"https://companyname.sharepoint.com/sites/DataServices/Shared%20Documents/Data%20Dictionaries/LSRSQL01_ACXM_20201003.html";
Console.WriteLine(LocalFileName);
myWebClient.Credentials = new NetworkCredential("jay.desai@companyname.com", "HelloWorld@2891", "NATSYS");
myWebClient.Proxy.Credentials = CredentialCache.DefaultNetworkCredentials;
//myWebClient.UseDefaultCredentials = true;
Dts.Events.FireInformation(0, String.Empty, String.Format("Uploading {0} to {1}", LocalFileName, DestinationURL), String.Empty, 0, ref FireAgain);
// upload the file
myWebClient.UploadFile(DestinationURL, "PUT", LocalFileName);
}
catch (Exception ex)
{
// Catch and handle error
Dts.Events.FireError(0, String.Empty, ex.Message, String.Empty, 0);
}
Dts.TaskResult = (int)ScriptResults.Success;
}
我可以打开同一个文件夹并使用浏览器上传文件。
已通过使用 CSOM 解决。为此,您首先需要安装 SharePoint Online 客户端组件 SDK。然后在项目中你必须引用 Microsoft.SharePoint.Client
和 Microsoft.SharePoint.Client.Runtime
。 Microsoft 引用是 here
在脚本任务中使用以下代码:
public void Main()
{
// TODO: Add your code here
ClientContext clientContext = new ClientContext("https://companyname.sharepoint.com");
SecureString passWord = new SecureString();
foreach (char c in "HelloWorld@1234".ToCharArray()) passWord.AppendChar(c);
clientContext.Credentials = new SharePointOnlineCredentials("jay.desai@company.com", passWord);
using(FileStream fileStream = new FileStream(@"C:\Users\jay.desai\Desktop\LSRSQL01_ACXM_20201003.html", FileMode.Open))
ClientOM.File.SaveBinaryDirect(clientContext, "/sites/DataServices/Shared Documents/Data Dictionaries/LSRSQL01_ACXM_20201003.html", fileStream, true);
Dts.TaskResult = (int)ScriptResults.Success;
}