你如何将文件复制到SharePoint使用一个WebService? [英] How do you copy a file into SharePoint using a WebService?

查看:127
本文介绍了你如何将文件复制到SharePoint使用一个WebService?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我书面方式一个WinForms C#2.0的应用程序,需要把一个XML文件导入到在SharePoint文档库。

我想用一个WebService,而不是使用对象模型(无sharepoint.dll这里参考)

我目前使用的是 HTTP://webserver/site/_​​vti_bin/copy.asmx web服务。

下面是一些code:

 字节[] xmlByteArray;
使用(MemoryStream的MemoryStream的=新的MemoryStream())
{
    xmlDocument.Save(MemoryStream的);
    xmlBytes = memoryStream.ToArray();
}字符串[] = destinationUrlArray新的字符串[] {HTTP://webserver/site/Doclib/Uplo​​adedDocument.xml};FieldInformation字段信息=新FieldInformation();
FieldInformation []栏= {}字段信息;
CopyResult [] resultsArray;使用(复制copyService =新副本())
{
    copyService.Credentials = CredentialCache.DefaultCredentials;
    copyService.Url =HTTP://webserver/site/_​​vti_bin/copy.asmx    copyService.Timeout = 600000;    UINT documentId = copyService.CopyIntoItems(,destinationUrlArray,田野,xmlByteArray,出resultsArray);
}

在此code运行时,我得到一个结果在resultsArray输出参数:

  DESTINATIONURL:HTTP://webserver/site/Doclib/Uplo​​adedDocument.xml
错误code:未知
消息:'对象引用未设置到对象的实例。

从我的搜索,我发现了几个可能的帮助。


  • <一个href=\"http://social.technet.microsoft.com/Forums/pt-BR/sharepointdevelopment/thread/5a1ce3c9-ebc7-4ec9-b0fb-b891d076591b\">Microsoft的TechNet - 如果源和目标网址都在同一SPWebApplication(网站集)的copy.asmx copyintoitems才起作用。


  • <一个href=\"http://social.microsoft.com/Forums/zh-CN/sharepointdevelopment/thread/273528be-b207-48d9-b697-74777416638e\">Microsoft社会 - 对象引用不设置到对象的实例
    出现错误的原因的SharePoint无法识别出特定的财产。


这使我相信我的源地址应该设置的东西,但什么?这是从客户端工作站发起并没有一个源URL。

任何帮助将appricated。

汉克你,

基思


解决方案

下面是目前工作:

 的WebRequest请求= WebRequest.Create(HTTP://webserver/site/Doclib/Uplo​​adedDocument.xml);
request.Credentials = CredentialCache.DefaultCredentials;
request.Method =PUT;
字节[]缓冲区=新的字节[1024];
使用(流流= request.GetRequestStream())
{
    使用(MemoryStream的MemoryStream的=新的MemoryStream())
    {
        dataFile.MMRXmlData.Save(MemoryStream的);
        memoryStream.Seek(0,SeekOrigin.Begin);
        对(INT I = memoryStream.Read(缓冲液,0,buffer.Length);我大于0;
            I = memoryStream.Read(缓冲液,0,buffer.Length))
        {
            stream.Write(缓冲液,0,i)的;
        }
    }
}WebResponse的响应= request.GetResponse();
response.Close();

所以...有没有人有意见为,如果这PUT方法是将SharePoint环境中比使用内置的web服务好?

现在我不得不说的PUT方法是更好,因为它的工作原理,我不能让WebService的工作。

基思

I am writting a winforms c# 2.0 application that needs to put an XML file into a document library on SharePoint.

I want to use a WebService instead of using the object model (no sharepoint.dll to reference here)

I am currently using the http://webserver/site/_vti_bin/copy.asmx webservice.

Here is some code:

byte[] xmlByteArray;
using (MemoryStream memoryStream = new MemoryStream())
{
    xmlDocument.Save(memoryStream);
    xmlBytes = memoryStream.ToArray();
}

string[] destinationUrlArray = new string[] {"http://webserver/site/Doclib/UploadedDocument.xml"};

FieldInformation fieldInfo = new FieldInformation();
FieldInformation[] fields = { fieldInfo };


CopyResult[] resultsArray;

using (Copy copyService = new Copy())
{
    copyService.Credentials = CredentialCache.DefaultCredentials;
    copyService.Url = "http://webserver/site/_vti_bin/copy.asmx";

    copyService.Timeout = 600000;

    uint documentId = copyService.CopyIntoItems("", destinationUrlArray, fields, xmlByteArray, out resultsArray);
}

When this code runs, I get a single result in the resultsArray out parameter:

DestinationURL: "http://webserver/site/Doclib/UploadedDocument.xml"
ErrorCode: UnKnown
ErrorMessage: "Object reference not set to an instance of an object."

From my searching, I have found a couple of possible helps.

  • Microsoft TechNet -- "The copy.asmx copyintoitems will only work if the source and destination urls are in the same SPWebApplication (Site Collection)."

  • Microsoft Social -- "Object reference not set to an instance of an object error occurs because of SharePoint not able to identified that particular property."

This leads me to believe my source url should be set to something, but what? This is originating from a client workstation and does not have a source URL.

Any help would be appricated.

hank you,
Keith

解决方案

Here is what is currently working:

WebRequest request = WebRequest.Create("http://webserver/site/Doclib/UploadedDocument.xml");
request.Credentials = CredentialCache.DefaultCredentials;
request.Method = "PUT";
byte[] buffer = new byte[1024];
using (Stream stream = request.GetRequestStream())
{
    using (MemoryStream memoryStream = new MemoryStream())
    {
        dataFile.MMRXmlData.Save(memoryStream);
        memoryStream.Seek(0, SeekOrigin.Begin);
        for (int i = memoryStream.Read(buffer, 0, buffer.Length); i > 0;
            i = memoryStream.Read(buffer, 0, buffer.Length))
        {
            stream.Write(buffer, 0, i);
        }
    }
}

WebResponse response = request.GetResponse();
response.Close();

So... Does anyone have an opinion as to if this "PUT" method is better in the SharePoint environment than using a built-in webservice?

Right now I would have to say the "PUT" method is better since it works and I could not get the WebService to work.

Keith

这篇关于你如何将文件复制到SharePoint使用一个WebService?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆