The below code work for all the Columns except for Publication
which is a Lookup.
The If publicationField
is ALWAYS Null...ideas?
// Assume the web has a list named "ADMPANewsAlertsWebAPI".
List announcementsList = clientContext.Web.Lists.GetByTitle(SharePointConnector.sp_NewsAlerts_list);
// This creates a CamlQuery that has a RowLimit of 100, and also specifies Scope="RecursiveAll"
// so that it grabs all list items, regardless of the folder they are in.
CamlQuery CamlQuery = new CamlQuery();
string qs =
"<View><ViewFields>" +
"<FieldRef Name='to' />" +
"<FieldRef Name='url' />" +
"<FieldRef Name='subject' />" +
"<FieldRef Name='Title' />" +
"<FieldRef Name='source' />" +
"<FieldRef Name='Created' />" +
"<FieldRef Name='userid' />" +
"<FieldRef Name='Category' />" +
"<FieldRef Name='comments' />" +
"<FieldRef Name='Publication' />" +
"</ViewFields></View>";
CamlQuery.ViewXml = qs;
ListItemCollection items = announcementsList.GetItems(CamlQuery);
// Retrieve all items in the ListItemCollection from List.GetItems(Query).
clientContext.Load(items);
clientContext.ExecuteQuery();
var list = new List<Business.Entities.api.newsalert>();
foreach (ListItem listItem in items)
{
var foo = new Business.Entities.api.newsalert();
foo.to = listItem["to"] == null ? String.Empty : listItem["to"].ToString();
foo.url = listItem["url"] == null ? String.Empty : listItem["url"].ToString();
foo.subject = listItem["subject"] == null ? String.Empty : listItem["subject"].ToString();
foo.title = listItem["Title"] == null ? String.Empty : listItem["Title"].ToString();
foo.source = listItem["source"] == null ? String.Empty : listItem["source"].ToString();
foo.timestamp = Offset(Convert.ToDateTime(listItem["Created"].ToString()));
foo.userid = listItem["userid"] == null ? String.Empty : listItem["userid"].ToString();
foo.categories = listItem["Category"] == null ? String.Empty : listItem["Category"].ToString();
foo.comments = listItem["comments"] == null ? String.Empty : listItem["comments"].ToString();
var publicationField = listItem["Publication"] as FieldLookupValue[];
if (publicationField != null) //<--- This line always Null
{
foreach (var lookupValue in publicationField)
{
var publication_Value = lookupValue.LookupValue;
var publication_Id = lookupValue.LookupId;
foo.publication = publication_Value;
}
}
list.Add(foo);
}