2014-06-08 29 views
0

我想創建一個返回SharePoint ListCollection的WCF服務。我已經用下面的代碼嘗試:如何序列化Microsoft.SharePoint.Client.ListItem(Sharepoint-2013)?

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文檔。

回答

0

搜索很多之後,我曾經碰到過我的解決方案如下,如果任何人有同樣的問題:

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

    }