Prasad Bolla's SharePoint Blog

Click Here to go through the Interesting posts within my Blog.

Click Here to go through the new posts in my blog.

Monday, February 27, 2012

Retrieving Data from SharePoint Links List and keeping Hyperlink for a Lookup Column using Traditional Programming


using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;
using System.Security;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using System.Data;
[assembly:AllowPartiallyTrustedCallers]
namespace LinksWebPartSample
{
    [Guid("90cd9729-5313-4321-9cd5-b2693ebae470")]
    public class LinksWebPartSample : System.Web.UI.WebControls.WebParts.WebPart
    {

        public LinksWebPartSample()
        {
        }
        string strError = string.Empty;
        SPGridView sgvLinks;
        protected override void CreateChildControls()
        {
            try
            {
                sgvLinks = new SPGridView();
                sgvLinks.ID = "sgvLinks";
                sgvLinks.Attributes.Add("RunAt", "Server");
                sgvLinks.AutoGenerateColumns = false;
                this.Controls.Add(sgvLinks);

                HyperLinkField hLink = new HyperLinkField();
                hLink.HeaderText = "Link Name";
                hLink.DataNavigateUrlFields = new String[] { "ItemLinkURL" };
                hLink.DataTextField = "ItemLinkTitle";
                sgvLinks.Columns.Add(hLink);

                HyperLinkField hCategory = new HyperLinkField();
                hCategory.HeaderText = "Category Name";
                hCategory.DataTextField = "LookUpTitle";
                hCategory.DataNavigateUrlFields = new String[] { "LookUpURL"};
                sgvLinks.Columns.Add(hCategory);

                sgvLinks.DataSource = getLinksData();
                sgvLinks.DataBind();

            }
            catch (Exception ex)
            {
                strError += ex.ToString();
            }
        }
        public DataTable getLinksData()
        {
            DataTable dt = new DataTable();
            try
            {
                dt.Columns.Add("ItemLinkURL");
                dt.Columns.Add("ItemLinkTitle");
                dt.Columns.Add("LookUpTitle");
                dt.Columns.Add("LookUpURL");

                SPWeb currentWeb = SPControl.GetContextWeb(Context);
                SPList lstLinks = currentWeb.Lists["Links"];
                SPQuery sQuery = new SPQuery();
                sQuery.Query = "";
                SPListItemCollection linksColl = lstLinks.GetItems(sQuery);
                if (linksColl.Count > 0)
                {
                    DataRow dRow;
                    foreach (SPListItem item in linksColl)
                    {
                        dRow = dt.Rows.Add();
                        SPFieldUrlValue fieldValue = new SPFieldUrlValue(item["URL"].ToString());
                        string linkTitle = fieldValue.Description;
                        string linkUrl = fieldValue.Url;

                        SPFieldLookupValue lookupValue = new SPFieldLookupValue(item["Category"].ToString());
                        string lookUpTitle = lookupValue.LookupValue;
                        int lookUpID = int.Parse(lookupValue.LookupId.ToString());

                        string strLookUpURL = currentWeb.Url + "/LIsts/Links Category/DispForm.aspx?ID="+ lookUpID+"&Source="+Page.Request.Url;
                        dRow["ItemLinkURL"] = linkUrl;
                        dRow["ItemLinkTitle"] = linkTitle;
                        dRow["LookUpTitle"] = lookUpTitle;
                        dRow["LookUpURL"] = strLookUpURL;
                    }
                  
                }
                return dt;
            }
            catch (Exception ex)
            {
                strError += ex.ToString();
                return null;
            }
        }
    }
}

No comments:

Post a Comment