Ersin
Ersin

Reputation: 31

Upload file to Sharepoint 2013 using web service - Object reference not set to an instance of an object

I'm trying to upload file to Sharepoint 2013 using web service copy.asmx

I've created simple project with the following method:

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 is part Copy service reference

http://SPSITE/_vti_bin/copy.asmx

The method is called using following parameters:

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

The problem is, when program executes

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

the web service returns in result "Unknown error" with description "Object reference not set to an instance of an object."

I really don't know what I'm missing. Can anyone help me out?

Thank you.

P.S. I've noticed in examples provided on the Internet that people are using a Copy class from copy.asmx. But I only have CopySoapClient class.

Upvotes: 3

Views: 3729

Answers (2)

Jackie
Jackie

Reputation: 25969

missing the OPTIONAL element of <Fields> and <FieldInformation >, resulted ErrorCode="Unknown" ErrorMessage="Object reference not set to an instance of an object." . even though, Field and FieldInformation supposed to be optional. It could be the server side configuration. Anyway, it works for me now, adding all attributed, required/optional.

Upvotes: 1

Martin Kas
Martin Kas

Reputation: 11

you used the right web services but on wrong SharePoint site. Try to make this web services reference on the same sharepoint site you have posted here as target library.

Upvotes: 1

Related Questions