1

I made a CAML query with the "CamlJS Console" Addon for Chrome. I joned 2 List to 1 relation list and my output from the query is always one item called "Kategorie" (category in english).

I want to write this into a field in the SharePoint newform.aspx, but the output i get is just Object Object.

I am not even sure if a join query is even possible through javascript because I am sure the query is valid.

At first: this is my caml query without using "include" :

<View><ViewFields><FieldRef Name='Kategorie' /></ViewFields><Joins><Join Type='INNER' ListAlias='cat'><Eq><FieldRef Name='KategorieID' RefType='ID' /><FieldRef Name='ID' List='cat' /></Eq></Join><Join Type='INNER' ListAlias='ctype'><Eq><FieldRef Name='ctypeID' RefType='ID' /><FieldRef Name='ID' List='ctype' /></Eq></Join></Joins><ProjectedFields><Field ShowField='Title' Type='Lookup' Name='Kategorie' List='cat' /><Field ShowField='Title' Type='Lookup' Name='Inhaltstyp' List='ctype' /></ProjectedFields><Query><Where><Contains><FieldRef Name='Inhaltstyp' /><Value Type='Text'>"+ contentTypeName.toString() +"</Value></Contains></Where></Query></View>

and this is the code built with the include:

var listItem = null;
var listItems1;
var listContentTypes = null;
var listItemEnumerator;
var oList;
var ctTypeID = undefined;
var contentTypeName;
var context;


function getContentTypeOfCurrentItem() {    

    listContentTypes = oList.get_contentTypes();
    context.load(listContentTypes);
    context.executeQueryAsync(Function.createDelegate(this, getContentTypeOfCurrentItemSucceeded), Function.createDelegate(this, onCtQueryFailed)); 
}



function getContentTypeOfCurrentItemSucceeded(sender, args) {    


    var ctid = ctTypeID.toString();
    var ct_enumerator = listContentTypes.getEnumerator();




    while (ct_enumerator.moveNext()) {

        var ct = ct_enumerator.get_current();   //durch enumerator designiertes element wird geladen

        if (ct.get_id().toString() == ctid) {

            contentTypeName = ct.get_name();    // read out Contenttype name 

            var list = context.get_web().get_lists().getByTitle("rel_cat_ci");
            var testQuery = new SP.CamlQuery();

            testQuery.set_viewXml("<View><Joins><Join Type='INNER' ListAlias='cat'><Eq><FieldRef Name='KategorieID' RefType='ID' /><FieldRef Name='ID' List='cat' /></Eq></Join><Join Type='INNER' ListAlias='ctype'><Eq><FieldRef Name='ctypeID' RefType='ID' /><FieldRef Name='ID' List='ctype' /></Eq></Join></Joins><ProjectedFields><Field ShowField='Title' Type='Lookup' Name='Kategorie' List='cat' /><Field ShowField='Title' Type='Lookup' Name='Inhaltstyp' List='ctype' /></ProjectedFields><Query><Where><Contains><FieldRef Name='Inhaltstyp' /><Value Type='Text'>"+ contentTypeName.toString() +"</Value></Contains></Where></Query></View>");


            listItems1 = list.getItems(testQuery);
            context.load(listItems1, "Include(Kategorie)");
            context.executeQueryAsync(Function.createDelegate(this, this.onRelQuerySucceeded), Function.createDelegate(this, this.onRelQueryFailed));  


        }
    }
}


function onCtQueryFailed(sender, args){

    alert("Query failed");

}


function onRelQuerySucceeded(sender, args) {
    var listItemInfo = "";                              //string for output
    listItemEnumerator = listItems1.getEnumerator();    //Indexed ref to queried elements

        while (listItemEnumerator.moveNext()) {
            var oListItem = listItemEnumerator.get_current();

            listItemInfo += oListItem.get_item("Kategorie");    
        }


    if(listItemInfo != ""){
        document.getElementById("cmdb_category_365755f2-3600-41d9-bf02-efb47430ebe6_$TextField").value = listItemInfo;
    }else{
        document.getElementById("cmdb_category_365755f2-3600-41d9-bf02-efb47430ebe6_$TextField").value = "Nicht Kategorisiert";
    }
}


function onRelQueryFailed(sender, args) {
    alert('Request failed.');
}



function getCtTypeListDefault() {   

    listContentTypes = oList.get_contentTypes();
    context.load(listContentTypes);
    context.executeQueryAsync(Function.createDelegate(this, getCtSuc), Function.createDelegate(this, getCtFail));

}


function getCtSuc() {   
    var ctTypeZero = listContentTypes.itemAt(0);
    document.getElementById("cmdb_category_365755f2-3600-41d9-bf02-efb47430ebe6_$TextField").value = ctTypeZero.get_name();
}


function getCtFail() {  
    alert("Something went really, really wrong! ContentType Liste nicht aufrufbar.");
}


function main(){


    context = new SP.ClientContext.get_current();
    oList = context.get_web().get_lists().getByTitle("cmdb_item");



    JSRequest.EnsureSetup();
    ctTypeID = JSRequest.QueryString["ContentTypeId"];

    if(ctTypeID != undefined){
        getContentTypeOfCurrentItem();
    }
    else{
        getCtTypeListDefault();
    }
}



SP.SOD.executeFunc('sp.js', 'SP.ClientContext', main)</code>
  • Is this the line producing 'object [Object]' document.getElementById("cmdb_category_...").value = listItemInfo;? If so, you need to debug that line and see what is in the object. – johnnycardy Sep 7 '15 at 8:22
1

Okay it was just

The function .get_lookupValue() which was left. I didn't know its a lookup field.

listItemInfo += oListItem.get_item("KategorieID").get_lookupValue();

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.