2011-11-29 99 views

回答

3

在SharePoint 2010中,可以使用三種不同類型的客戶端對象模型擴展。它們是託管客戶端對象模型,ECMAScript和silverlight擴展。

這個環節更貼近您的需求How to: Retrieve Lists Using JavaScriptHow do you get the current list item in JavaScript?

SP.ListOperation.Selection Methods

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

檢查以下詳細信息的鏈接:
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 item How to – SharePoint 2010 – JS client object model and UI advancements

+0

我收到錯誤「TypeError:SP.ListOperation is undefined」。你能提出任何解決這個問題的建議嗎? –

+0

我試着把'ExecuteOrDelayUntilScriptLoaded'(ReadCustomvalue,「sp.js」)和SP.SOD.executeFunc('sp.js','SP.ClientContext',ReadCustomvalue'')放在一起。但他們都沒有解決這個問題。 –

15

交互時,您可以使用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); 

然後,你可以加載你需要的字段:

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

item現將字段填入你要求,你可以像這樣進行檢索:

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

注意您應該使用顯示內容,而不是內部要加載的字段名稱。

+1

我想從_spPageContextInfo全局讀取pageItemId,但它沒有該屬性。我在展示形式。幫幫我? :\ – Renan

+2

Nevermind ...我剛剛檢查出來,該屬性僅當列表是文檔庫時才存在。無論如何,好的答案。 – Renan

+0

在SP 2013中不起作用 – kevin

1
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); 
相關問題