Skip to main content

SharePoint 2010 retrieving lookup values using ECMAScript

Script to retrieve single and multiple value lookup fields values using SharePoint 2010 Client Object Model for JavaScript (ECMAScript):


var listName = "list name here";
var listItemId = 23; // id here

function retrieveLookupValues() {
    var clientContext = new SP.ClientContext.get_current();
    var oList = clientContext.get_web().get_lists().getByTitle(listName);
    var selectedItems = SP.ListOperation.Selection.getSelectedItems();
    var camlQuery = new SP.CamlQuery();
    camlQuery.set_viewXml("< View >< Query >< Where >< Eq >< FieldRef Name='ID' / >< Value Type='Counter'>" + listItemId + "< /Value>< /Eq>< /Where>< /Query>< RowLimit>100< /RowLimit>< /View>");
    this.collListItem = oList.getItems(camlQuery);
    clientContext.load(collListItem, 'Include(CustomSingelLookupFld, CustomMultilLookupFld)');
    clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}

function onQuerySucceeded(sender, args) {
 var listInfo = "";
    var listItemEnumerator = collListItem.getEnumerator();

    while (listItemEnumerator.moveNext()) {
        var oListItem = listItemEnumerator.get_current();
        // get single value LookupField:
        var oCustomSingelLookupFld = oListItem.get_item("CustomSingelLookupFld");
  listInfo += "oCustomSingelLookupFld.Value = " + oCustomSingelLookupFld.get_lookupValue() + " \t oCustomSingelLookupFld.Id = " + oCustomSingelLookupFld.get_lookupId();
  // get multiple value LookupField:
        var oCustomMultilLookupFld = oListItem.get_item("CustomMultilLookupFld");
  for(var ix = 0; ix < oCustomMultilLookupFld.length; ix++){
   listInfo += "oCustomSingelLookupFld[" + ix + "].Value = " + oCustomMultilLookupFld[ix].get_lookupValue() + " \t oCustomSingelLookupFld[" + ix + "].Id = " + oCustomMultilLookupFld[ix].get_lookupId();
  }
    }
 alert(listInfo);
}

function onQueryFailed(sender, args) {
    alert('Request failed. ' + args.get_message());
}

Comments

Popular posts from this blog

Emmet in Visual Studio Code | cheat sheet link

Support for   Emmet   snippets and expansion is built right into Visual Studio Code,   no extension required .   Emmet 2.0   has support for the majority of the   Emmet Actions   including expanding   Emmet abbreviations and snippets .   Emmet is a web-developer’s toolkit that can greatly improve your HTML & CSS workflow   Links: Emmet in Visual Studio Code   Cheat Sheet (emmet.io)  

How to deloy InfoPath 2010 "Administrator-approved form template" to SharePoint 2010 programaticaly

Scenario: you've developed and InfoPath 2010 form. This form contains code that requires full trust, so you need to deploy it as a administrator-approved form template. This requires an farm administrator to check, upload and activate the form for the site collection. Solution:  to automate this operation I  created a farm level feature - w hen the feature is activated upload the form  programmatically and w hen the feature is deactivated remove the form  programmatically. Here is how I did it: 1. Create an Empty SharePoint Project and set it to be deployed as a farm solution 2. Add an Empty element item - EmptyElement1 . This will also generate a feature - Feature1 3. Delete the Elements.xml file of the EmptyElement1 4. Copy/Paste the InfoPath 2010 form into the EmptyElement1 5. Right click on the added form and click Properties:  - Deployment Type: ElementFile  - Deployment Location> Path: set it to blank 6. Open the generated featu...

Creating SharePoint 2010 Calculated Field from code in sites with different regional settings

Say you need to create the following calculated column in different sites that have different regional settings: English, French, etc. to format the DateTime [Start Time] filed to  "dd-mm-yyyy". The following formula would look list this in English - with a comma separator: = TEXT ([ Start Time ] , "dd-mm-yyyy" ) Like this in french - with a semicolon separator: = TEXT ([ Start Time ] ; "dd-mm-yyyy" ) When creating the column from from code  behind to treat this  the SP Web . Locale . TextInfo . ListSeparator  property must be used to get the correct list separator: SPList spList = SPContext . Current . Web . Lists [ "List Title" ]; string newFieldName = spList . Add ( "CalculatedField" , SPFieldType . Calculated , false ); SPFieldCalculated newField = ( SPFieldCalculated ) spList . GetField ( newFieldName ); newField . Formula = String . Format (@ "=TEXT([Start Time]{0} ""dd-mm-yyyy"")...