使用 ECMAScript/JavaScript 对象模型按内部名称获取 SharePoint 列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8777505/
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
Get SharePoint list by Internal Name using ECMAScript / JavaScript Object Model
提问by Ahmed Magdy
How can I load items from a SharePoint list using its InternalName? As far as I know I can get it using either Id or Title like the following:
如何使用其 InternalName 从 SharePoint 列表加载项目?据我所知,我可以使用 Id 或 Title 获取它,如下所示:
var clientContext = new SP.ClientContext('/News/');
var web = clientContext.get_web();
var list = web.get_lists().getById("{1DBA9283-0AFA-4FA1-9BBA-70D8D190971F}");
...
回答by Thorsten Hans
no the CSOM only offers methods to query lists by it's Id or Title.
不,CSOM 仅提供通过其 ID 或标题查询列表的方法。
See http://msdn.microsoft.com/en-us/library/ee549620.aspx
请参阅http://msdn.microsoft.com/en-us/library/ee549620.aspx
The SharePoint List Schema doesn't offer InternalNames at the moment. See the Schema description http://msdn.microsoft.com/en-us/library/ms415091.aspx
SharePoint 列表架构目前不提供 InternalNames。请参阅架构描述http://msdn.microsoft.com/en-us/library/ms415091.aspx
Thorsten
托尔斯滕
回答by Sandeep
Its always recommended getting lists using ListUrl , which is not changed when List Title changes.
它始终建议使用 ListUrl 获取列表,列表标题更改时不会更改。
回答by Shegit Brahm
I don't know if you mean that, but inside my JavaScript-File I'm able to use Object Model if I declare these three lines first.
我不知道你是不是这个意思,但在我的 JavaScript 文件中,如果我首先声明这三行,我就可以使用对象模型。
/// <reference name="MicrosoftAjax.js" />
/// <reference path="file://C:/Program Files/Common Files/Microsoft Shared/Web Server Extensions/14/TEMPLATE/LAYOUTS/SP.core.debug.js" />
/// <reference path="file://C:/Program Files/Common Files/Microsoft Shared/Web Server Extensions/14/TEMPLATE/LAYOUTS/SP.debug.js" />
/// <reference name="MicrosoftAjax.js" />
/// <reference path="file://C:/Program Files/Common Files/Microsoft Shared/Web Server Extensions/14/TEMPLATE/LAYOUTS/SP.core.debug.js" />
/// <reference path="file://C:/Program Files/Common Files/Microsoft Shared/Web Server Extensions/14/TEMPLATE/LAYOUTS/SP.debug.js" />
This is relevant code, works without call with {SelectedItem} or stuff:
这是相关代码,无需调用 {SelectedItem} 或其他内容即可工作:
var items = SP.ListOperation.Selection.getSelectedItems();
var listID = SP.ListOperation.Selection.getSelectedList();
var items = SP.ListOperation.Selection.getSelectedItems();
var listID = SP.ListOperation.Selection.getSelectedList();
This loads (parts) of the Client Object Model, so I guess there you can go on. Because this is supported with IntelliSense.
这会加载(部分)客户端对象模型,所以我想您可以继续。因为这是由 IntelliSense 支持的。
Edit2: The other way to get and use a list with JavScript only is
Edit2:另一种获取和使用列表的方法是仅使用 JavScript
var web;
var context;
var listTitle = "ListName";
function InitiateThisScript(itemId) {
context = new SP.ClientContext.get_current();
web = context.get_web();
list = web.get_lists().getByTitle(listTitle);
item = list.getItemById(itemId);
context.load(web;
context.load(list);
context.load(item);
context.executeQueryAsync(handleItem(item, list));
}
This way requires in your Elements.xml, where I defined my buttons, that you call it
这种方式需要在我定义按钮的 Elements.xml 中调用它
CommandAction="javascript:InitiateThisScript('{SelectedItemId}','');" />
Edit3: Be careful using this JavaScript without any security checks. Because for example you have delivered this solution to a site, which has lets say two lists. First one you suggested to have this JavaScript and a second one. If you have custom buttons that appears in both list than you work on second list, but using the buttons fire to the first list as long as it is possible.
Lets say you have a button that clears content and you have in both lists a column called "title". If you are on second list and press button "delete title" than on your first list the title from item with same itemId will be deleted. On your second list happens nothing.
This appears from visibility of your buttons and no check, if designated list is the one you are working on.
Edit3:在没有任何安全检查的情况下使用此 JavaScript 时要小心。因为例如您已将此解决方案交付给一个站点,该站点可以说是两个列表。第一个你建议使用这个 JavaScript 和第二个。如果您的自定义按钮出现在两个列表中,而不是在第二个列表中工作,但只要可能,使用这些按钮就会触发第一个列表。
假设您有一个清除内容的按钮,并且在两个列表中都有一个名为“标题”的列。如果您在第二个列表中并按下按钮“删除标题”而不是在第一个列表中,则具有相同 itemId 的项目的标题将被删除。在你的第二个清单上什么也没有发生。
如果指定列表是您正在处理的列表,则这会出现在您按钮的可见性上,无需检查。
Shegit
谢吉特
Edit: Scrolling my tabs I found this one: Retrieve items from a folder with EcmaScript & COM
编辑:滚动我的选项卡我发现了这个:Retrieve items from a folder with EcmaScript & COM
回答by Anatoly Mironov
I think that was you who asked the same question on sharepoint stackexchange. Just to link to my answer there, here is the link. There I give a complete example how you can get sharepoint lists using their "internalName" (url)
我想是你在 sharepoint stackexchange 上问了同样的问题。只是为了链接到我的答案,这里是链接。在那里我给出了一个完整的例子,你如何使用他们的“internalName”(url)获取共享点列表