Blog Home  Home RSS 2.0 Atom 1.0 CDF  
The Efficient Coder - Custom WebParts in SharePoint 2007
There has got to be a better way of communicating with our computers!
 
 Sunday, February 04, 2007

Custom WebParts in SharePoint 2007

Creating a custom WebPart in SharePoint 2007 isn't all that terribly difficult, just a few basic steps that may not be completely obvious...in addition if you want to deploy your WebPart on SP 2007 it may contain some additional resources such as gif's or java scripts.  Wouldn't it be nice if you could just deploy one assembly with everything you need?  Well that's possible so let's do that as well...

  1. To start out, just create a new VS.NET 2005 class library project and add a reference to "System.Web".
  2. Go ahead and setup a reasonable sounding Assembly Name and Default NameSpace, these will be important when registering your component with Share Point 2007.
  3. While your are changing your project settings, find the Signing Tab and make sure "Sign the assembly" is checked.  You will then need to go ahead and create a new KeyFile.  So far straight forward right?
  4. Now let's add our web part, to do this simply add an class to your project.
  5. The code in your web part should look something like Not a example of good coding but that isn't the point ;-):

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.Reflection;
using System.Data;
using System.Data.SqlClient;
using System.ComponentModel;

namespace SoftwareLogistics.SharePointTest
{

   [Guid("72a24b71-f3a9-4fee-9272-4f3c27c87559")] 
   
public class SampleSP2007Part : System.Web.UI.WebControls.WebParts.WebPart
   
{
      
System.Web.UI.WebControls.DataGrid grdTime;

      protected
override void CreateChildControls()
      {
         
Image img = new Image();
         
img.ImageUrl = Page.ClientScript.GetWebResourceUrl(this.GetType(), "SoftwareLogistics.SharePointTest.Images.Target.gif");

         grdTime = new System.Web.UI.WebControls.DataGrid();
         
string dsn = "server=??????;database=??????;user id=????;password=?????";

         
SqlDataAdapter da = new SqlDataAdapter("select top 5 * from usv_time_tracking order by start_date desc", new SqlConnection(dsn));
         
DataTable tblTime = new DataTable("My Time");

         da.Fill(tblTime);
         grdTime.DataSource = tblTime;
         grdTime.DataBind();

         Controls.Add(img);
         Controls.Add(
new LiteralControl("<br/>"));
         Controls.Add(grdTime);

         base.CreateChildControls();
      }

      protected override void Render(HtmlTextWriter writer)
      {
         writer.Write(
"<h4>Hello World!</h4>");
         
base.Render(writer);
      }
   }
}

Before installing this to SharePoint 2007, let's complete this section by talking about what we need to do to embed the resources with the assembly.

  1. Create an Image directory within your project and copy the Image there:
  2. You can see that the name including the NameSpace is "SoftwareLogistics.SharePointTest.Images.Target.gif" (Case is important here)
  3. Once you copied the image there you need to make sure you click on "Properties" and then set the Build Action to "Embedded Resource" for Target.gif, very important to do this...
  4. Now open up your "AssemblyInfo.cs" file that is in the Properties folder of your project.  Add a line to it similar to
    [assembly: WebResource("SoftwareLogistics.SharePointTest.Images.Target.gif","image/gif")
  5. You will also need to add using System.Web.UI; to the top of your AssemblyInfo.cs.

  6. Finally in the code you can set the url of the image as:
    img.ImageUrl = Page.ClientScript.GetWebResourceUrl(this.GetType(), "SoftwareLogistics.SharePointTest.Images.Target.gif");

Now your assembly is ready to go all you need to do is turn that into and assembly and your are ready to incorporate it into your SharePoint 2007 site, you can even us this as a WebPart or even a standard Server Control on any ASP.NET 2.0 site.

To install the the component on the SharePoint2007 server you need to do the next couple of steps...

  1. First we need to get our component into the GAC so just drag it into the file pane for Windows\Assembly, you can see the my file here:
  2. Make a note of the Public Key Token, you will need that to register your part with SharePoint, you can also right mouse click on the assembly click on Properties and copy the Pulbic Key Token from there.
  3. The final step to make it appear as a "potential" web part within SharePoint is to register the component in the Web.Config file.  So find Web.Config for the SharePoint instance you want this work with and open it in your favorite XML editor.
  4. Find the section labeled "SafeControls" and add the following line (you can always just copy one of the existing lines and fill in your information.
     <SafeControl Assembly="SoftwareLogistics.SharePointTest, Version=1.1.0.0, Culture=neutral, PublicKeyToken=8220d66cd77f3b8d" Namespace="SoftwareLogistics.SharePointTest" TypeName="*" Safe="True" />

At this point SharePoint knows about your WebPart, but you'll need to make it part of the Gallery so open up your SharePoint site and do these final steps

  1. Click on "Site Actions" and then "Site Settings"
  2. Within the "Galleries" section click on "Web Parts"
  3. Click on New, if all went well you should see the part you created within the list, if you don't see it there, go ahead and reset IIS.
  4. Put a check mark next to your new web part and then click the "Populate Gallery" button.

At this point your web part is ready to be included just like any other WebPart.

Happy Coding!

- ec

2/4/2007 1:32:43 PM (Eastern Standard Time, UTC-05:00)  #    Comments [0]   ASP.NET | SharePoint 2007 | Software Engineering  |  Trackback
Tracked by:
http://9qk-information.info/08785309/hotel-kalos-giardino-naxos.html [Pingback]
http://9oe-information.info/74569851/movie-teather-location.html [Pingback]
http://9oq-information.info/08139325/index.html [Pingback]
http://9ra-information.info/42662051/index.html [Pingback]
http://9st-information.info/96593584/index.html [Pingback]
http://9rr-information.info/39301552/weaving-school-westmont-illinois.html [Pingback]
http://9sd-information.info/78681631/index.html [Pingback]
http://9rn-information.info/02774729/index.html [Pingback]
http://9rc-information.info/66566305/index.html [Pingback]
http://9sd-information.info/33351537/gastronomia-italiana-ricetta-cucina.html [Pingback]
http://9rq-information.info/29091341/credit-problem-student-loan.html [Pingback]
http://9tc-free-porn.info/43221308/young-chubby-teens.html [Pingback]
http://9uabg-free-porn.info/38091556/index.html [Pingback]
http://9uaao-free-porn.info/72293051/prostitute-story-erotica.html [Pingback]
http://9uaal-free-porn.info/93192858/index.html [Pingback]
http://9tr-free-porn.info/72723267/index.html [Pingback]
http://9uack-free-porn.info/95138534/a-book-of-teenage-quotes.html [Pingback]
http://9uabq-free-porn.info/22620038/new-baby-gear.html [Pingback]
http://9uaen-le-informazioni.info/42711882/index.html [Pingback]
http://9uaea-le-informazioni.info/66773008/index.html [Pingback]
http://9uafr-le-informazioni.info/02710176/index.html [Pingback]
http://9uafk-le-informazioni.info/89979253/index.html [Pingback]
http://9uaek-le-informazioni.info/96429015/index.html [Pingback]
http://9uaen-le-informazioni.info/65146644/index.html [Pingback]
http://9uaes-le-informazioni.info/68740589/index.html [Pingback]
http://9uahg-le-informazioni.info/42385708/index.html [Pingback]
http://9uahn-le-informazioni.info/01277909/soluzioni-rayman-3.html [Pingback]
http://9uahi-le-informazioni.info/35361904/ps2-bitcomet.html [Pingback]
http://9uagj-le-informazioni.info/35744967/demolitore-tassellatore.html [Pingback]
http://9uagr-le-informazioni.info/39139768/doriana-cherchi.html [Pingback]
http://9uahj-le-informazioni.info/47451817/index.html [Pingback]
http://9uagf-le-informazioni.info/79138685/index.html [Pingback]
http://9uajo-free-porn.info/80604020/ho-layout-pictures.html [Pingback]
http://9uakf-free-porn.info/13331570/index.html [Pingback]
http://9ualt-free-porn.info/00991606/index.html [Pingback]
http://9uaid-free-porn.info/85661070/index.html [Pingback]
http://9uaik-free-porn.info/33759151/index.html [Pingback]
http://9uair-free-porn.info/50504097/video-game-hisytory.html [Pingback]
http://9ualg-free-porn.info/77824867/index.html [Pingback]
http://freewebs.com/aspxfaq/10/sitemap7.html [Pingback]
http://freewebs.com/toltom/09/sitemap8.html [Pingback]
http://freewebs.com/toltom/03/ohio-valley-bank.html [Pingback]
http://freewebs.com/toltom/08/sitemap1.html [Pingback]
http://freewebs.com/toltom/13/www-lonely-wolf-com-mix.html [Pingback]
http://fartooblog.tripod.com/123.html [Pingback]
http://fartooblog.tripod.com/184.html [Pingback]
http://zoedfw.org/sitemap50.html [Pingback]
http://zbal5s.org/national-amusement.html [Pingback]
http://topslots.nl.eu.org/02/sitemap20.html [Pingback]
http://freewebs.com/amexa/44/www-excaliburhotel-com.html [Pingback]
http://freewebs.com/amexa/29/game-winners.html [Pingback]
http://freewebs.com/amexa/28/wachovia-online.html [Pingback]
http://pinofranc.homestead.com/01/mobile-satellite-tv.html [Pingback]
http://pinofranc.homestead.com/00/window.html [Pingback]
http://pinofranc.homestead.com/03/syracuse-basketball.html [Pingback]
http://lagxz-xxx.com/free-milf-porn.html [Pingback]
http://aixpj-www.com/lesbian-foot-sex.html [Pingback]
http://minveenews.angelfire.com/80.html [Pingback]
http://lasduunews.angelfire.com/77.html [Pingback]
http://freewebs.com/madfeenews/184.html [Pingback]
http://kjipn-ooo.com/female-ejaculation-video-clips.html [Pingback]
http://pohteenews.tripod.com/111.html [Pingback]
http://chg6x-hhh.com/women-being-fucked.html [Pingback]
http://h6vcn-xxx.biz/tight-young-panties.html [Pingback]
http://metnm-www.biz/hardcore-men.html [Pingback]
http://l6u7u-eee.com/vintage-interracial-porn.html [Pingback]
http://freewebs.com/tferma/10/prefabricated-metal-shed-kits.html [Pingback]
http://freewebs.com/lcddlp/03/tampa-tribune.html [Pingback]
http://freewebs.com/niret/12/motorcycle-repair.html [Pingback]
http://freewebs.com/aspxtut/02/pharmacy.html [Pingback]
http://freewebs.com/tferma/05/sitemap10.html [Pingback]
http://iyhzn-rrr.com/erect-nipple-jpg.html [Pingback]
http://www.freewebtown.com/unibetkom/0015-blog.html [Pingback]
http://ramambo.nl.eu.org/11/teamstore-si-cnn.html [Pingback]
http://ramambo.nl.eu.org/marroitt.html [Pingback]
http://ramambo.nl.eu.org/nbc-dealornodeal-com.html [Pingback]
http://wevcjxy.biz/students-fuck-teachers.html [Pingback]
http://voretom.nl.eu.org/teen-girls-in-thongs.html [Pingback]
http://umuqjqf.com/mega-kids-and-teens.html [Pingback]
http://narovkom.nl.eu.org/scobby-doo-porn.html [Pingback]
http://mv8oyh3.biz/teen-torture-bondage.html [Pingback]
http://nasferablog.netfirms.com/110.html [Pingback]
http://cyisevw.com/national-institute-of-mental-health.html [Pingback]
http://obgbtzr.com/bbs-porn.html [Pingback]
http://uro--blog.nl.eu.org/wet-models.html [Pingback]
http://teno--blog.nl.eu.org/christina-aguilara-nude.html [Pingback]
http://nasferablog.netfirms.com/477.html [Pingback]
http://toh--blog.nl.eu.org/wildblacksex-com.html [Pingback]
http://nasferablog.netfirms.com/149.html [Pingback]
http://bvdwkri.biz/real-schoolgirls.html [Pingback]
http://valwgki.biz/www-petsmart.html [Pingback]
http://dhqldvz.biz/boys-piss.html [Pingback]
http://gqkkthz.biz/garden-fence.html [Pingback]
http://nasferablog.netfirms.com/424.html [Pingback]
http://zf1y1fs.biz/waltisneyworld.html [Pingback]
http://wwad6lf.biz/waltdisenyworld.html [Pingback]
http://ph6uked.com/hardcore-mom-sex-moms-xxx.html [Pingback]
http://derfoblog.ifrance.com/sitemap1.html [Pingback]
http://freewebs.com/sruone/karl-rove.html [Pingback]
http://freewebs.com/sruone/sitemap197.html [Pingback]
http://lopbafrea.homestead.com/168.html [Pingback]
http://aqupofot.nl.eu.org/barnes-and-noble-book-stores.html [Pingback]
http://halloweenus.net/581.html [Pingback]
http://pharmacy.dutyweb.org/ [Pingback]
http://acomplia-it.seek-drugs.com/ordinare-acomplia-il-giorno-seguente-consegna.... [Pingback]
Name
E-mail
Home page

Comment (HTML not allowed)  

Enter the code shown (prevents robots):

Copyright © 2009 Kevin D. Wolf. All rights reserved.
DasBlog 'Portal' theme by Johnny Hughes.
Pick a theme: