如何从CAML QUERY sharepoint

时间:2018-05-08 08:52:33

标签: javascript jquery sharepoint caml

在我的项目中有3个列表

  • 有2个字段的Lis​​teBatiment(我 DListeBatiment ,CodeBatiment)
  • 包含3个字段的Lis​​teEtage( IDListeEtage ,CodeEtage,IDListeBatiment)
  • 包含3个字段的Lis​​teLocal( IDListeLocal ,CodeLocal,IDListeEtage)

链接字段

  • ListeBatiment和ListeEtage通过lookupField IDListeBatiment在SHAREPOINT中链接
  • ListeEtage和ListeLocal通过lookupField IDListeEtage链接SHAREPOINT

Code JAVASCRIPT检索CodeEtage和CodeLocal:

var my_caml_query=
  "<View>"+
      "<ViewFields>"+
          "<FieldRef Name='CodeLocal' />"+
          "<FieldRef Name='CodeEtage' />"+
      "</ViewFields>"+
      "<Joins>"+
          "<Join Type='INNER' ListAlias='ListeEtage'>"+
              "<Eq>"+
                  "<FieldRef Name='IDListeEtage' RefType='ID' />"+
                  "<FieldRef Name='ID' List='ListeEtage' />"+
              "</Eq>"+
          "</Join>"+
      "</Joins>"+
      "<ProjectedFields>"+
          "<Field ShowField='CodeEtage' Type='Lookup' Name='CodeEtage' List='ListeEtage' />"+
      "</ProjectedFields>"+
  "</View>";

RecupCodeEtage('ListeEtage',my_caml_query);

function RecupCodeEtage(listName, CAML) {           
  var clientContext = SP.ClientContext.get_current();
  var oList = clientContext.get_web().get_lists().getByTitle(listName);
  var camlQuery = new SP.CamlQuery();
  camlQuery.set_viewXml(CAML);
  this.collListItem = oList.getItems(camlQuery);
  clientContext.load(collListItem);
  clientContext.executeQueryAsync(Function.createDelegate(this, function(){SuccessRecupCodeEtage(collListItem);}), Function.createDelegate(this, this.onQueryFailed));
};
function SuccessRecupCodeEtage(){
// code here if ok
}
function onQueryFailed()
{
// ;...
}

所以,我不知道如何处理以检索CodeLocal,CodeEtage和codeBatiment。

有人有解决方案或想法吗?

感谢

1 个答案:

答案 0 :(得分:0)

这是我的测试代码供您参考。

enter image description here

<script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.js"></script>
    <script type="text/javascript">
        function useRESTwithCAML(listName, CAML) {
            var clientContext = new SP.ClientContext.get_current();
            var oList = clientContext.get_web().get_lists().getByTitle(listName);

            var camlQuery = new SP.CamlQuery();
            camlQuery.set_viewXml(CAML);
            this.collListItem = oList.getItems(camlQuery);

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

        };

        var CAMLJoins =
               "<Joins>" +
               "<Join Type='INNER' ListAlias='ListeEtage'>" +
               "<Eq>" +
               "<FieldRef Name='IDListeEtage' RefType='Id'/>" +
               "<FieldRef List='ListeEtage' Name='ID'/>" +
               "</Eq>" +
               "</Join>" +
               "<Join Type='INNER' ListAlias='ListeBatiment'>" +
               "<Eq>" +
               "<FieldRef List='ListeEtage' Name='IDListeBatiment' RefType='Id'/>" +
               "<FieldRef List='ListeBatiment' Name='ID'/>" +
               "</Eq>" +
               "</Join>" +               
               "</Joins>";

        var CAMLProjFlds =
            "<ProjectedFields>" +
            "<Field Name='Batiment' Type='Lookup' List='ListeBatiment' ShowField='CodeBatiment'/>" +
            "<Field Name='Etage' Type='Lookup' List='ListeEtage' ShowField='CodeEtage'/>" +            
            "</ProjectedFields>";

        var ViewXml = "<View><Query></Query><ViewFields><FieldRef Name='Batiment'/><FieldRef Name='Etage'/><FieldRef Name='CodeLocal'/></ViewFields>" + CAMLProjFlds + CAMLJoins + "<RowLimit>100</RowLimit></View>";

        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('Batiment').get_lookupValue() +
                    '\nDLastName: ' + oListItem.get_item('Etage').get_lookupValue() +
                    '\nDLastName: ' + oListItem.get_item('CodeLocal');
            }

            alert(listItemInfo.toString());
        }

        function onQueryFailed(sender, args) {

            alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
        }
    </script>

    <input id="Button1" onclick="useRESTwithCAML('ListeLocal', ViewXml)" type="button" value="testCAMLJoin" />