如何序列化Microsoft.SharePoint.Client.ListItem(Sharepoint-2013)?

时间:2014-06-08 07:25:18

标签: c# wcf sharepoint sharepoint-2013

我想创建一个返回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;

    }

}

但它给出了如下错误:

键入&#39; Microsoft.SharePoint.Client.ListItem&#39;无法序列化。请考虑使用DataContractAttribute属性对其进行标记,并使用DataMemberAttribute属性标记要序列化的所有成员。如果类型是集合,请考虑使用CollectionDataContractAttribute对其进行标记。有关其他受支持的类型,请参阅Microsoft .NET Framework文档。

1 个答案:

答案 0 :(得分:1)

搜索完之后,如果有任何人遇到同样的问题,我会遇到如下问题:

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;
        }

    }