2016-10-20 46 views
-1

在工作中,我們必須從SharePoint 2010遷移到2013年,並且用於檢索列表項目的代碼不再有效。這裏是我的代碼爲SP 2010:如何從SharePoint 2013中檢索列表項目

com.mycompany.intranet.Lists listService = new com.mycompany.intranet.Lists(); 

listService.Credentials = System.Net.CredentialCache.DefaultCredentials; 

listService.Url = "url"; 

System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument(); 

string listName = cbxMailDistributionList.SelectedValue.ToString(); 
string viewName = ""; 
string rowLimit = "0"; 

System.Xml.XmlElement query = xmlDoc.CreateElement("Query"); 
System.Xml.XmlElement viewFields = xmlDoc.CreateElement("ViewFields"); 
System.Xml.XmlElement queryOptions = xmlDoc.CreateElement("QueryOptions"); 

viewFields.InnerXml = "<FieldRef Name='Title' />"; 

System.Xml.XmlNode nodeListItems = 
listService.GetListItems(listName, viewName, query, viewFields, rowLimit, queryOptions, null); 

xmlDoc.LoadXml(nodeListItems.InnerXml); 

xlNodeList rows = xmlDoc.GetElementsByTagName("z:row"); 

List<string> recipients = new List<string>(); 

foreach (XmlNode attribute in rows) 
{ 
    if(attribute.Attributes["ows_Title"].Value == null){} 
    else { 
    if (recipients.Contains(attribute.Attributes["ows_Title"].Value)){} 
    else { 
     recipients.Add(attribute.Attributes["ows_Title"].Value); 
     } 
     } 
} 

recipients.Sort(); 
distributionList = recipients; 

你能幫我讓它再次與SharePoint 2013列表一起工作嗎?

網址已經被更新,但我得到以下錯誤:https://msdn.microsoft.com/query/dev14.query?appId=Dev14IDEF1&l=DE-DE&k=k(EHNullReference);k(TargetFrameworkMoniker-.NETFramework,Version%3Dv4.0);k(DevLang-csharp)&rd=true

但名單沒有空字段。

listName 

是列表元素的ID。

請幫忙。

在此先感謝!

回答

0

最後,這段代碼再次工作:

  List<string> recipients = new List<string>(); 

      string siteURL = @"myurl/"; 

      ClientContext cc = new ClientContext(siteURL); 
      cc.Credentials = System.Net.CredentialCache.DefaultCredentials; 

      Web web = cc.Web; 

      List list = web.Lists.GetById(new Guid(cbxMailDistributionList.SelectedValue.ToString())); 

      CamlQuery caml = new CamlQuery(); 

      ListItemCollection items = list.GetItems(caml); 

      cc.Load<List>(list); 
      cc.Load<ListItemCollection>(items); 
      cc.ExecuteQuery(); 

      foreach (Microsoft.SharePoint.Client.ListItem item in items) 
      { 
       if(item.FieldValues["Title"] == null) { } 
       else 
       { 
        if (recipients.Contains(item.FieldValues["Title"].ToString())) { } 
        else 
        { 
         recipients.Add(item.FieldValues["Title"].ToString()); 
        } 
       } 
      } 

      recipients.Sort(); 
      distributionList = recipients; 

     } 
     else 
     { 
      distributionList = null; 
     } 

新的一天,新的運氣。對不起,提前發佈這個問題。我應該睡一個晚上。 ;)

BR

相關問題