使用 CAML 查询从 SharePoint 列表中获取最后一项

发布于 2024-11-08 02:11:49 字数 1786 浏览 1 评论 0原文

我想从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

メ斷腸人バ 2024-11-15 02:11:49

对于 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文