1

I have some javascript which is looking to a list, it returns more than one result. I want to be able to store each result in its own object. How can I do this? Please see my code for illustration thanks.

var _ClientContext  = null,
    _List           = "",
    _Field          = "",
    _SiteURI        = "",
    _Item           = "";

//how we create a constructor / class in OOP JS
var SharePointStuff = function(SiteURI) {
    this._SiteURI = SiteURI; 
    this.GetSetClientContext(this._SiteURI);
}

//how we create a method in OOP JS
SharePointStuff.prototype.GetListItem = function(List, Title, Field, Caml, RetrievingSettings) {
    this._List = List;
    this._Field = Field;
    this._Title = Title;

    var SPList = this.GetSetClientContext(this.SiteURI).get_web().get_lists().getByTitle(this._List);
    var CamlQuery = new SP.CamlQuery();
    //query gets settings
    CamlQuery.set_viewXml(Caml);
    var SPListItems = SPList.getItems(CamlQuery);
    this.SPListItems = SPListItems;
    this.GetSetClientContext(this.SiteURI).load(SPListItems);
    if(RetrievingSettings)
        this.GetSetClientContext(this.SiteURI).executeQueryAsync(Function.createDelegate(this,this.GetListItemSuccessSettings),Function.createDelegate(this,this.GetListItemFailureSettings));
    else
        this.GetSetClientContext(this.SiteURI).executeQueryAsync(Function.createDelegate(this,this.GetListItemSuccess),Function.createDelegate(this,this.GetListItemFailure));

}

SharePointStuff.prototype.GetListItemSuccessSettings = function(sender, args) {
    //loop through settings and return them as an array
    var PSTranslationSettings = { FieldSetting: {  }, RequiredLists: {  } };
    var ItemEnumerator = this.SPListItems.getEnumerator();
    while(ItemEnumerator.moveNext()) {
        var ListItem = ItemEnumerator.get_current();
        console.log(ListItem);
        console.log("ID: " + ListItem.get_item("ID") + " Value: " + ListItem.get_item("SettingValue"));
        PSTranslationSettings.FieldSetting["ID"] = ListItem.get_item("ID")[0];
        PSTranslationSettings.FieldSetting["SettingValue"] = ListItem.get_item("SettingValue")[0];
        PSTranslationSettings.RequiredLists["ID"] = ListItem.get_item("ID")[1];
        PSTranslationSettings.RequiredLists["SettingValue"] = ListItem.get_item("SettingValue")[1];
    }
    console.log(PSTranslationSettings);
    //return PSTranslationSettings;
}

SharePointStuff.prototype.GetListItemFailureSettings = function(sender, args) {

}


SharePointStuff.prototype.GetListItemSuccess = function(sender, args) {
    //loop through settings and return them as an array
    var ItemEnumerator = SPListItems.getEnumerator();
    while(ItemEnumerator.moveNext()) {
        var ListItem = ItemEnumerator.get_current();
    }
}

SharePointStuff.prototype.GetListItemFailure = function(sender, args) {

}

SharePointStuff.prototype.GetSetClientContext = function(SiteURI) {
    if(this._ClientContext == null) {
        this._ClientContext = new SP.ClientContext(SiteURI);
    }
    return this._ClientContext; 
}

function TranslationSystemInit() {
//setting variables
   var SPStuff = new SharePointStuff("https://pointsolutioncouk.sharepoint.com/sites/pssuite-uat");
// var ClientContext = SharePointStuff.GetSetClientContext("SITE_URI");

       //"<ViewFields>" +
      //"<FieldRef Name='ID' />"+
      //"<FieldRef Name='SettingValue' />"+
   //"</ViewFields>"+


   var Settings = SPStuff.GetListItem("LIST","PS_TranslationSystem_LookupField","SettingValue",
   "<View>"+
    "<Query>"+
           "<Where>"+
              "<Or>"+
                 "<Eq>"+
                    "<FieldRef Name='Title' />"+
                    "<Value Type='Text'>PS_TranslationSystem_LookupField</Value>"+
                 "</Eq>"+
                 "<Eq>"+
                    "<FieldRef Name='Title' />"+
                    "<Value Type='Text'>PS_TranslationSystem_RequiredLists</Value>"+
                 "</Eq>"+
              "</Or>"+
           "</Where>"+
           "</Query>"+
       "</View>",true),
   SPFormsClasses = ["ui-accordion-header","fd_title","ui-tabs-anchor"]; //define all classes for SPForms controls
}

document.onload = SP.SOD.executeFunc("sp.js","SP.ClientContext",TranslationSystemInit);

1 Answer 1

2

You can use an array which will store each of your object, example:

SharePointStuff.prototype.GetListItemSuccessSettings = function(sender, args) {
    var PSTranslationSettings = new Array();    
    var ItemEnumerator = this.SPListItems.getEnumerator();
    while(ItemEnumerator.moveNext()) {
        var ListItem = ItemEnumerator.get_current();
        var Setting = { FieldSetting: {  }, RequiredLists: {  } };
        Setting.FieldSetting["ID"] = ListItem.get_item("ID")[0];
        Setting.FieldSetting["SettingValue"] = ListItem.get_item("SettingValue")[0];
        Setting.RequiredLists["ID"] = ListItem.get_item("ID")[1];
        Setting.RequiredLists["SettingValue"] = ListItem.get_item("SettingValue")[1];
        PSTranslationSettings.push(Setting);
    }    
    return PSTranslationSettings;
}

Then you can loop through the PSTranslationSettings object to obtain each object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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