c# - 使用 Web 服务将文件上传到 Sharepoint 2013 - 对象引用未设置为对象的实例

标签 c# web-services sharepoint sharepoint-2013

我正在尝试使用网络服务 copy.asmx 将文件上传到 Sharepoint 2013

我使用以下方法创建了一个简单的项目:

public bool UploadFile(string file, string destination)
{
bool success = false;
CopySoapClient client = new CopySoapClient();

if (client.ClientCredentials != null)
{
client.ClientCredentials.Windows.ClientCredential = new NetworkCredential("admin", "password", "domain");
}

try
{
client.Open();

string filename = Path.GetFileName(file);
string destinationUrl = destination +@"/"+ filename;
string[] destinationUrls = { destination };

FieldInformation i1 = new FieldInformation { DisplayName = "Title", InternalName = "Title", Type = FieldType.Text, Value = filename};
FieldInformation[] info = { i1 };
CopyResult[] result;
byte[] data = File.ReadAllBytes(file);

uint ret = client.CopyIntoItems(file, destinationUrls, info, data, out result);

if (result != null && result.Length > 0 && result[0].ErrorCode == 0)
success = true;
}
finally
{
if (client.State == System.ServiceModel.CommunicationState.Faulted)
client.Abort();

if (client.State != System.ServiceModel.CommunicationState.Closed)
client.Close();
}

return success;
}

CopySoapClient 是复制服务引用的一部分

http://SPSITE/_vti_bin/copy.asmx

使用以下参数调用该方法:

UploadFile(@"C:\temp\test.txt", "http://SPSITE/sites/Connector/documents/test.txt");

问题是,当程序执行时

uint ret = client.CopyIntoItems(file, destinationUrls, info, data, out result); 

Web 服务返回结果“未知错误”,描述为“对象引用未设置到对象的实例。”

我真的不知道我错过了什么。谁能帮我吗?

谢谢。

附言我在 Internet 上提供的示例中注意到人们正在使用 copy.asmx 中的 Copy 类。但我只有 CopySoapClient 类。

最佳答案

您使用了正确的 Web 服务,但在错误的 SharePoint 站点上。尝试在您在此处作为目标库发布的同一个共享点站点上引用此 Web 服务。

关于c# - 使用 Web 服务将文件上传到 Sharepoint 2013 - 对象引用未设置为对象的实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20329473/

相关文章:

c# - Windows 8 上列表框项目选择周围的白框

C# MySQL DataRow 具有多个值

c# - 在负载均衡器和 SSL 后面配置 WCF

sharepoint - 正确处置 SharePoint 对象?

sharepoint - 在 SharePoint 生成的电子邮件中批准/拒绝

c# - 过多调试输出 "Event X was called with Y arguments, but it is defined with Z parameter(s)"的解决方法

c# - 如何使用 C# 更改 Windows 配色方案?

java - 为什么我们使用 <cxf :rsServer> as opposed to a plain <jaxrs:server> when using the CXF-RS component?

.net - Web 服务的本土消费

algorithm - 如何防止多个用户同时将项目添加到 Sharepoint 列表