【问题标题】:How to Serialize Microsoft.SharePoint.Client.ListItem (Sharepoint-2013)?如何序列化 Microsoft.SharePoint.Client.ListItem (Sharepoint-2013)?
【发布时间】:2014-06-08 17:53:49
【问题描述】:

我想创建一个返回 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 对其进行标记。有关其他支持的类型,请参阅 Microsoft .NET Framework 文档。

【问题讨论】:

    标签: c# wcf sharepoint sharepoint-2013


    【解决方案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;
            }
    
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-05
      相关资源
      最近更新 更多