我希望创建一个返回SharePoint ListCollection的WCF服务。我通过使用以下代码进行了尝试:
public class Service1 : IService1
{
public ListItemCollection GetList()
{
string username = "xxx";
string userPassword ="xxx";
var securePassword = new SecureString();
for (int i = 0; i < userPassword.Length; i++)
{
securePassword.AppendChar(userPassword[i]);
}
var creds = new SharePointOnlineCredentials(username, securePassword);
var clientContext = new ClientContext("MySharepointurl");
clientContext.Credentials = creds;
List announcementsList = clientContext.Web.Lists.GetByTitle("mylist");
CamlQuery query = CamlQuery.CreateAllItemsQuery(100);
var items = announcementsList.GetItems(query);
clientContext.Load(items);
clientContext.ExecuteQuery();
return items;
}
}但它给我的错误如下:
不能序列化“Microsoft.SharePoint.Client.ListItem”类型。考虑用DataContractAttribute属性标记它,并用DataMemberAttribute属性对它的所有成员进行标记。如果类型是集合,请考虑使用CollectionDataContractAttribute标记它。有关其他受支持类型,请参阅文档。
发布于 2014-06-08 09:53:49
在搜索了很多之后,我发现了我的解决方案如下,如果有人有同样的问题:
public List<MyClass> GetList()
{
try
{
string username = ConfigurationManager.AppSettings["username"];
string password = ConfigurationManager.AppSettings["password"];
string url = ConfigurationManager.AppSettings["AccountUrl"];
var newList = new List<MyClass>();
var securePassword = new SecureString();
for (int i = 0; i < password.Length; i++)
{
securePassword.AppendChar(password[i]);
}
var creds = new SharePointOnlineCredentials(username, securePassword);
var clientContext = new ClientContext(url)
{
Credentials = creds
};
List announcementsList = clientContext.Web.Lists.GetByTitle("mylist");
CamlQuery query = CamlQuery.CreateAllItemsQuery();
var items = announcementsList.GetItems(query);
clientContext.Load(items);
clientContext.ExecuteQuery();
foreach (var col in items)
{
newList.Add(new MyClass()
{
Id = Convert.ToInt32(col["ID"]),
FirstName = (string) col["FN"],
LastName = (string) col["LN"],
Email = (string) col["EM"],
UserId = (string) col["UID"],
Password = (string) col["PD"],
Title = (string) col["Title"]
});
}
return newList;
}
catch (Exception)
{
return null;
}
}https://stackoverflow.com/questions/24103928
复制相似问题