Search code examples
javascriptajaxsharepointsharepoint-2010csom

Need help getting LookupList values using CSOM with a CAML Query


SHAREPOINT 2010 users

I have a SharePoint List full of items, and one of the field is Topics. I would like to be able to return those values. If I use the actual field Title, I will get [Object Object] as opposed to the object value. I am wondering if it is an issue with the terms I am using since they are so similar to .Net.

Anyways, here is what I have thus far.

<!DOCTYPE html> 

<%@ Page language="C#" %> 

<%@ Register Tagprefix="SharePoint" 

     Namespace="Microsoft.SharePoint.WebControls" 

     Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> 

<%@ Import Namespace="Microsoft.SharePoint" %> 

<html xmlns:mso="urn:schemas-microsoft-com:office:office" xmlns:msdt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882"> 

<head> 

<meta name="WebPartPageExpansion" content="full" /> 

    <!-- the following 5 js files are required to use CSOM --> 

    <script type="text/javascript" src="/_layouts/1033/init.js"></script> 

    <script type="text/javascript" src="/_layouts/MicrosoftAjax.js"></script> 

    <script type="text/javascript" src="/_layouts/sp.core.js"></script> 

    <script type="text/javascript" src="/_layouts/sp.runtime.js"></script> 

    <script type="text/javascript" src="/_layouts/sp.js"></script> 

 <script type="text/javascript" src="//code.jquery.com/jquery-1.11.3.min.js"></script> 

<SharePoint:FormDigest ID="FormDigest1" runat="server"></SharePoint:FormDigest> 



    <script type="text/ecmascript"> 







  window.onload = function OnLoad() { 





SP.SOD.executeFunc('sp.js','SP.ClientContext',retrieveListItems()); 



var oList; 



function retrieveListItems() { 



var Url = '/Policies'; 



    var clientContext = new SP.ClientContext(Url);     //.(Url); //SP.ClientContext.get_current(); 



   var oList = clientContext.get_web().get_lists().getByTitle('HRPolicies'); 
 var oListTags = clientContext.get_web().get_lists().getByTitle('TopicsSF'); 

//var field = oList.get_fields().getByInternalNameOrTitle("HRTopics");    
//var lookupField = clientContext.CastTo<FieldLookup>(field); 

//var childIdField = oList["HRTopics"] as FieldLookupValue[];

//if (childIdField != null)
//{
 //   foreach(var lookupValue in childIdField)
  //  {
  //      var childId_Value = lookupValue.LookupValue;
  //      var childId_Id = lookupValue.LookupId;
  //  }
//}

//end if no working, try field itself as getllokup as

    var camlQuery = new SP.CamlQuery(); 

   camlQuery.set_viewXml(multiLookupValues); 

   // this.collListItem = oList.getItems(camlQuery); 

     var PertinentItems = oList.getItems(camlQuery); 

clientContext.Load(item, i => i["HRTopics"]);
       clientContext.Load(items);
        clientContext.ExecuteQuery();
        var lookupValues = new ArrayList();
        FieldLookupValue[] values = item["HRTopics"] as FieldLookupValue[];
        foreach (ListItem listItem in items)
        {
            var lookupValue = new FieldLookupValue { LookupId = listItem.Id };
            lookupValues.label;
        }
        item.ParseAndSetFieldValue(lookupColumnName, null);
        item["HRTopics"] = lookupValues.ToArray();
        ctx.ExecuteQuery();

 //   var lookupListId = new Guid(lookupField.LookupList);
  //  var lookupList = clientContext.Web.Lists.GetById(lookupListId);
//
  //  clientContext.load(collListItem,lookupField); 

   // works alert("birds"); 



   // clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));        

    clientContext.executeQueryAsync(onQuerySucceeded, onQueryFailed);        
//




//} 



function onQuerySucceeded(sender, args) { 



   var listItemInfo = ''; 



    var listItemEnumerator = collListItem.getEnumerator(); 



   while (listItemEnumerator.moveNext()) { 

        var oListItem = listItemEnumerator.get_current(); 

        listItemInfo += '\nID: ' + oListItem.get_id() + 

            '\nTitle: ' + oListItem.get_item('Title') + 
 '\nHRTopics: ' + oListItem.get_item('HRTopics') 
    } 





  document.getElementById('realData').innerHTML = listItemInfo.toString();



} 



function onQueryFailed(sender, args) { 



    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace()); 

} 

} 





    </script> 





<!--[if gte mso 9]> 

<SharePoint:CTFieldRefs runat=server Prefix="mso:" FieldList="FileLeafRef,WikiField,_dlc_DocId,_dlc_DocIdUrl,_dlc_DocIdPersistId"><xml>







































<mso:CustomDocumentProperties> 

<mso:_dlc_DocId msdt:dt="string">SCP5UENJTFED-1359-20</mso:_dlc_DocId> 

<mso:_dlc_DocIdItemGuid msdt:dt="string">7d65ee24-642b-450f-8285-a1fd946836f4</mso:_dlc_DocIdItemGuid> 

<mso:_dlc_DocIdUrl msdt:dt="string">site/_layouts/DocIdRedir.aspx?ID=SCP5UENJTFED-1359-20, SCP5UENJTFED-1359-20</mso:_dlc_DocIdUrl> 

</mso:CustomDocumentProperties> 

</xml></SharePoint:CTFieldRefs><![endif]--> 

</head> 

<body> 

   <div id="realData">

     </div>   

</body> 

</html>​

Solution

  • The value returned for a lookup column through the JavaScript object model is a compound object (with several properties) instead of a primitive or a string. You might want to access either the ID of the item looked up or the text value of the field being looked up, so it returns both.

    Try adding .get_lookupValue() to the end of oListItem.get_item('HRTopics').

    Edit:

    If the lookup column allows multiple values, the object returned by .get_item() will be an array of objects; the array itself does not support the .get_lookupValue() method. You will need to iterate over the elements in the array and call .get_lookupValue() on each element in order to get the desired values.

    var valueString = "";
    var values = oListItem.get_item('HRTopics');
    for(var i in values){
        valueString += values[i].get_lookupValue();
    }