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

时间:2013-12-02 13:27:38

标签: 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服务返回结果“未知错误”,描述“对象引用未设置为对象实例。”

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

谢谢。

P.S。我在互联网上提供的示例中注意到人们正在使用copy.asmx中的Copy类。但我只有CopySoapClient类。

2 个答案:

答案 0 :(得分:1)

您使用的是正确的网络服务,但是在错误的SharePoint网站上。尝试在此处作为目标库发布的同一个sharepoint站点上创建此Web服务引用。

答案 1 :(得分:1)

缺少< Fields>的OPTIONAL元素和< FieldInformation>,导致ErrorCode =“Unknown”ErrorMessage =“对象引用未设置为对象的实例。” 。尽管如此,Field和FieldInformation应该是可选的。它可能是服务器端配置。无论如何,它现在适用于我,添加所有属性,必需/可选。