C# 如何使用 WebService 将文件复制到 SharePoint 中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/787610/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you copy a file into SharePoint using a WebService?
提问by Keith Sirmons
I am writting a winforms c# 2.0 application that needs to put an XML file into a document library on SharePoint.
我正在编写一个需要将 XML 文件放入 SharePoint 上的文档库的 winforms c# 2.0 应用程序。
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.asmxwebservice.
我目前正在使用http://webserver/site/_vti_bin/copy.asmx 网络服务。
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 输出参数中得到一个结果:
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."
Microsoft TechNet——“copy.asmx copyintoitems 仅在源 URL 和目标 URL 位于同一个 SPWebApplication(网站集)中时才有效。”
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 应该设置为某些东西,但是什么?这源自客户端工作站,没有源 URL。
Any help would be appricated.
任何帮助将被应用。
hank you,
Keith
谢谢你,
基思
采纳答案by Keith Sirmons
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
基思
回答by Tony Testa
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();
回答by Kasper
I don't get it, why are you using Copy rather then UpdateListItems. Perhaps UpdateListItems will be a better match?
我不明白,你为什么使用 Copy 而不是UpdateListItems。也许 UpdateListItems 会更匹配?
回答by Nat
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);
}
回答by pat
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. Looks like Basically the request fails because the destination url can't be resolved.
检查 Steve Curran 在此线程上的回答http://social.msdn.microsoft.com/Forums/en-SG/sharepointdevelopment/thread/833e38a8-f13c-490d-8ba7-b889b6b25e38。看起来基本上请求失败,因为无解析目标 url。
(Limitations of a new stackflow user - can't post more than one link. See my comment for the rest)
(新 stackflow 用户的限制 - 不能发布多个链接。其余的请参阅我的评论)
pat
拍
回答by Vitor
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.
我不确定它是否会解决您的问题,但是,当您引用网络服务时,请不要使用 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 方面的专家,但我很确定网络服务属于主服务器,而不是特定站点。
Hope it helps.
希望能帮助到你。
回答by Ian
I get the same message when I use the default credentials. Try replacing them with this:
当我使用默认凭据时,我收到相同的消息。尝试用这个替换它们:
copyWebService.Credentials
= new NetworkCredential("Administrator", "pass", "MyDomain");
回答by Pawel Gorczynski
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"
身份冒充=“真”
in the server.web section.
在 server.web 部分。
回答by Mark
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.
如果您的共享点服务器建立在场上,请检查您的“备用访问映射”,看看是否有条目: yourwebserverurl 内联网 yourwebserverurl 如果没有,请添加它。
for my case, after adding this, the Copy service start working.
就我而言,添加此内容后,复制服务开始工作。
It probably due to farm load balance address resolve related.
这可能与场负载平衡地址解析有关。