How to retrieve list items from SharePoint 2013

时间:2016-10-20 12:40:17

标签: c# list sharepoint-2013

At work we had to move from SharePoint 2010 to 2013 and my code for retrieving list items doesn't work anymore. Here is my code for SP 2010:

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;

Can you please help me to get it working with a SharePoint 2013 list again?

URL has already been updated but i get the following error: 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

But the list has no empty fields.

listName 

is the ID of the list element.

Please help.

Thanks in advance!

1 个答案:

答案 0 :(得分: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

相关问题