javascript 读取列表项 Sharepoint 2010 客户端对象模型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12696061/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 16:54:09  来源:igfitidea点击:

Reading the list items Sharepoint 2010 client object model

javascriptsharepointsharepoint-2010sharepoint-api

提问by user346514

I have a list where I am storing the image URLs and I am trying to read list of items and display the images on the page. For that I wrote the script something like below....

我有一个用于存储图像 URL 的列表,我正在尝试读取项目列表并在页面上显示图像。为此,我编写了如下所示的脚本....

<script type="text/javascript">
    function ViewItem()
    {
        var myQueryString = '<Query><Where><Eq><FieldRef Name="Anchor" /><Value 

Type="Boolean">1</Value></Eq></Where></Query>'; 

        var context = new SP.ClientContext.get_current();
        var web = context.get_web();
        var list = web.get_lists().getByTitle('AnchorImageList');
        var myquery = new SP.CamlQuery();

        myquery.set_viewXml(myQueryString); 

        myItems = list.getItems(myquery);


        context.load(myItems, 'Include(Title,ImageURL)');
        context.executeQueryAsync(Function.createDelegate(this, this.success), 

Function.createDelegate(this, this.failed));
    }
    function success() 
    {

        var LinkURL= "";
        var ImageURL="";
        var ListEnumerator = this.myItems.getEnumerator();
        while(ListEnumerator.moveNext())
        {
            var currentItem = ListEnumerator.get_current();
            LinkURL = currentItem.get_item('Title') ;
            ImageURL= currentItem.get_item('ImageURL');
            document.write('<img src="' + ImageURL+ '"+>');
            alert(LinkURL);

        }

    }
    function failed(sender, args) 
    {
        alert("failed. Message:" + args.get_message());
    }
</script>
<a href="#" onclick="Javascript:ViewItem();">View Items</a>

In my CAML query I am trying to filter items which are tagged yes for "Anchor?"(yes/no column).

在我的 CAML 查询中,我试图过滤为“锚点?”(是/否列)标记为是的项目。

But I am seeing all the results even though I tagged few items not to display. What I am doing wrong here. Please someone help me. Also,after the images are loaded on the page, the page is still showing the wheel as if it is processing something. Do I need to do something for this?

但是即使我标记了一些不显示的项目,我也看到了所有结果。我在这里做错了什么。请有人帮助我。此外,在页面上加载图像后,页面仍然显示轮子,好像它正在处理某些事情。我需要为此做些什么吗?

回答by Maksym

try this one:

试试这个:

<View>
  <Query>
    <Where>
      <Eq>
        <FieldRef Name="Anchor" />
        <Value Type="Boolean">1</Value>
      </Eq>
    </Where>
  </Query>
</View>

in case if it doesn't work for you, follow next steps:

如果它不适合您,请按照以下步骤操作:

  1. Create a list view using standard functionality.
  2. Open it at SharePoint Designier and just copy CAML query from the code.
  1. 使用标准功能创建列表视图。
  2. 在 SharePoint Designer 中打开它,然后从代码中复制 CAML 查询。

Hope this will help.

希望这会有所帮助。

回答by CBono

Remove the Querytags from the CAML query stored in myQueryString. The tags are added implicitly when the query is run.

卸下Query从存储在CAML查询标签myQueryString。运行查询时会隐式添加标签。

It's tripped me up before, too. The insidious thing about it is that the query won't ever fail outright; sometimes it works, sometimes it doesn't, making it a pain to debug.

它以前也让我绊倒过。它的阴险之处在于查询永远不会彻底失败。有时它起作用,有时不起作用,这使得调试变得很痛苦。

回答by J Ames

I found that if you use just single quotes in your CAML query it will work. Mixing double and single quotes Hymans up the query for some reason. Hope that helps.

我发现如果您在 CAML 查询中只使用单引号,它将起作用。由于某种原因,混合使用双引号和单引号会增加查询。希望有帮助。