Tuesday, January 3, 2012

Sample Code Getting Data from SharePoint to Asp.Net.

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.SharePoint;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            SPSecurity.RunWithElevatedPrivileges(delegate()
        {
            using (SPSite site = new SPSite("http://vlnrm:100/prasad Data"))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPList lstDoc = web.Lists["Posts"];
                    SPQuery sQuery = new SPQuery();
                    sQuery.Query = "<OrderBy><FieldRef Name='ID' Ascending='False' /></OrderBy>";
                    //sQuery.ViewAttributes = "Scope='RecursiveAll'";
                    SPListItemCollection myColl = lstDoc.GetItems(sQuery);
                    Response.Write("<Table>");
                    Response.Write("<Tr>");
                    Response.Write("<Td>");
                    Response.Write("<Strong>");
                    Response.Write("Post Name");
                    Response.Write("</Strong>");
                    Response.Write("</Td>");
                    Response.Write("<Td>");
                    Response.Write("<Strong>");
                    Response.Write("Item ID");
                    Response.Write("</Strong>");
                    Response.Write("</Td>");
                    Response.Write("</Tr>");
                    string strOriginalUrl, strFileName = string.Empty;
                    int itemID;
                    if (myColl.Count > 0)
                    {
                        for (int i = 0; i < myColl.Count; i++)
                        {
                            SPListItem item = myColl[i];
                            //strDocUrl = item["FileRef"].ToString();
                            //strDocUrl = item["Title"].ToString();
                            //strDocUrl = strDocUrl.Substring(strDocUrl.IndexOf('#') + 1);
                            itemID = int.Parse(item.ID.ToString());
                            strOriginalUrl = web.Url + "/Lists/Posts/Post.aspx?ID=" + itemID;
                            strFileName = item["Title"].ToString();
                          
                            Response.Write("<Tr>");
                            Response.Write("<Td>");
                            Response.Write("<A href='" + strOriginalUrl + "' Target='_blank'>" + strFileName + "</A>");
                            Response.Write("</Td>");
                            Response.Write("<Td>");
                            Response.Write(itemID);
                            Response.Write("</Td>");
                            Response.Write("</Tr>");
                        }
                    }
                    Response.Write("</Table>");

                }
            }
        });
        }
    }
}

No comments:

Post a Comment