2

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

First of all, it looks the Publication field is a Lookup field with Multiple Allow multiple values.

So I simulated your code on my side and found out the following

  • There is no issue in CAML Query.
  • You can retrieve multiple lookup field values as the below code.
  • The Lookup publicationField field is always null in case it actually don't have a value.

Retrieve the Multiple Lookup field value and bind it to Grid view

            CamlQuery camlQuery = new CamlQuery();
            camlQuery.ViewXml = "<View><ViewFields><FieldRef Name='Title' /><FieldRef Name='Publication' /> </ViewFields></View>";
            Microsoft.SharePoint.Client.ListItemCollection collListItem = oList.GetItems(camlQuery);
            clientContext.Load(collListItem);
            clientContext.ExecuteQuery();
            DataTable dtAudit = new DataTable();
            dtAudit.Columns.Add("Multi-Lookup-FieldValue", typeof(System.String));
            foreach (Microsoft.SharePoint.Client.ListItem oListItem in collListItem)
            {
                var publicationField = oListItem["Publication"] as FieldLookupValue[];

                if (publicationField != null)
                {

                    foreach (var lookupValue in publicationField)
                    {
                        DataRow dRow = dtAudit.NewRow();
                        var publication_Value = lookupValue.LookupValue;
                        var publication_Id = lookupValue.LookupId;
                        dRow["Multi-Lookup-FieldValue"] = publication_Value;
                        dtAudit.Rows.Add(dRow);
                    }
                }

            }

            GridView1.DataSource = dtAudit;
            GridView1.DataBind();

Output

enter image description here


Base on my code and the attached output, it should be working on your side, So try to do the following:

  • Make sure that Lookup publicationField field has a value.
  • Try to replace my code on your side and debug it.

Regarding the Lookup field value without multiple values you should use the following:

    publicationField = listItem["Publication"] as FieldLookupValue;
       if (publicationField != null)
          {
             foo.publication = publicationField.LookupValue;

          }
  • Unfortunately, I still have the same results. Also, my column does NOT have Allow Mutliple Values turned on. There is content in the Publication column – Tim Cadieux Jul 8 '17 at 18:17
  • wait please! are you sure column does NOT have Allow Mutliple Values turned on ! – Mohamed El-Qassas MVP Jul 8 '17 at 18:18
  • So your code is not correct? let me provide you the correct code for lookup column without multiple value – Mohamed El-Qassas MVP Jul 8 '17 at 18:19
  • 1
    no need for for each and FieldLookupValue[] array here, because the lookup field without multiple value, so your code should like var publicationField = oListItem["Publication"] as FieldLookupValue; var publication_Value = publicationField.LookupValue; – Mohamed El-Qassas MVP Jul 8 '17 at 18:22
  • please feedback me the result! – Mohamed El-Qassas MVP Jul 8 '17 at 18:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.