[英]How to upload a file to a SharePoint library via one of the standard webservices
[英]How to upload a document to SharePoint using WebServices
我瀏覽了多個教程,並提出了許多沒有結果的問題。 這是逐步的:
1)我使用Microsoft Visual Studio 2012。 我沒有安裝SP服務器,不能使用Microsoft.SharePoint.dll。 但是,我可以使用Web服務
2)我創建一個控制台項目,並添加一個WebReference這樣的
3)這是我的完整代碼
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ConsoleApplication6;
namespace ConsoleApplication6
{
class Program
{
static void Main(string[] args)
{
string srcUrl = @"C:\\xxx\\test.txt";
System.IO.FileStream fStream = System.IO.File.OpenRead(srcUrl);
string fileName = fStream.Name.Substring(3);
byte[] contents = new byte[fStream.Length];
fStream.Read(contents, 0, (int)fStream.Length);
fStream.Close();
ServiceWebReference.Lists listService = new ServiceWebReference.Lists(); // "Lists" get underlined with a red line ?
listService.ClientCredentials = System.Net.CredentialCache.DefaultCredentials;
try
{
// adding attachment
string result = listService.AddAttachment("testList", "1", fileName, contents);
Console.WriteLine(result);
}
catch (System.Web.Services.Protocols.SoapException e)
{
Console.WriteLine(e.GetBaseException());
Console.WriteLine(e);
}
}
}
}
我是否以正確的方式添加了參考? 如果是,那為什么ServiceWebReference.Lists listService ... ... ...
用紅色加下划線(不識別名稱空間)?
如何使代碼正常工作?
由於Lists.asmx
是SOAP Web服務,因此可以按照以下文章中的描述進行引用: 如何:向Web服務添加引用
如何在Visual Studio中添加Lists.asmx
SOAP Web服務:
Add Service Reference
出現“ Add Service Reference
對話框,在“ Service Reference Settings
對話框中,單擊“ Add Web Reference
,如下所示
指定服務端點網址
視覺工作室
樣例代碼
namespace SPUpload
{
class Program
{
static void Main(string[] args)
{
var attachmentUrl = UploadAttachment(@"c:\temp\usermanual.rtf","Phones","1");
}
private static string UploadAttachment(string filePath, string listName, string listItemId)
{
var listsSvc = new ListsService.Lists();
listsSvc.Credentials = System.Net.CredentialCache.DefaultCredentials;
var fileName = Path.GetFileName(filePath);
var fileContent = File.ReadAllBytes(filePath);
return listsSvc.AddAttachment(listName, listItemId, fileName, fileContent);
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.