首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何序列化Microsoft.SharePoint.Client.ListItem (Sharepoint-2013)?

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

Stack Overflow用户
提问于 2014-06-08 07:25:18
回答 1查看 2.2K关注 0票数 0

我希望创建一个返回SharePoint ListCollection的WCF服务。我通过使用以下代码进行了尝试:

代码语言:javascript
复制
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标记它。有关其他受支持类型,请参阅文档。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-06-08 09:53:49

在搜索了很多之后,我发现了我的解决方案如下,如果有人有同样的问题:

代码语言:javascript
复制
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;
        }

    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24103928

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档