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

Exploring SharePoint 2007 with PowerShell

The more I work with SP2007, the more I get pumped about the technology.  Although there is a lot you can do right out-of-the-box, the programming model behind this really allows it to be a great framework for any development platform.

To get started programming, you probably want to get an idea of how the Object Model works, a real simple way to do this is with PowerShell.  I have an SP2007 dev server I setup that I installed PowerShell & PowerShell IDE on.  Once you do that, just get a hold of the SP2007 SDK documentation, this will give you an idea of the structure of the Object Model then start playing!  The following screen shot should be enough to get you going:

Obviously this same concept would work to exercise any object model!

Enjoy!

- ec

 

2/9/2007 10:44:00 AM (Eastern Standard Time, UTC-05:00)  #    Comments [0]   ASP.NET | PowerShell | SharePoint 2007  |  Trackback
 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

SharePoint 2007 - It's finally all coming together!

I remember in the very early days of .NET they had a cool demo of how to build a portal in ASP.NET, this was called "I Buy Spy" from what I understood went on to be DOTNETNUKE, I was excited about the concept of configurable content within the portal, it had all the right concepts, adding tabs, little widgets you could configure etc... These were really just Web Controls (ASCX) or Server Controls (compiled DLL's).  My third version of "The Chaos Filter" used this concept extensively. 

Then with ASP.NET 2.0, they introduced the concept of WebParts, this was nice however you had to build up a set of scaffolding and use the provider model to use these within your site (at least as pure web parts).  Not terribly difficult but it limited your deployed options.  At the time SharePoint 2003 had something called "WebParts" as well, extremely similar in both appearance and function, however these "WebParts" were not the same thing as those created with ASP.NET 2.0.  Very disappointing (and confusing), SharePoint 2003 web parts actually came out first, and I assume that Microsoft kept the name since it seems to fit this concept so well and the intent with ASP.NET 2.0 web parts was that they would work with SharePoint 2007.

Version 3.0 of the product really defined and flushed out the data model and workflow engine however, I just wasn't very happy with the presentation layer in ASP.NET 1.1 using the ASCX's and custom controls.  With the introduction of the ASP.NET 2.0 I started on version 4.0 of "The Chaos Filter", this time I focused on an architecture that was built from the ground-up to use web parts and leverage the existing data model and workflow concepts that make the Chaos Filter unique.  This architecture relied heavily on code generation from a product called CodeSmith, templates where created to not only create a simple DAL that mapped to tables in the database, but it also created two web parts (master/detail) for each tables.  This obviously doesn't mean that you can generate 100% of the application, however it does mean that it can very rapidly give you web parts that work out-of-the box that you can customize.  Anytime I hear "You can build your hole site in just three lines of code" my spider senses tell me to watch out!  This solution is really intended to put in place the framework and plumbing that you can open up in your development environment and make it do something useful.

So here we are, SharePoint 2007 was released last November, what does this give us that we really didn't have before?  We now have the ability to easily create little "chunks" of functionality in the form of "WebParts" that can be wired up to create applications.  So with the data model defined in V3.0 of my product, the architecture to include code generation defined in V4.0 of my product, and a mature framework in SharePoint 2007, it's time to start figuring out how to package these concepts into something that will provide value.

-ec

2/4/2007 10:05:56 AM (Eastern Standard Time, UTC-05:00)  #    Comments [0]   ASP.NET | Chaos Filter | SharePoint 2007 | Software Engineering  |  Trackback
Copyright © 2008 Kevin D. Wolf. All rights reserved.
DasBlog 'Portal' theme by Johnny Hughes.
Pick a theme: