[英]Need help getting LookupList values using CSOM with a CAML Query
SHAREPOINT 2010用戶
我有一個充滿項目的SharePoint列表,並且其中一個字段是“主題”。 我希望能夠返回這些值。 如果使用實際字段“標題”,則將獲得[Object Object],而不是對象值。 我想知道我使用的術語是否有問題,因為它們與.Net非常相似。
無論如何,這是我到目前為止所擁有的。
<!DOCTYPE html>
<%@ Page language="C#" %>
<%@ Register Tagprefix="SharePoint"
Namespace="Microsoft.SharePoint.WebControls"
Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<html xmlns:mso="urn:schemas-microsoft-com:office:office" xmlns:msdt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882">
<head>
<meta name="WebPartPageExpansion" content="full" />
<!-- the following 5 js files are required to use CSOM -->
<script type="text/javascript" src="/_layouts/1033/init.js"></script>
<script type="text/javascript" src="/_layouts/MicrosoftAjax.js"></script>
<script type="text/javascript" src="/_layouts/sp.core.js"></script>
<script type="text/javascript" src="/_layouts/sp.runtime.js"></script>
<script type="text/javascript" src="/_layouts/sp.js"></script>
<script type="text/javascript" src="//code.jquery.com/jquery-1.11.3.min.js"></script>
<SharePoint:FormDigest ID="FormDigest1" runat="server"></SharePoint:FormDigest>
<script type="text/ecmascript">
window.onload = function OnLoad() {
SP.SOD.executeFunc('sp.js','SP.ClientContext',retrieveListItems());
var oList;
function retrieveListItems() {
var Url = '/Policies';
var clientContext = new SP.ClientContext(Url); //.(Url); //SP.ClientContext.get_current();
var oList = clientContext.get_web().get_lists().getByTitle('HRPolicies');
var oListTags = clientContext.get_web().get_lists().getByTitle('TopicsSF');
//var field = oList.get_fields().getByInternalNameOrTitle("HRTopics");
//var lookupField = clientContext.CastTo<FieldLookup>(field);
//var childIdField = oList["HRTopics"] as FieldLookupValue[];
//if (childIdField != null)
//{
// foreach(var lookupValue in childIdField)
// {
// var childId_Value = lookupValue.LookupValue;
// var childId_Id = lookupValue.LookupId;
// }
//}
//end if no working, try field itself as getllokup as
var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml(multiLookupValues);
// this.collListItem = oList.getItems(camlQuery);
var PertinentItems = oList.getItems(camlQuery);
clientContext.Load(item, i => i["HRTopics"]);
clientContext.Load(items);
clientContext.ExecuteQuery();
var lookupValues = new ArrayList();
FieldLookupValue[] values = item["HRTopics"] as FieldLookupValue[];
foreach (ListItem listItem in items)
{
var lookupValue = new FieldLookupValue { LookupId = listItem.Id };
lookupValues.label;
}
item.ParseAndSetFieldValue(lookupColumnName, null);
item["HRTopics"] = lookupValues.ToArray();
ctx.ExecuteQuery();
// var lookupListId = new Guid(lookupField.LookupList);
// var lookupList = clientContext.Web.Lists.GetById(lookupListId);
//
// clientContext.load(collListItem,lookupField);
// works alert("birds");
// clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
clientContext.executeQueryAsync(onQuerySucceeded, onQueryFailed);
//
//}
function onQuerySucceeded(sender, args) {
var listItemInfo = '';
var listItemEnumerator = collListItem.getEnumerator();
while (listItemEnumerator.moveNext()) {
var oListItem = listItemEnumerator.get_current();
listItemInfo += '\nID: ' + oListItem.get_id() +
'\nTitle: ' + oListItem.get_item('Title') +
'\nHRTopics: ' + oListItem.get_item('HRTopics')
}
document.getElementById('realData').innerHTML = listItemInfo.toString();
}
function onQueryFailed(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
}
</script>
<!--[if gte mso 9]>
<SharePoint:CTFieldRefs runat=server Prefix="mso:" FieldList="FileLeafRef,WikiField,_dlc_DocId,_dlc_DocIdUrl,_dlc_DocIdPersistId"><xml>
<mso:CustomDocumentProperties>
<mso:_dlc_DocId msdt:dt="string">SCP5UENJTFED-1359-20</mso:_dlc_DocId>
<mso:_dlc_DocIdItemGuid msdt:dt="string">7d65ee24-642b-450f-8285-a1fd946836f4</mso:_dlc_DocIdItemGuid>
<mso:_dlc_DocIdUrl msdt:dt="string">site/_layouts/DocIdRedir.aspx?ID=SCP5UENJTFED-1359-20, SCP5UENJTFED-1359-20</mso:_dlc_DocIdUrl>
</mso:CustomDocumentProperties>
</xml></SharePoint:CTFieldRefs><![endif]-->
</head>
<body>
<div id="realData">
</div>
</body>
</html>
通過JavaScript對象模型為查找列返回的值是一個復合對象(具有多個屬性),而不是原始或字符串。 您可能要訪問所查找項目的ID或所查找字段的文本值,因此它都返回。
嘗試將.get_lookupValue()
添加到oListItem.get_item('HRTopics')
的末尾。
編輯:
如果查找列允許多個值,則.get_item()
返回的對象將是對象數組; 陣列本身不支持.get_lookupValue()
方法。 您將需要遍歷數組中的元素,並在每個元素上調用.get_lookupValue()
以獲得所需的值。
var valueString = "";
var values = oListItem.get_item('HRTopics');
for(var i in values){
valueString += values[i].get_lookupValue();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.