简体   繁体   English

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

[英]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.

相关问题 如何从Microsoft.SharePoint.Client.ListItem检索所有属性? - How to retrieve all properties from Microsoft.SharePoint.Client.ListItem? 如何从类Microsoft.SharePoint.Client.ListItem中获取字段类型(整数,日期时间,选择)? - how do you get the field type (integer, datetime, choice) from the class Microsoft.SharePoint.Client.ListItem? C# - 将文件作为附件添加到 Microsoft.SharePoint.Client.ListItem (SharePoint 2010) - C# - Add a File as Attachment to Microsoft.SharePoint.Client.ListItem (SharePoint 2010) 从WPF远程创建SharePoint-2013的简单页面 - Creating simple pages for SharePoint-2013 remotely from WPF Sharepoint 2013-如何注销WinRT客户端 - Sharepoint 2013 - How to logout a WinRT Client Sharepoint 2013外部列表:ListItem.Update()正在还原项目值 - Sharepoint 2013 external lists: ListItem.Update() is reverting item values sharepoint listitem 权限不持久 - sharepoint listitem permissions not persisting 如何使用 Microsoft Graph 获取 DriveItem 上的 Sharepoint ListItem - How to get Sharepoint ListItem on DriveItem with Microsoft Graph API c# SDK 如何使用.Net更新Sharepoint ListItem? - How do I update a Sharepoint ListItem with .Net? SharePoint 2010 - 客户端对象模型 - 向ListItem添加附件 - SharePoint 2010 - Client Object Model - Add attachment to ListItem
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM