简体   繁体   English

如何使用WebService将文件复制到SharePoint?

[英]How do you copy a file into SharePoint using a WebService?

I am writting a winforms c# 2.0 application that needs to put an XML file into a document library on SharePoint. 我正在编写一个winforms c#2.0应用程序,需要将XML文件放入SharePoint上的文档库中。

I want to use a WebService instead of using the object model (no sharepoint.dll to reference here) 我想使用WebService而不是使用对象模型(这里没有sharepoint.dll引用)

I am currently using the http://webserver/site/_vti_bin/copy.asmx webservice. 我目前正在使用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: 当这段代码运行时,我在resultsArray out参数中得到一个结果:

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 TechNet - “只有源和目标URL位于同一SPWebApplication(网站集)中时,copy.asmx copyintoitems才有效。”

  • Microsoft Social -- "Object reference not set to an instance of an object error occurs because of SharePoint not able to identified that particular property." Microsoft Social - “由于SharePoint无法识别该特定属性,因此未将对象引用设置为对象错误的实例。”

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. 这源于客户端工作站,没有源URL。

Any help would be appricated. 任何帮助都会被批评。

hank you, 谢谢你,
Keith 基思

I know this is an old thread but it kept coming up as I was searching for a solution to the same problem. 我知道这是一个老线程,但它一直在我寻找解决同样问题的方法。

Check Steve Curran's answer on this thread http://social.msdn.microsoft.com/Forums/en-SG/sharepointdevelopment/thread/833e38a8-f13c-490d-8ba7-b889b6b25e38 . 查看Steve Curran在这个帖子上的答案http://social.msdn.microsoft.com/Forums/en-SG/sharepointdevelopment/thread/833e38a8-f13c-490d-8ba7-b889b6b25e38 Looks like Basically the request fails because the destination url can't be resolved. 看起来基本上请求失败,因为目标网址无法解析。

(Limitations of a new stackflow user - can't post more than one link. See my comment for the rest) (新的stackflow用户的限制 - 不能发布多个链接。请参阅我的评论)

pat

your code is fine, just use the destination url instead of an empty string. 你的代码没问题,只需使用目标网址而不是空字符串。 See below: 见下文:

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

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

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(destinationUrl , destinationUrlArray, fields, xmlByteArray, out resultsArray);
}

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? 那么......有没有人对这种“PUT”方法在SharePoint环境中比使用内置Web服务更好有意见?

Right now I would have to say the "PUT" method is better since it works and I could not get the WebService to work. 现在我不得不说“PUT”方法更好,因为它工作,我无法让WebService工作。

Keith 基思

I get the same message when I use the default credentials. 当我使用默认凭据时,我收到相同的消息。 Try replacing them with this: 尝试用这个代替它们:

copyWebService.Credentials 
    = new NetworkCredential("Administrator", "pass", "MyDomain");

I had a similiar problem, it turned out that the the client was configured to use NTLM security, but no NTLM header was attached. 我有一个类似的问题,结果是客户端配置为使用NTLM安全性,但没有附加NTLM头。

I my case, becuase of the fact that I was using this code on the server-side of an ASP.NET applicaton, was to enable Windows authentication and set 我的情况,因为我在ASP.NET应用程序的服务器端使用此代码,是为了启用Windows身份验证和设置

identity impersonate="true" identity impersonate =“true”

in the server.web section. 在server.web部分。

if your sharepoint server is built on a farm, Check your "Alternate Access Mapping" see if there is an entry: yourwebserverurl intranet yourwebserverurl if not, add it. 如果您的sharepoint服务器是在服务器场上构建的,请检查“备用访问映射”,看看是否有条目:yourwebserverurl intranet yourwebserverurl如果没有,请添加它。

for my case, after adding this, the Copy service start working. 对于我的情况,添加此项后,复制服务开始工作。

It probably due to farm load balance address resolve related. 它可能是由于农场负载平衡地址解决相关。

I'm not sure if it will solve your problem but, when you reference the webservice, don't use the [site] part of the URL. 我不确定它是否能解决您的问题但是,当您引用Web服务时,请不要使用URL的[site]部分。

Try instead: http://[server]/_vti_bin/[webservice]. 请尝试:http:// [server] / _vti_bin / [webservice]。

I'm not an expert in SP but I'm pretty sure the webservices belongs to the main server, not to an especific site. 我不是SP的专家,但我很确定web服务属于主服务器,而不是特定的站点。

Hope it helps. 希望能帮助到你。

Here's some code I wrote awhile (i apologize, i've had to piece meal it together, but hopefully you get the point of it) 这是我写了一段时间的一些代码(我道歉,我不得不把它一起吃,但希望你明白了)

    // Create a request using a URL that can receive a post. 
WebRequest request = WebRequest.Create("http://sharepointsite/somefile.txt");

// Set the Method property of the request to POST.
request.Method = "PUT"

Stream dataStream;

// Set the ContentType property of the WebRequest.
request.ContentType = "multipart/form-data; charset=ISO-8859-1";

byte[] byteArray = File.ReadAllBytes(@"c:\somefile.txt");

// Set the ContentLength property of the WebRequest.
request.ContentLength = byteArray.Length;

// Get the request stream.
dataStream = request.GetRequestStream();

// Write the data to the request stream.
dataStream.Write(byteArray, 0, byteArray.Length);

// Close the Stream object.
dataStream.Close();

// Get the response.
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
HttpStatusCode statCode = response.StatusCode;

// Get the stream containing content returned by the server.
dataStream = response.GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd();

// Clean up the streams.
reader.Close();
dataStream.Close();
response.Close();

I don't get it, why are you using Copy rather then UpdateListItems . 我不明白,为什么你使用Copy而不是UpdateListItems Perhaps UpdateListItems will be a better match? 也许UpdateListItems会更好匹配?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 SharePoint Copy Web 服务时如何设置托管元数据字段? - How do you set a Managed Metadata field when using SharePoint Copy Web Service? 如何使用SharePoint Copy Web服务的CopyIntoItems方法? - How do you use the CopyIntoItems method of the SharePoint Copy web service? 如何使用与Sharepoint相同的凭据调用Web服务? - How to call webservice using same credentials as Sharepoint? 如何将文件上传到 sharepoint 中的文档库? - How do you upload a file to a document library in sharepoint? 如何使用WSS将Active Directory用户添加到SharePoint? - How do you add an Active Directory user to SharePoint using WSS? 如何使用 sharepoint 2010 的 API 创建页面? - How do you create a page using the API for sharepoint 2010? 如何使用 Kestrel 对简单的 Web 服务进行复制和运行部署? - How do I do a copy-and-run deploy of a simple webservice using Kestrel? 如何以编程方式阅读SharePoint列表? - How do you Read SharePoint Lists Programmatically? 使用 web 服务将文件从一个服务器文件夹复制到另一个文件夹 - Copy a file from one server folder to another folder using webservice 如何使用WCF Web服务将文件从网络路径复制到本地位置? - How to copy file from network path to local location with WCF Webservice?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM