如何使用 javascript 读取 sharePoint 列表项值(当前项)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8319331/
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-08-24 05:33:29  来源:igfitidea点击:

How to read sharePoint List item value (current Item) using javascript

javascriptlistsharepoint

提问by SP10

I have to read Title and Locationfrom a picture library and display using CEWP.

我必须从图片库中读取标题和位置并使用 CEWP 显示。

Can someone suggest how to read SharePoint list item values using Javascript.

有人可以建议如何使用 Javascript 读取 SharePoint 列表项值。

回答by vmdominguez

You can use the JavaScript Client Object Model. Assuming the window's _spPageContextInfoobject is set with the webServerRelativeUrl, pageListId, and pageItemIdproperties initialized:

您可以使用 JavaScript 客户端对象模型。假设窗口的_spPageContextInfo对象设置与webServerRelativeUrlpageListIdpageItemId属性进行初始化:

var context = new SP.ClientContext(_spPageContextInfo.webServerRelativeUrl);
var list = context.get_web().get_lists().getById(_spPageContextInfo.pageListId);
var item = list.getItemById(_spPageContextInfo.pageItemId);

Then you can load the fields you need:

然后你可以加载你需要的字段:

context.load(item, "Title", "Location");
context.executeQueryAsync(Function.createDelegate(this, this.mySuccessFunction), Function.createDelegate(this, this.myErrorFunction));

itemwill now be populated with the fields you requested, and you can retrieve them like so:

item现在将填充您请求的字段,您可以像这样检索它们:

var itemTitle = item.get_item("Title");
var itemLocation = item.get_item("Location");

Note that you should use the display, not internal, names of the fields you want to load.

请注意,您应该使用要加载的字段的显示名称,而不是内部名称。

回答by Niranjan Singh

In SharePoint 2010 there are three different types of Client Object Model extension you can use. They are Managed Client Object Model, ECMAScript and silverlight extension.

在 SharePoint 2010 中,您可以使用三种不同类型的客户端对象模型扩展。它们是托管客户端对象模型、ECMAScript 和 Silverlight 扩展。

This link more close to your requirement How to: Retrieve Lists Using JavaScriptand How do you get the current list item in JavaScript?

此链接更接近您的要求如何:使用 JavaScript 检索列表以及 如何在 JavaScript 中获取当前列表项?

SP.ListOperation.Selection Methods

SP.ListOperation.Selection 方法

var value = SP.ListOperation.Selection.getSelectedItems();

Check following links for more information:
SharePoint 2010: Use ECMAScript to manipulate (Add/Delete/Update/Get) List Items
Accessing List Data using the JavaScript Client OM
Using the SP2010 Client Object Model to update a list itemHow to – SharePoint 2010 – JS client object model and UI advancements

检查以下链接以获取更多信息:
SharePoint 2010:使用 ECMAScript 操作(添加/删除/更新/获取)列表项
使用 JavaScript 客户端 OM 访问列表数据 使用
SP2010 客户端对象模型更新列表项如何 – SharePoint 2010 – JS 客户端对象模型和 UI 改进

回答by Nitin Rastogi

You can use jQuery to access the list items via SharePoint Web Services. Refer for example here - SharePoint Web Services with jQuery

您可以使用 jQuery 通过 SharePoint Web Services 访问列表项。请参阅此处的示例 - SharePoint Web Services with jQuery

回答by Brandon James

if _spPageContextInfo.pageItemId is undefined.
Use this function
function getUrlVars() {
var vars = [],
    hash;
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
for (var i = 0; i < hashes.length; i++) {
    hash = hashes[i].split('=');
    vars.push(hash[0]);
    vars[hash[0]] = hash[1];
}
return vars;
}
//THEN DO THIS
var id = getUrlVars()["ID"];
//THEN DO YOUR MAGIC
var context = new SP.ClientContext(_spPageContextInfo.webServerRelativeUrl);
var list = context.get_web().get_lists().getById(_spPageContextInfo.pageListId);
var item = list.getItemById(id);

回答by MishaU

In SP2010 there is new client APIs that allow you to interact with SharePoint sites from JavaScript

在 SP2010 中有新的客户端 API,允许您通过 JavaScript 与 SharePoint 网站进行交互

SharePoint 2010 Client Object Model

SharePoint 2010 客户端对象模型

JavaScript Class Library

JavaScript 类库