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

     


 


When opening task T1


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
  2. Add Reference of System.Data
  3. Write coding to fetch data

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.
  6. Complie the solution.
  7. 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.

     


  8. Recompile the code.
  9. Right click and Package the solution


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


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


  5. 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.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s