HyperLinkField in SPGridView using Moss 2007 Object model

No.of Views1923
Bookmarked0 times
Downloads 
Votes0
By  Dhananjay Kumar   On  16 Feb 2010 00:02:56
Tag : SharePoint , Development and Programming
HyperLinkField in SPGridView using Moss 2007 Object model
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Objective

This article is going to explain

1. How to work with SPGridView
2. How to add HypeLinkField on a SPGridView.
3. How to Put SPGridView on a web part.
4. How to deploy the web part on a sharepoint site.

Assumption

I have a SharePoint list called TestingHyperLink. Columns of SharePoint list are as below.

1. Name column is of type Single Line of Text.
2. Favorite WebSite column is of type HyperLink or Picture
3. Favorite Site column is of type HyperLink or Picture

Image Loading
Image Loading


When opening task T1

Image Loading

I have created a SharePoint view for the List. View contains only columns Name,Favorite WebSite and Favorite List. I have given the name of the view as MyView.

Displaying in SPGridView in Web Part

Note : I am using Visual Studio Extension 2008 for WSS 3.0 . If you don’t have installed on your visual studio download and install that. 

1. Create a New Web Part by selecting File –> New -> SharePoint -> Web Part 

Image Loading

2. Add Reference of System.Data

Image Loading

 

3. Write coding to fetch data 

C# Code

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.Data;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;

namespace WebPart1
{
    [Guid("b60d63e9-0784-4904-9aa3-9a3d25932441")]public class WebPart1 : System.Web.UI.WebControls.WebParts.WebPart
    {
        SPSite mySite;
        SPWeb myWeb;
        SPList myList;
        SPGridView myView;public WebPart1()
        {
        }protected override void CreateChildControls()
        {base.CreateChildControls();

            SPSite mySite = new SPSite("http://adfsaccount:2222/");
            SPWeb myWeb = mySite.OpenWeb();
            myList = myWeb.Lists["TestingHyperLink"];
            myView = new SPGridView();

            SPGridView sp = BindToGrid(myList, myView);
            Panel panel1 = new Panel();
            panel1.Controls.Add(sp);
        }private SPGridView BindToGrid(SPList myList, SPGridView myView)
        {//myView = new SPGridView();SPView sharepointview = myList.Views["MyView"];
            SPListItemCollection listCollection = myList.GetItems(sharepointview);
            DataTable dt = new DataTable();


            dt.Columns.Add("Name", typeof(string));
            dt.Columns.Add("FavWebSite");
            dt.Columns.Add("FavList");

            DataRow dtRow;foreach (SPListItem listitem in listCollection)
            {
                dtRow = dt.Rows.Add();
                dtRow["Name"] = listitem["Name"];string favwebsite = (string)listitem["Favorite WebSite"];string favwebsiteUri = GetUri(favwebsite);
                dtRow["FavWebSite"] = favwebsiteUri;string favlist = (string)listitem["Favorite List"];string favlistUri = GetUri(favlist);
                dtRow["FavList"] = favlistUri;

            }

            SPBoundField boundfield = new SPBoundField();
            boundfield.HeaderText = "Name";
            boundfield.DataField = "Name";
            myView.Columns.Add(boundfield);


            HyperLinkField hyperFieldfavsite = new HyperLinkField();string[] favsitearray = new string[1];
            favsitearray[0] = "FavWebSite";
            hyperFieldfavsite.DataTextField = "FavWebSite";
            hyperFieldfavsite.DataNavigateUrlFields = favsitearray;
            hyperFieldfavsite.DataNavigateUrlFormatString = "{0}";
            myView.Columns.Add(hyperFieldfavsite);


            HyperLinkField hyperFieldfavlist = new HyperLinkField();string[] favlistarray = new string[1];
            favsitearray[0] = "FavList";
            hyperFieldfavsite.DataTextField = "FavList";
            hyperFieldfavsite.DataNavigateUrlFields = favlistarray;
            hyperFieldfavsite.DataNavigateUrlFormatString = "{0}";
            myView.Columns.Add(hyperFieldfavlist);


            myView.AutoGenerateColumns = false;
            myView.DataSource = dt.DefaultView;
            myView.DataBind();// Panel1.Controls.Add(myView);return myView;



        }private string GetUri(string str)
        {string[] stemp = str.Split(",".ToCharArray());string s1 = stemp[0];string s2 = stemp[1];
            HyperLink h = new HyperLink();
            h.NavigateUrl = s1;return h.NavigateUrl;
        }


    }
}

Explanation

1. TestingHyperLink is name of the list.
2. MyView is name of the view.
3. BindToGrid method is just creating Data table. Adding the column to data table and populating the row.
4. HypeLinkField is used to populate the column as link.
5. GetUri method is constructing URI from the string.

4. Complie the solution.
5. To deploy put the link of the site collection. Where you want to deploy the web part. Right click on Solution then properties then click on Debug tab. In Start Browser field put URL of site collection.

Image Loading

6. Recompile the code.
7. Right click and Package the solution 

Image Loading

 8. Deploy the solution. Again right click on solution and click Deploy.

Image Loading

 

9. Do an IISRESET. Go to command prompt and type this IISRESET. This is to restart the IIS.
10. Open the SharePoint site.
11. Go to Site Action then Site Setting
12. Go to Site Collection Feature under Site Collection Administration tab. You should able to see the web part with SPGridView you deployed now.

Image Loading

 

13. Add web part from Edit mode to view the Grid View.

Conclusion

I have shown in this article, how to add HyperLink field in SPGridView and then deply SPGridView on a webpart. Thanks for reading.

 

 
Sign Up to vote for this article
 
About Author
 
Dhananjay Kumar
Occupation-Software Engineer
Company-Infosys Technolgies,Pune
Member Type-Gold
Location-India
Joined date-20 Jul 2009
Home Page-http://dhananjaykumar.net/
Blog Page-http://dhananjaykumar.net/
Dhananjay Kumar is Microsoft MVP on connected system. He blogs at http://dhananjaykumar.net/ . You can follow him http://twitter.com/debugmode_/ and reach him at dhananjay.25july@gmail.com
 
 
Other popularSectionarticles
Comments
By:RahulDate Of Posted:4/6/2011 9:31:58 AM
Group By in View
Excellent Article Dhananjay!!! I have one query here, in my default view I have grouped by couple of columns.So when I bind that columns to my SPGrid the list is not grouping ? is there any other this I need to do in the coding ?
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top