none
Retrieve an optional field item through CSOM RRS feed

  • Question

  • Hi 

    Below is my code and i am trying to retrieve an optional field from the list where user might give value of the field or not since it is a not required field.

    Please let help where i lack in my logic

    /////////////////CODE SUCCEED method/////////////////////////////////////////

                                       

    function getTabData() {

    var tabContext = new SP.ClientContext.get_current();

           var list = tabContext.get_web().get_lists().getByTitle('TabDataList');

            var camlTab = new SP.CamlQuery();

            if($( '#tabFirst').length)

            {

            camlTab.set_viewXml("<View><Query><Where><Eq><FieldRef Name='Tab' /><Value Type='Choice'>Tab 1</Value></Eq></Where></Query></View>");

                       

            tabDetailColl1 = list.getItems(camlTab);  

            tabContext.load(tabDetailColl1);  

         

            //tabContext.executeQueryAsync(onSucceededTab, onFailedTab);

             tabContext.executeQueryAsync(Function.createDelegate(this,function(){onSucceededTab("Tab 1",tabDetailColl1);}),

                                            Function.createDelegate(this, this.onFailedTab));

            }

    if($( '#tabSecond').length)

            {

            camlTab.set_viewXml("<View><Query><Where><Eq><FieldRef Name='Tab' /><Value Type='Choice'>Tab 2</Value></Eq></Where></Query></View>");

                       

            tabDetailColl2 = list.getItems(camlTab);  

            tabContext.load(tabDetailColl2);  

         

            //tabContext.executeQueryAsync(onSucceededTab, onFailedTab);

     tabContext.executeQueryAsync(Function.createDelegate(this,function(){onSucceededTab("Tab 2",tabDetailColl2);}),

                                            Function.createDelegate(this, this.onFailedTab));

            }

       }

          }   

    function onSucceededTab(sender, args) {

           var enumerator = args.getEnumerator();  

           var markupTab = 'List is not found<br><br>';   

             var tabName=' ';

          while (enumerator.moveNext()) {   
          var listItem = enumerator.get_current();   

         tabName =listItem.get_item('Title');
         tabContent =listItem.get_item('Content');
         tabImage = listItem.get_item('Image').get_url();

    if (listItem.get_item('LinkText') && listItem.get_item('Hyperlink').get_url()) {

         tabLinkText =listItem.get_item('LinkText');
         tabHyperlink = listItem.get_item('Hyperlink').get_url(); 
    }   

         }   

         if(sender=='Tab 1'){
              document.getElementById('tabFirst').href = '#' + tabName;
      document.getElementById('tabFirst').innerHTML = tabName;
      document.getElementById('tab1').innerHTML = tabContent;
        document.getElementById('hyperlink1').innerHTML = tabLinkText;
      document.getElementById('hyperlink1').href = tabHyperlink;

    document.getElementById('image1').src = tabImage;
         }

              if(sender=='Tab 2'){
    document.getElementById('tabSecond').href = '#' + tabName;
      document.getElementById('tabSecond').innerHTML = tabName;
      document.getElementById('tab2').innerHTML = tabContent;

    document.getElementById('hyperlink2').innerHTML = tabLinkText;
      document.getElementById('hyperlink2').href = tabHyperlink;

      document.getElementById('image2').src = tabImage;
         }

    /////////////////CODE SUCCEED method/////////////////////////////////////////


    Paru

    Tuesday, January 17, 2017 11:20 AM

Answers

  • Hi sadomovalex

    I could solve the issue using the below code

          

    if (listItem.get_item('LinkText') && listItem.get_item('Hyperlink').get_url()) {


     tabLinkText =listItem.get_item('LinkText');
         tabHyperlink = listItem.get_item('Hyperlink').get_url();

    document.getElementById('hyperlink4').innerHTML = tabLinkText;
    document.getElementById('hyperlink4').href = tabHyperlink;

     } 

    this code could check for the null values .

    Thanks for your help

    Paru


    Paru

    • Marked as answer by Paru Upreti Friday, January 20, 2017 7:09 AM
    Thursday, January 19, 2017 7:00 PM

All replies

  • what is the question?

    Blog - http://sadomovalex.blogspot.com
    Dynamic CAML queries via C# - http://camlex.codeplex.com

    Tuesday, January 17, 2017 2:31 PM
  • Hi sadomovalex

    My question is, 

    i have tabs where data is coming from list, and that list has 3 columns where 2 are mandatory field and 1 field is not mandatory.

    When user leaves non mandatory field blank i want blank to be displayed on my page or else if user inputs any value it should display that value.

    I think its simple but hard to explain :(

    Please try and let me know the solution

    Thanks

    Paru


    Paru

    Tuesday, January 17, 2017 2:45 PM
  • Hi Paru,

    I suggest you provide the detailed information about your list(all field name and field type), and provide some screenshots for further research.

    Or please provide more details about how to reproduce this issue, others would get easier to research on it.

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Wednesday, January 18, 2017 7:44 AM
    Moderator
  • and what is the problem with provided code? Does it throw exception or shows incorrect values?

    Blog - http://sadomovalex.blogspot.com
    Dynamic CAML queries via C# - http://camlex.codeplex.com

    Wednesday, January 18, 2017 2:18 PM
  • Hi sadomovalex

    I could solve the issue using the below code

          

    if (listItem.get_item('LinkText') && listItem.get_item('Hyperlink').get_url()) {


     tabLinkText =listItem.get_item('LinkText');
         tabHyperlink = listItem.get_item('Hyperlink').get_url();

    document.getElementById('hyperlink4').innerHTML = tabLinkText;
    document.getElementById('hyperlink4').href = tabHyperlink;

     } 

    this code could check for the null values .

    Thanks for your help

    Paru


    Paru

    • Marked as answer by Paru Upreti Friday, January 20, 2017 7:09 AM
    Thursday, January 19, 2017 7:00 PM
  • Hi Paru,

    Thanks for sharing!  please mark the reply as answer, it will make others who stuck with the similar issue easier to search for valid solutions in this forum.

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Friday, January 20, 2017 1:21 AM
    Moderator