使用Javascript从Sharepoint列表的列表视图中检索项目

时间:2015-02-27 11:59:25

标签: javascript sharepoint-2013 sharepoint-designer

这是我的代码:\

此处pranav_list是列表名称,Main是该列表的视图。

<script type="text/javascript">
    function retrieveListViewItems(){
    alert('start');
    var cc = SP.ClientContext.get_current();
    var alllist = cc.get_web().get_lists().getByTitle('pranav_list');
    var view = alllist.get_views().getByTitle('Main');
    var caml = new SP.CamlQuery();
    caml.set_viewXml('<View><Query><Where><Leq>' + 
                '<FieldRef Name=\'ID\'/><Value Type=\'Number\'>100</Value>' + 
                '</Leq></Where></Query><RowLimit>8</RowLimit></View>'); 

    this.list = view.getItems(caml);
    cc.load(list);
   cc.executeQueryAsync(Function.createDelegate(this,this.QuerySucceeded5),Function.createDelegate(this,this.onQueryFailed));
    }
    function QuerySucceeded5(){
    var listiteminfo='';

    var listItemEnumerator = list.getEnumerator();
    while(listItemEnumerator.moveNext())
    {
      var listitem = listItemEnumerator.get_current();

     listiteminfo += '\nID:'+ listitem.get_id() +
                      '\nFirstName:' + listitem.get_item('Title') +
                      '\nLastName:' + listitem.get_item('LN') +
                      '\nAge:' + listitem.get_item('Age') +
                      '\n City:' + listitem.get_item('City');

    // alert(listiteminfo.toString());       
    }
    alert(listiteminfo.toString());
            }
    function onQueryFailed(){
    alert("Failed");
    }
    </script>

帮助。!!

2 个答案:

答案 0 :(得分:1)

由于JSOM API不支持从View中检索列表项,因此您可以考虑以下方法:

  • 检索视图的CAML查询
  • SP.List.getItems()方法
  • 提供该查询

实施例

function getItemsFromView(listTitle, viewTitle,success,error)
{
    var ctx = SP.ClientContext.get_current();
    var list = ctx.get_web().get_lists().getByTitle(listTitle);
    var view = list.get_views().getByTitle(viewTitle);
    ctx.load(view,'ViewQuery');
    ctx.executeQueryAsync(
        function() {
            var viewQry = "<View><Query>" + view.get_viewQuery() + "</Query></View>";
            getItems(listTitle,viewQry,success,error);
        },
        error);
}

function getItems(listTitle, queryText,success,error) 
{
    var ctx = SP.ClientContext.get_current();
    var list = ctx.get_web().get_lists().getByTitle(listTitle);
    var query = new SP.CamlQuery();
    query.set_viewXml(queryText);
    var items = list.getItems(query);
    ctx.load(items);
    ctx.executeQueryAsync(
        function() {
            success(items);
        },
        error
   );
}

用法

以下示例演示了如何从All Tasks列表中的Tasks视图中检索列表项:

getItemsFromView("Tasks", "All Tasks",
 function(items){
    for(var i = 0; i < items.get_count(); i++){
        var item = items.get_item(i);
        console.log(item.get_item('Title'));
    }
 },
 function(sender,args){ 
    console.log(args.get_message())
 });

答案 1 :(得分:0)

这可能对这里的一些人有所帮助。在我添加行以检查sp.js是否已加载之前,代码对我不起作用。

SP.SOD.executeFunc('sp.js', 'SP.ClientContext', sharePointReady);

function sharePointReady(){
   //Code goes here
}

另请注意,我使用的是当前网站以外的网站的上下文。请参阅下面的整个解决方案。谢谢瓦迪姆!真正的生命保护者。其他示例代码和文档代码对我不起作用。

&#13;
&#13;
<script type="text/javascript">

$(document).ready(function(){
    //Make sure the necessary SharePoint script file 'sp.js' is loaded before your code runs
    SP.SOD.executeFunc('sp.js', 'SP.ClientContext', sharePointReady);

    function sharePointReady(){

        getItemsFromView("My List", "All Items",
         function(items){
             for(var i = 0; i < items.get_count(); i++){
                 var item = items.get_item(i);
                 console.log(item.get_item('Title'));
             }
         },
         function(sender,args){ 
             console.log(args.get_message())
         });
    }
}); //End Document.ready

function getItemsFromView(listTitle, viewTitle,success,error)
{
    var ctx = new SP.ClientContext('https://mywebsite.name/site/subsite');
    var list = ctx.get_web().get_lists().getByTitle(listTitle);
    var view = list.get_views().getByTitle(viewTitle);
    ctx.load(view,'ViewQuery');
    ctx.executeQueryAsync(
        function() {
            var viewQry = "<View><Query>" + view.get_viewQuery() + "</Query></View>";
            getItems(listTitle,viewQry,success,error);
        },
        error);
}

function getItems(listTitle, queryText,success,error) 
{
    var ctx = new SP.ClientContext('https://mywebsite.name/site/subsite'');
    var list = ctx.get_web().get_lists().getByTitle(listTitle);
    var query = new SP.CamlQuery();
    query.set_viewXml(queryText);
    var items = list.getItems(query);
    ctx.load(items);
    ctx.executeQueryAsync(
        function() {
            success(items);
        },
        error
   );
}
</script>
&#13;
&#13;
&#13;