我查看了多个教程并询问了许多问题,没有结果。这是一步一步:
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 ... ... ...
用红色加下划线(不识别命名空间)?
如何使代码工作?
答案 0 :(得分:0)
由于Lists.asmx
是SOAP Web服务,因此可以按照文章中所述引用它:How to: Add a Reference to a Web Service
如何在Visual Studio中添加Lists.asmx
SOAP Web服务:
Add Service Reference
出现Add Service Reference
对话框,在Service Reference Settings
对话框中,点击Add Web Reference
,如下所示
指定服务端点Url
Visual Studio
示例代码
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);
}
}
}