需要帮助使用 CSOM 和 CAML 查询获取 LookupList 值
Need help getting LookupList values using CSOM with a CAML Query
SHAREPOINT 2010 用户
我有一个充满项目的 SharePoint 列表,其中一个字段是主题。我希望能够 return 这些值。如果我使用实际字段标题,我将得到 [Object 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 或所查找字段的文本值,因此 returns 两者都可以。
尝试将 .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();
}
SHAREPOINT 2010 用户
我有一个充满项目的 SharePoint 列表,其中一个字段是主题。我希望能够 return 这些值。如果我使用实际字段标题,我将得到 [Object 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 或所查找字段的文本值,因此 returns 两者都可以。
尝试将 .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();
}