开发者

Getting last item from the SharePoint list using CAML queries

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开发者_如何学Go.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>


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜