使用 CAML 查询从 SharePoint 列表中获取最后一项
我想从 SharePoint 列表中获取最后一项,但由于我是 SharePoint 新手,所以我很难完成任何事情。首先,这个CAML API很难找,然后我也确实没有任何编程经验。
无论如何,这是我在 ASPX 页面中的逻辑。我想显示最后一项:
<html>
<head>
<meta name="WebPartPageExpansion" content="full" />
<meta name="ProgId" content="SharePoint.WebPartPage.Document" />
<script runat="server" type="">
protected void sevak(object sender, EventArgs e)
string lastitem;
try {
using (SPSite objsite = new SPSite()) {
using (SPWeb objWeb = objSite.OpenWeb(....)) {
SPList objList = objWeb.Lists["List"];
SPQuery objQuery = new SPQuery();
objQuery.Query = "<OrderBy><FieldRef Name='Number' Ascending='False'/></OrderBy><RowLimit>1</RowLimit>";
objQuery.Folder = objList.RootFolder;
SPListItemCollection colItems = objList.GetItems(objQuery);
if (colItems.Count>0) {
lastitem=colItems[0];
}
}
}
}
catch (Exception ex) { }
return lastitem;
</script>
<SharePoint:CssLink runat="server"></SharePoint:CssLink>
<SharePoint:ScriptLink runat="server" language="javascript" name="core.js"></SharePoint:ScriptLink>
</head>
<body>
<form id="form1" runat="server">
<h1>T-Site</h1>
<p>
<asp:Button runat="server" Text="Submit" id="Button1" OnClick="sevak" OnClientClick="javascript:window:alert('Your Request for a document number has been received');"></asp:Button>
</p>
</body>
</form>
</html>
I want to get the last item from a SharePoint list, but since I am new to SharePoint, I am having a really hard time accomplishing anything. First of all, this CAML API is tough to find, then I really don't have any programming experience either.
Anyway, here's my logic in a ASPX page. I want to display the last item:
<html>
<head>
<meta name="WebPartPageExpansion" content="full" />
<meta name="ProgId" content="SharePoint.WebPartPage.Document" />
<script runat="server" type="">
protected void sevak(object sender, EventArgs e)
string lastitem;
try {
using (SPSite objsite = new SPSite()) {
using (SPWeb objWeb = objSite.OpenWeb(....)) {
SPList objList = objWeb.Lists["List"];
SPQuery objQuery = new SPQuery();
objQuery.Query = "<OrderBy><FieldRef Name='Number' Ascending='False'/></OrderBy><RowLimit>1</RowLimit>";
objQuery.Folder = objList.RootFolder;
SPListItemCollection colItems = objList.GetItems(objQuery);
if (colItems.Count>0) {
lastitem=colItems[0];
}
}
}
}
catch (Exception ex) { }
return lastitem;
</script>
<SharePoint:CssLink runat="server"></SharePoint:CssLink>
<SharePoint:ScriptLink runat="server" language="javascript" name="core.js"></SharePoint:ScriptLink>
</head>
<body>
<form id="form1" runat="server">
<h1>T-Site</h1>
<p>
<asp:Button runat="server" Text="Submit" id="Button1" OnClick="sevak" OnClientClick="javascript:window:alert('Your Request for a document number has been received');"></asp:Button>
</p>
</body>
</form>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于 CAML 查询,使用 U2U CAML 查询生成器工具,它非常易于使用,
其次,您必须在安装了 SharePoint 的服务器上执行对象模型代码,逻辑是正确的,但我发现您的语法存在一些问题,
请从这篇文章中获取帮助
http://www.a2zdotnet.com/View.aspx?Id=114
for CAML query use U2U CAML query builder tool its very easy to use
second you have to execute your object model code on a server on which SharePoint is installed logic is correct but i see some where problem with your syntax
for syntax take help from this post
http://www.a2zdotnet.com/View.aspx?Id=114