[英]How to Serialize Microsoft.SharePoint.Client.ListItem (Sharepoint-2013)?
I want to create a WCF Service that returns the SharePoint ListCollection. 我想创建一个返回SharePoint ListCollection的WCF服务。 I have tried by using following code:
我已经尝试通过使用以下代码:
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;
}
}
But it gives me error like follows: 但这给了我如下错误:
Type 'Microsoft.SharePoint.Client.ListItem' cannot be serialized. 类型'Microsoft.SharePoint.Client.ListItem'无法序列化。 Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute.
考虑使用DataContractAttribute属性标记它,并使用DataMemberAttribute属性标记要序列化的所有成员。 If the type is a collection, consider marking it with the CollectionDataContractAttribute.
如果类型是集合,请考虑使用CollectionDataContractAttribute对其进行标记。 See the Microsoft .NET Framework documentation for other supported types.
有关其他受支持的类型,请参见Microsoft .NET Framework文档。
after searching lot i had come across my solution as follows if any one had same problem: 搜索很多之后,如果有人遇到相同的问题,我遇到了如下问题:
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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.