search blog
most popular
MCP MCTS MCT MVP

SharePoint BDC Part 2: Creating your first BDC Web Part

Author: Tobias Zimmergren
URL: http://www.zimmergren.net | http://www.tozit.com | @zimmergren

Introduction

In my previous article (SharePoint BDC Part 1: Getting started with the Business Data Catalog) I talked about what you'll need to do in order to begin to work with the BCD in SharePoint (MOSS 2007 specifically, as it's an Enterprise feature).

In this article I will briefly guide you through the process of creating your first BDC Web Part that will communicate with the BDC.

Note: You can download the source code for this sample project here [Download]

Prerequisites

You need to have your BDC set up and ready to use before you can start creating your own BDC Web Parts to communicate with the BDC. (See my previous article for a walkthrough)

We will also use Microsoft Visual Studio (2005/2008) to create the Web Part.

Enjoy!

What we had -> where we're going

In the previous article I talked about using the built-in Web Parts for your BDC needs. Although they fill a lot of requirements, sometimes you just might want to write your custom Web parts instead.

This is what the standard BDC list looks like:

image

This is what the final result will look like: (Note, this is a custom Web Part which you'll develop in the following steps)
image

Step 1 - Create a new Web Part project

I will expect that you already know how to create a new Web Part project (either using the VSeWSS or using WSPBuilder, or any other tool of preference).

See this article for a WSPBuilder complete walkthrough: WSPBuilder - Walkthrough of the Visual Studio Add-in

Step 2 - Add the BDC references to your project

Once you've got a Web Part project up and running, you should add some references that are needed for the BDC Object Model.

You should add the "Microsoft® Office SharePoint® Server component" reference to your project:
image

Once you've added this reference to your project, you have a GO for using the BDC namespaces.

Add the following using-statements to the top of your Web Part:

using Microsoft.Office.Server.ApplicationRegistry.MetadataModel;
using Microsoft.Office.Server.ApplicationRegistry.SystemSpecific.Db;
using Microsoft.Office.Server.ApplicationRegistry.Runtime; (if you need it..)

This is what my Visual Studio project looks like when using the WSPBuilder to create my Web Parts (notice the reference to microsoft.sharepoint.portal which comes from the reference we added):
image

Now we're good to go! Let's start coding then!

Step 3 - Utilize the BDC object model to access data

Note, you can download the complete Visual Studio Project at the end of this article with the source code.

Fetches all the LOB instances from the metadata repo:

var myInstances = ApplicationRegistry.GetLobSystemInstances();

 

Fetch the HR instance for Human Resources:

var humanRecourcesInstance = myInstances["HR"];

 

Pick out the Department entity from the HR instance:

var departmentEntity = humanRecourcesInstance.GetEntities()["Department"];

 

Fetches the method GetDepartments which is specified in our ADF file (Application Definition File) for our BDC connection:

var getDepartmentMethod = departmentEntity.GetMethods()["GetDepartments"];

 

Fetches the MethodInstance DepartmentFinderInstance:

var deptFinderInstance =
      getDepartmentMethod.GetMethodInstances()["DepartmentFinderInstance"];

 

We call the departmentEntity.Execute() method to fetch DbEntityInstance's:

var foundEntities = 
     (DbEntityInstanceEnumerator)departmentEntity.Execute(
                                                                          deptFinderInstance, 
                                                                          humanRecourcesInstance);

 

Simply loop the DbInstances and then shoot out their name in a bunch of literal controls.
In a real scenario, we would most likely add those intances to an SPGridView instead of printing Literals to enable Views, Sorting, Paging and a whole set of awesome features that comes with the Grid View.:


while (foundEntities.MoveNext())
{
    var currentDepartment = (DbEntityInstance)foundEntities.Current;
    Controls.Add(new Literal{Text =
              "<div style='border-bottom:1px dashed #c0c0c0; width:100%;'>"});

    Controls.Add(new Literal {Text =
              currentDepartment.GetFormatted("Name")+ "<br/>"}); 

    Controls.Add(new Literal { Text=
currentDepartment.GetFormatted("DepartmentID").ToString() }); 

    Controls.Add(new Literal{ Text = "</div>" });
}

When you've written the aforementioned code, you will have the base set up for retreiving data from your BDC connection. You will (of course) have to dig a whole lot deeper if you want to master the arts of BDC and the object model that comes with that. But at least it's a start for you to get to know the namespace, and learn what namespaces to use - and see that it actually works!

Step 4 - Finalize and deploy your Web Part

Once you've created the project, build it and deploy to your SharePoint development machine and add your Web part to see the action:

image
(Yes, the Web Part is called SharePointRules BDC Web Part - and for a good reason too!)

Step 5 - May your creation shine upon SharePoint! (aka. Final result)

This is what the creation looks like (wow..), which basically only utilize the most basic aspects of the BDC Object Model to get started - but here you have it in action, fetching data immediately from your back-end SQL server (Adventure Works database in this case) using the BDC:

image

Summary

That's a wrap for this article. I have walked you through the few simple steps needed to create your own first BDC Web Part.

In my next BDC article I will talk about what alternative tools I find suitable to use for generating your ADF (Application Definition File).

 


Published: Jul-25-09 | 5 Comments | 0 Links to this post

SharePoint BDC Part 1: Getting Started with the Business Data Catalog

Author: Tobias Zimmergren
URL: http://www.zimmergren.net | http://www.tozit.com 

Introduction

In this article I will guide you through the very basics of getting started with Business Data Catalog, BDC:

  1. Install the AdventureWorks 2008 Sample Databases
    1. We will use this database as our example for retrieving data using the BDC.
  2. I will step you through the simple process of creating your ADF (Application Definition File)
    1. We will use this file as our import connection
  3. I will guide you through how we can import this ADF file and create our BDC Application
  4. Lastly, I will guide you through the process of creating a basic site and use some of the basic BDC Web Parts

Install the AdventureWorks sample database

  • You'll need to go and download the AdventureWorks sample databases
    image

  • Just finish the installation by clicking next a couple of times and let the installer do it's normal Microsoft-installer magic.
  • You should now see a couple of new databases in your SQL Server Management Studio:
    image
    AdventureWorks, AdventureWorks2008, AdventureWorksDW, AdventureWorksDW2008, AdventureWorksLT, AdventureWorksLT2008

Alright - We've got our databases, now we need to start thinking about how we will get data from our SQL server into SharePoint. This is done by creating/generating an Application Definition File (ADF) as you will see in the next section.

Creating the ADF (Application Definition File)

Alright, there's a few different options to create your ADF (Application Definition File). I will show you how to get started with using the free tool called "Application Definition Editor" that comes with the latest SharePoint Server SDK.

Note: See the bottom section in this article for a summary of links to all resources mentioned in the article.

After you have installed the latest SDK, you can choose to install the "Microsoft BDCTool" located here by default: "C:\Program Files\2007 Office System Developer Resources\Tools\BDC Definition Editor". Which will give you the following item in your Start Menu:
image

Launch the BDC Application Definition Designer

Click the application and launch the editor. You will see an interface like this:
image

Create or import your ADF file

There's basically two alternatives when it comes to editing an ADF (Application Definition File).
One is to create a new one (which I will guide you through first), and the other is to import an existing one (which I will show you after the first alternative).

Alt 1) Creating our own ADF file
Now we're going to connect to our newly created sample-databases and create an ADF file for use with those databases.
  • Click on ADD LOB System
  • Choose Connect To Database
  • You will see a nice popup-dialog where you will be able to enter the connection details to your desired database
    • Enter your connection details, example:
       image
  • You are presented with the "Designer Surface" that looks something like this:
    image
  • In our case, we're going to use the table called "vEmployee" which exist in the AdventureWorks database in order to pull out some information about our employees.
    • Search for the table called vEmployee and drag it out to the Design Surface
    • Search for the table called vEmployeeDepartments and drag it out to the Design Surface
    • It should look something like this:
      image
    • Make any necessary changes, then click OK
    • You'll see a view similar to this one after some tweaking:
      image 
    • After you've done the necessary changes to your configuration, making sure it's a valid ADF with proper filters, enumerators and methods or whatever you need in your application then smile, because we're done with that part!
Alt 2) Importing an existing ADF file

If you don't want to do everything from scratch or you've already got an ADF file that you wish to modify, you can do so by importing an existing ADF file into the Definition Editor. Here's how:

  • Open the BDC Definition Editor tool, then click the "Import" button in the menu:
    image
  • Browse to your existing ADF file and choose to import it. Simple as that.
    (I am importing a file called BDCAWDW.xml, which contains definitions for Product, Reseller, ProductSubcategory, ProductCategory as shown below)
  • You'll see the imported ADF file's structure immediately in the designer, under the prerequisite that your SQL connection string in the ADF file is valid:
    image 

Note: I will not detail how you create filters, finders, methods etc. in this article. You can read more about that here:
http://msdn.microsoft.com/en-us/library/ms145931(SQL.90).aspx

I may cover the topic of ADF-functionality in another article later on.

Generate the ADF file from the designer

I really don't need to tell you this, but there's a button called "Export" which you use to export the definition you've created using the definition editor to an xml file:
image

Import the ADF file

If we have gotten this far, we might as well get the last few bits in place.
What we now need to do is to import our ADF file into SharePoint, since that's where it should reside. Follow along with these few simple steps to make sure you're properly importing your file into SharePoint.

  • Navigate to your Shared Services Provider Administration site (You can access your SSP through Central Administration)
  • You are presented with a section called "Business Data Catalog" where you'll find a bunch of different alternatives.
  • Make sure you have the permissions to modify the BDC (See the link Business Data Catalog permissions)
  • Click "Import application definition"
    image  
  • Browse for your .xml file and click "OK":
    image
  • You'll see a progress bar (You don't see that a lot in SharePoint. I love it!), telling your how the import process is going:
    image
  • When it's done, you'll click "OK" and be presented with an overview of your imported BDC Application:
    image
Configure permissions on the BDC Application Definition

In order for all users to be able to select/read data from your BDC Application, you'll need to make sure they've got the appropriate permissions to actually do so.

Usually I do this setting on each of the imported entities, in case you want specific permissions on different entities - instead of on the entire application.

  • Select the DropDown list on your first entity and choose "Manage Permissions":
    image 
  • Choose "Add Users/Group":
    image
  • Enter "NT AUTHORITY\AUTHENTICATED USERS" and choose "Select in Clients":
    image
  • Repeat these steps for the other entity as well.
  • You're done.

Now we have created or imported an ADF file with the Business Data Catalog Definition Editor tool, exported it to an .xml file, imported it into SharePoint, set basic permissions on the entities.

Next, we should make sure that the application works in SharePoint by adding a Business Data Catalog-WebPart to a page.

Use the basic built-in BDC Web Parts

Awesome. Now that we have gotten this far by importing an ADF file into SharePoint and set appropriate permissions on the entities - We're ready to actually use the ADF connection to view stuff in our database.

Note: I have created a new blank site where I can easily show the built-in BDC Web Parts - so that's where I am adding my Web Parts.

 

  • Add two Web Parts to your page called "Business Data List" and "Business Data Item":
    (Note that when you've configured a BDC application, you'll see the Business Data web parts)
    image
  • Choose to edit the properties of the Business Data List Web Part:
    image
  • Click the Browse-icon to the right to pop up the BDC entity chooser:
    image
  • It will present you with the following interface (note, BDC applications will of course vary depending on what you have imported..):
    image 
  • Double click the "Employee" type, and then click "OK" in your Web Part property window.
  • Repeat this process for the "Business Data Item" Web Part, and select "Employee" in the BDC Type Picker as well.

Now we've got one BDC List Web Part which will list all employees, and one BDC Item Web Part that will display details about the employee we select.

In order for this to work we must connect the two Web Parts.

  • Edit menu of your Web Part -> Connections -> Send Selected item To -> Employee
    image

Test our BDC Application out, and make sure it works!

  • Choose "LastName" then "contains" and enter "smith":
    image
  • Select one of the results by clicking the radiobutton to the left, and see that the result (details) about the Employee shows up in the connected Web Part:
    image 

Resources and links

Summary

This article is a basic step-by-step guide to getting started with BDC in MOSS 2007. I've shown you every step from creating the databases required (in our case some sample databases) to creating the ADF file and to finally utilize the BDC connection from a site, using the BDC Web Parts.

In an upcoming post I will talk about how you can create your own BDC Web Parts! Keep your eyes open!


Published: Jun-25-09 | 31 Comments | 0 Links to this post

WSPBuilder - Walkthrough of the Visual Studio Add-in

Author: Tobias Zimmergren
http://www.zimmergren.net

Introduction

Alright. People have approached me lately and asked me if I could give them a brief introduction to the WSPBuilder extensions available for Visual Studio. Instead of taking all those discussions on one by one, I've decided to document some of the main features here. If I'm missing out on something, please let me know and I'll fill it up.

Bil Simser did a survey with the SharePoint MVP's and summarized the foremost favorite CodePlex projects in this article. 

In this article I will cover one of my favorite tools - WSPBuilder.

WSPBuilder background

A SharePoint Solution Package (WSP) creation tool for WSS 3.0 & MOSS 2007
No more manually creating the manifest.xml file
No more manually specifying the DDF file
No more using the makecab.exe application

Carsten Keutmann, an MVP and friend in Copenhagen is the brilliant mind behind this awesome application.

The idea behind the WSPBuilder add-in for Visual Studio is that it's based on any normal "Class library" template - which means that you can easily copy your entire WSPBuilder project to a machine that doesn't have WSPBuilder and still be able to open the project. - This is something you can't do with a lot of other extension tools (say, the VSeWSS for example)

WSPBuilder Installation

Just download the latest release of the "WSPBuilder Extensions - Visual Studio Addin" and let the installation guide take you through the most simple process ever - clickety click.

Creating a WSPBuilder project

When you have installed the add-in to Visual Studio, you should now be able to create a new project based on the "WSPBuilder" template.

To kick this off, let's create our WSPBuilder project:
image

Note: You don't have to create a WSPBuilder template, you can create a normal Class Library as well. The only thing about a WSPBuilder template is that it will automatically create the "12" folder along with a temporary strong-key so you don't have to do that right now.

When you've created the project, you'll see a structure like this one in your solution explorer:
 image

The WSPBuilder will create the 12-folder, since it's good practice to start your projects from the 12-root. It will also add the file "solutionid.txt" which contains a GUID to be used on the .wsp package, for easy reference. You will also get a strong-key generated for you so you don't have to worry about signing your project right now.

Alright, now that we're up and running with a blank WSPBuilder project - we should start by adding something to the solution.

WSPBuilder Templates - Overview

In an ordinary fashion, right click on the project and choose Add - New item.
image

Choose the "WSPBuilder" node and you will see an overview over the available templates like this:
image

Let's walk through each and every one of them! The joy! :-)

Blank Feature Template Overview

A blank feature does exactly what the name implies, it creates a blank feature for you!

I'm creating a blank feature, and naming it to "BlankFeature1" so we easily can distinguish it from the other folders created later on.

With WSPBuilder, when you create a new item based on a template, you'll get a dialog asking you for some variables - and since this is a feature, it's going to need a Title, Description and of course a Scope:
image

Your solution tree will be populated with a few new things, in this case the BlankFeature1 that we chose to create:
image

As you will see, you get not only the perfectly correct 12-hive structure - you will also get the feature.xml and elements.xml files created for you, and the feature.xml file can look like this:

<?xml version="1.0" encoding="utf-8"?>
<Feature  Id="8e039720-d7df-460a-8d65-c52e47417fdf"
          Title="BlankFeature1"
          Description="Awesome description for BlankFeature1"
          Version="12.0.0.0"
          Hidden="FALSE"
          Scope="Web"
          DefaultResourceFile="core"
          xmlns="http://schemas.microsoft.com/sharepoint/">
  <ElementManifests>
    <ElementManifest Location="elements.xml"/>   
  </ElementManifests>
</Feature>

Event Handler Template Overview

With the Event Handler item template, you will not only get the correct 12-structure in your solution - you will also get the reference to "Microsoft.SharePoint.dll" added automatically, since an event handler requires some talking to the SharePoint Object Model.

We will get our feature.xml and elements.xml as normal - but this time the elements.xml is pre-populated with some tags to hook up our event handler:

<?xml version="1.0" encoding="utf-8" ?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Receivers ListTemplateId="100">
    <Receiver>
      <Name>AddingEventHandler</Name>
      <Type>ItemAdding</Type>
      <SequenceNumber>10000</SequenceNumber>
      <Assembly>Zimmergren.SharePoint.Demo.WSPBuilder, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b7201b5590fd1fc0</Assembly>
      <Class>Zimmergren.SharePoint.Demo.WSPBuilder.EventHandler1</Class>
      <Data></Data>
      <Filter></Filter>
    </Receiver>
  </Receivers>
</Elements>

As you can see, the elements.xml file is referring to the assembly called Zimmergren.SharePoint.Demo.WSPBuilder and a class called EventHandler1.

With the magic of WSPBuilder, this class has of course also been created for us and will look something similar to this:

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;

namespace Zimmergren.SharePoint.Demo.WSPBuilder
{
    class EventHandler1 : SPItemEventReceiver
    {
        public override void ItemAdded(SPItemEventProperties properties)
        {
            base.ItemAdded(properties);
        } 

        public override void ItemAdding(SPItemEventProperties properties)
        {
            base.ItemAdding(properties);
        } 

        public override void ItemUpdated(SPItemEventProperties properties)
        {
            base.ItemUpdated(properties);
        } 

        public override void ItemUpdating(SPItemEventProperties properties)
        {
            base.ItemUpdating(properties);
        } 
    }
}



Solution Installer Configuration

If you've ever used the SharePoint Installer from CodePlex, you know that when you want to use it with your own .wsp file you need to do some adjustments to the configuration xml.

With the Solution Installer Configuration template you will get this configuration automatically created and hooked up with your project. The Setup.exe.config file might look like this:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="BannerImage" value="Default"/>
    <add key="LogoImage" value="None"/>
    <add key="EULA" value="EULA.rtf"/>
    <add key="SolutionId" value="6e23b11d-8460-49a0-b2f1-b8aa78d7c58d"/>
    <add key="FarmFeatureId" value="bb1586eb-3427-483b-baa5-ae5498c47d69"/>
    <add key="SolutionFile" value="Zimmergren.SharePoint.Demo.WSPBuilder.wsp"/>
    <add key="SolutionTitle" value="Zimmergren.SharePoint.Demo.WSPBuilder"/>
    <add key="SolutionVersion" value="1.0.0.0"/>
    <add key="UpgradeDescription" value="Upgrades {SolutionTitle} on all frontend web servers in the SharePoint farm."/>
    <add key="RequireDeploymentToCentralAdminWebApplication" value="true"/>
    <add key="RequireDeploymentToAllContentWebApplications" value="false"/>   
  </appSettings>
</configuration>



Web Part Feature

This is by far one of the most popular templates, as it crates a generic template for your web part and also creates the feature for provisioning the Web Part to the Web Part Gallery.

You will get the elements.xml file configured something like this:

<?xml version="1.0" encoding="utf-8" ?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Module Name="WebPartPopulation" Url="_catalogs/wp" RootWebOnly="TRUE">
    <File Url="WebPartFeature1.webpart" Type="GhostableInLibrary">
      <Property Name="Group" Value="MyGroup"></Property>
      <Property Name="QuickAddGroups" Value="MyGroup" />
    </File>
  </Module>
</Elements>

and you'll get the required .webpart file configured something like this:

<?xml version="1.0" encoding="utf-8" ?>
<webParts>
  <webPart xmlns="http://schemas.microsoft.com/WebPart/v3">
    <metaData>
      <type
        name="Zimmergren.SharePoint.Demo.WSPBuilder.WebPartFeature1,
        Zimmergren.SharePoint.Demo.WSPBuilder,
        Version=1.0.0.0,
        Culture=neutral,
        PublicKeyToken=b7201b5590fd1fc0" />
      <importErrorMessage>
            Cannot import WebPartFeature1 Web Part.
      </importErrorMessage>
    </metaData>
    <data>
      <properties>
        <property name="Title" type="string">WebPartFeature1</property>
        <property name="Description" type="string">
            Description for WebPartFeature1
        </property>
      </properties>
    </data>
  </webPart>
</webParts>

and you will get the WebPartFeature1.cs file created automatically (or whatever you choose to name it) and it usually look like this:

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;

namespace Zimmergren.SharePoint.Demo.WSPBuilder
{
    [Guid("a043d73d-7418-4918-baed-828a2bc77019")]
    public class WebPartFeature1 : Microsoft.SharePoint.WebPartPages.WebPart
    {
        private bool _error = false;
        private string _myProperty = null; 

        [Personalizable(PersonalizationScope.Shared)]
        [WebBrowsable(true)]
        [System.ComponentModel.Category("My Property Group")]
        [WebDisplayName("MyProperty")]
        [WebDescription("Meaningless Property")]
        public string MyProperty
        {
            get
            {
                if (_myProperty == null)
                {
                    _myProperty = "Hello SharePoint";
                }
                return _myProperty;
            }
            set { _myProperty = value; }
        } 

        public WebPartFeature1()
        {
            this.ExportMode = WebPartExportMode.All;
        } 

        /// <summary>
        /// Create all your controls here for rendering.
        /// Try to avoid using the RenderWebPart() method.
        /// </summary>
        protected override void CreateChildControls()
        {
            if (!_error)
            {
                try
                { 

                    base.CreateChildControls(); 

                    // Your code here...
                    this.Controls.Add(new LiteralControl(this.MyProperty));
                }
                catch (Exception ex)
                {
                    HandleException(ex);
                }
            }
        } 

        /// <summary>
        /// Ensures that the CreateChildControls() is called before events.
        /// Use CreateChildControls() to create your controls.
        /// </summary>
        /// <param name="e"></param>
        protected override void OnLoad(EventArgs e)
        {
            if (!_error)
            {
                try
                {
                    base.OnLoad(e);
                    this.EnsureChildControls(); 

                    // Your code here...
                }
                catch (Exception ex)
                {
                    HandleException(ex);
                }
            }
        } 

        /// <summary>
        /// Clear all child controls and add an error message for display.
        /// </summary>
        /// <param name="ex"></param>
        private void HandleException(Exception ex)
        {
            this._error = true;
            this.Controls.Clear();
            this.Controls.Add(new LiteralControl(ex.Message));
        }
    }
}



Web Service Template

The following files will be automatically created for you:

  • 12\LAYOUTS\WebService1.asmx
  • WebServiceCode\WebService1.cs

WebService1.asmx may look like this:

<%@ WebService Language="C#"
Class="Zimmergren.SharePoint.Demo.WSPBuilder.WebService1,
Zimmergren.SharePoint.Demo.WSPBuilder,
Version=1.0.0.0,
Culture=neutral,
PublicKeyToken=b7201b5590fd1fc0"  %>

WebService1.cs may look like this:

using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;

namespace Zimmergren.SharePoint.Demo.WSPBuilder
{
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class WebService1 : System.Web.Services.WebService
    { 

        public WebService1()
        { 
        } 

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        } 
    }
}



Custom Field Type Template

The Custom Field Type template will create all the necessary files to get up and going with a Custom Field Control.

The following files will be generated and populated:

  • 12\TEMPLATE\CONTROLTEMPLATES\CustomFieldType1FieldEditor.ascx
  • 12\TEMPLATE\XML\fldtypes_CustomFieldType1.xml
  • FieldTypeCode\CustomFieldType1.cs
  • FieldTypeCode\CustomFieldType1Control.cs
  • FieldTypeCode\CustomFieldType1FieldEditor.cs

The contents in these files are too much to bring up in a single blogpost, so if you're thrilled about seeing what they look like - create your own project and dig in :-)

Feature With Receiver

Does what it says it's supposed to do. Creates a FeatureReceiver and all required files.

  • 12\TEMPLATE\FEATURES\FeatureWithReceiver1\elements.xml
  • 12\TEMPLATE\FEATURES\FeatureWithReceiver1\feature.xml
  • FeatureCode\FeatureWithReceiver1.cs

Feature.xml might look like this:

<?xml version="1.0" encoding="utf-8"?>
<Feature  Id="3e724aaf-c1ed-4a93-ae1c-c6d3f59b2214"
          Title="FeatureWithReceiver1"
          Description="Description for FeatureWithReceiver1"
          Version="12.0.0.0"
          Hidden="FALSE"
          Scope="Web"
          DefaultResourceFile="core"
         ReceiverAssembly="Zimmergren.SharePoint.Demo.WSPBuilder,
         Version=1.0.0.0,
        Culture=neutral,
        PublicKeyToken=b7201b5590fd1fc0"
      ReceiverClass="Zimmergren.SharePoint.Demo.WSPBuilder.FeatureWithReceiver1"
        xmlns="http://schemas.microsoft.com/sharepoint/">
  <ElementManifests>
    <ElementManifest Location="elements.xml"/>
  </ElementManifests>
</Feature>

FeatureWithReceiver1.cs might look like this:

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;

namespace Zimmergren.SharePoint.Demo.WSPBuilder
{
    class FeatureWithReceiver1 : SPFeatureReceiver
    {
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            throw new Exception("The method or operation is not implemented.");
        } 

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            throw new Exception("The method or operation is not implemented.");
        } 

        public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        {
            throw new Exception("The method or operation is not implemented.");
        } 

        public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        {
            throw new Exception("The method or operation is not implemented.");
        }
    }
}

Sequential Workflow Feature and State Machine Workflow Feature Templates

Creates the necessary files to get started with your Sequential Workflow code.

  • 12\TEMPLATE\SequentialWorkflowFeature1\elements.xml
  • 12\TEMPLATE\SequentialWorkflowFeature1\feature.xml
  • WorkflowCode\SequentialWorkflowFeature1.cs
  • WorkflowCode\SequentialWorkflowFeature1.designer.cs

The same routine applies to the State Machine Workflow Feature template.

 

Web Part Without Feature

Finally, you can create a Web Part without the feature - basically just creating the .webpart file and the .cs file.

  • 80\wpcatalog\WebPart1.webpart
  • WebPartCode\WebPart1.cs

Solution tree overview

Since I've been bashing all kinds of templates in here, you'll see that there's a huge tree of files - all automatically created in less than 1 minute.

image

Template Overview Summary

Alright, the templates I've been mentioning before are great to get rolling with a new SharePoint project. But what about deployment of this solution? How do we create our .wsp file, and how do we choose where the files should land (Global Assembly Cache - GAC - or the /bin folder?)

That's what the next section is all about - bringing some clarification to how the WSPBuilder creates your packages.

WSP Creation and Deployment with WSPBuilder

So, when we're satisfied with our awesome project and want to build a .wsp package from it - we can simply choose to right click the project -> WSPBuilder -> Build WSP and it will automatically create the .wsp for us:
image

This will create a .wsp file in your project folder like so:
image

Now, if you want to check the contents of the .wsp package, you simply rename the .wsp file to .cab and open it, like so:
image

Manifest.xml

In the cabinet (.wsp package) you will find the file called Manifest.xml - this is the file that tells SharePoint where to actually deploy the solution - GAC or BIN.

If you don't do any changes at all, this file will look something like this:
image

As you can see, the DeploymentTarget is set to "GlobalAssemblyCache"  and your dll will go into the GAC.

Now, in this particular case we can not deploy to the /bin folder anyway - as we have types in our assembly that MUST go into the GAC (Workflows and EventReceivers are two of those types).

But if we were to have a Web Part project or what not - and we want to deploy it only to the /bin folder, follow along with the next few steps.

Scoping the assembly for BIN instead of GAC (including Code Access Security generation)

Okay. So you don't want it in the GAC, but in your BIN folder instead. That's okay, just follow along with these few simple steps:

  • Remove your /bin/debug folder entirely from your solution (make sure the .dll gets wiped)
  • If the 80-folder in your project root isn't created - create it
    • Create a folder called "bin" folder in the 80 folder
    • Your solution tree should look something similar to this:
      image
    • Right click your project and choose "Properties"
    • Choose the "Build" tab
    • Change the Output path from "bin\Debug" to "80\bin\":
      image

When you build your project now, your .dll should pop into the "80\bin\" folder in your solution tree like this:
image

Ready to Rock - Scoping the assembly for the /bin folder

If we go about building our .WSP package again (right click project - WSPBuilder - Build WSP) and rename the .wsp to .cab and check the manifest.xml file - we should see two things done different:

  • DeploymentTarget is set to WebApplication (any chosen WebApp, e.g. /bin)
    image
  • Some general CAS (Code Access Security) permissions has been automatically added to make your assembly run:
    image

Deployment with WSPBuilder

Okay. So we've created our project, scoped it either for GAC (do nothing) or for /bin (make the changes in the previous section) - and we want to deploy it. What do we do?

  • Right Click the Project -> WSPBuilder -> Deploy
    Your output window will show something like this:
    image

Check your Solution Management in Central Administration under the tab "Operations" and make sure it's successfully deployed:
image

Conclusion and Summary

This post simply walks through some of the more popular features of the WSPBuilder created by my pal Carsten Keutmann in Copenhagen.

If there's any questions or comments - please add them in the comments section below.

Thanks


Published: Apr-08-09 | 169 Comments | 0 Links to this post

How To: SharePoint and Silverlight 2.0 – Part 1

Author: [MVP] Tobias Zimmergren
Web: http://www.zimmergren.net

Prephase

I have previously written up a few articles on how you can get more from your SharePoint environment by enhancing it with AJAX, .NET 3.5 and Silverlight.

References to those articles can be found here:

My intention is to get a SharePoint / Silverlight article series going, and this is to be the first article in the series – How to get up and running!

Prerequisits

Must have:

Note: I’m not going to describe how you create a .xap file – you’ll find plenty of resources for that on the net. Just go google! (Live.com, yeah!)

Nice to have:

Part 1 – Step by step to configure your SharePoint environment for Silverlight 2.0

First of all, if you don’t want to do the manual .NET 3.5 settings in your web.config – there’s a great feature to take care of this on CodePlex which can be found under the Features project.

Step 1: Download, install and deploy the .NET 3.5 Web Config feature
  • Download the .NET 3.5 web.config feature from here
  • Install the .wsp into your SharePoint environment
  • Deploy the .wsp into your SharePoint environment to the appropriate Web Application
    image
  • Activate the feature for your Web Application (the one you deployed to)
    (This is done from Central Administration – Application Management – Manage Web Application Features)
    image 

Now we’re all set with the pre-configurations of the web.config – though there’s one more thing we need to manually do.

Step 2: Adding the final touches to web.config manually

Since the features project doesn’t include Silverlight by default (except for the Beta 2 version, which we’re not interested in..) you should now open up your web.config manually and

  • add the following line to <system.web> <compilation> <assemblies>:

<add assembly="System.Web.Silverlight,
    Version=2.0.5.0,
    Culture=neutral,
    PublicKeyToken=31bf3856ad364e35" />

It should look something like this:
image

Step 3: Add System.Web.Silverlight.dll to the GAC (Global Assembly Cache)

Add the System.Web.Silverlight.dll to the Global Assembly Cache (either drag’n’drop it into C:\Windows\assembly or use Gacutil.exe or use the default .NET Configuration Tool)

You’ll find the System.Web.Silverlight.dll assembly in the Silverlight 2.0 SDK folder, located here:

C:\Program Files\Microsoft SDKs\Silverlight\v2.0\Libraries\Server\

Step 4: Set the correct MIME-type for the Silverlight .xap filetype

Go to your IIS management console (Start – Run - “inetmgr” without the quotes)

  • Select your Web Application from the list, and select properties:
    image
  • Choose “HTTP Headers” and then “MIME Types…”
    image
  • Add the MIME-type for Silverlight 2.0 Applications as shown:
    image
  • Okay, Okay, Okay (Press the buttons..)
  • Close the IIS manager as we will not need it anymore for the time being!

Now when all those fancy-pancy things are done – let’s get rolling with creating a simple Hello World Web Part using Silverlight 2.0, shall we?

Part 2 – Creating a first Web Part to host a Silverlight application

If you’ve read this far you should now be set up properly to create a Silverlight Web Part (Really, a Web part that loads the silverlight application and renders in the browser)

Step 1: Visual Studio 2008 time!

First of all, make sure you’ve got the .xap file in handy, then launch Visual Studio 2008 SP1.

  • Create a new Web Part project in your desired fashion – I’m using the WSS Extensions for ease:
    image
  • Add a reference to the System.Web.Silverlight assembly and to the System.Web.Extensions assembly, it should look something like this:
    image
     
  • Add the following using statements:
    Note: I’ve stripped down the default using statements, as they’re overkill for this – this is what you should need

    using System.Runtime.InteropServices;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.SilverlightControls;


  • Make an override on the OnInit method (We will dynamically add a ScriptManager to the current page, if there’s not one already):

    protected override void OnInit(System.EventArgs e)
    {
        base.OnInit(e);
        var sm = ScriptManager.GetCurrent(Page);
        if(sm == null)
        {
            var scriptManager = new ScriptManager();
            scriptManager.EnablePartialRendering = true;
            Page.Form.Controls.AddAt(0,scriptManager);
        }
    }

  • Write the following very simple code to load a Silverlight class into the Web Part, set it’s source to our .xap file and simply add it to the controls collection:  

    private Silverlight mySLControl;
    protected override void CreateChildControls()
    {
        base.CreateChildControls();
        mySLControl = new Silverlight
        {
            ID = "HelloSilverlightControl",
            Width = new Unit(367), 
            Height = new Unit(150),
            Source = @"/_layouts/ZimmerLight/Hello/Hello.xap"
        };
        Controls.Add(mySLControl);
    }

  • Build and deploy the Web Part, and cross your fingers!
    (Using the WSS Extensions for Visual Studio, just rightclick the project and choose Deploy)
    image 
Step 2: Add the Web Part to a page
  • I’ve put my Web Part in a “Silverlight Web Parts” group – simply choose it and click add.
    image 
  • Voila, a fully functional Silverlight 2.0 Web Part rendered inside SharePoint – without any trouble!
    image


Summary and Download

As you’ve seen in this article, it isn’t too hard to get up and running with Silverlight 2.0 (and .NET 3.5 of course) and get our first Silverlight 2.0 Web Part spinning in SharePoint.

You should now be able to:

  • Configure your environment to use Silverlight 2.0
  • Hook up your Silverlight Application(s) in SharePoint
  • Enjoy the richness of Silverlight in SharePoint!

You can download the Visual Studio project from here

Comments and Feedback appreciated

Please leave your print in the comments, feedback is always nice :-)


Published: Dec-10-08 | 23 Comments | 0 Links to this post

CKS EBE – Comments Manager

Introduction

<EDIT>
    A new version of this tool can be downloaded [here]
    Read more about the new version [here]
</EDIT>

I have been getting a few requests from people who've been getting quite a lot of spam in their Enhanced Blog Edition SharePoint blog, and who don’t currently have the ability to change any code or make adjustments. (They’ve got permissions to the system, but not to actually do anything like upgrade the assembly, add a nifty re-captcha validator or any other changes. They’re only allowed to do the “admin-stuff through the admin-interface”).

My quick and dirty solution was to provide them with a “Comments Manager” which basically is a Windows Application that will list all comments in the blog with a checkbox beside it. The “admin” can then check the desired (or, undesired..) blog comments and then kill them. This solution proved to be much faster than to do the same using the web browser using e.g. the tedious “Edit in datasheet view” option.

Note; This isn’t by far any cool application – it merely does what the few people want it to do, kill undesired comments with a better overview :)

Features

This light-weight application will do the following:

  • List all comments
    • Comment Author
    • Trimmed Body
    • Background-coloring
      • Green for comments with status = “Approved”
      • Red for all other (Rejected and Pending)
  • Kill selected comments

Simple as that!

Comments Manager preview

image

Download

Download the CKS:EBE Comments Manager v1.1.0.0

If you have ideas or comments, or if it simply doesn’t work – leave a comment!
Better yet, leave a comment anyway! :)

Future additions

If required by the people using this simple app – the future versions could include

  • Comment status change [Approval status]
  • Use Web Services to connect to your SP site instead
  • Track- and linkback manager
  • etc. etc.


Published: Oct-16-08 | 4 Comments | 0 Links to this post

Web Part Caching – A simple approach

All kudos to Vince Rothwell who provided an awesome blogpost on SharePoint Caching and the CacheDependency object

This week I am tutoring a SharePoint 2007 development class over at Informator in Gothenburg, Sweden. Today we’ve been looking at Web Parts, creation of custom webparts and best practices for creating our custom solutions based on web parts.

I quickly coded up a sample which is caching items in a webpart - Huge server load is a major impact point for some organizations, making caching a strategically important choice if there’s much redundancy.

Anyway, you probably already know that it’s a good thing to cache your data once in a while if you’ve got heavy load and the data isn’t any “to-the-minute” critical information. So let’s get on with it..

Why?

Since people have been asking for a ‘simple sample’ of how to cache things in SharePoint – I thought that I would provide just that, a simple sample. For more in-depth information about caching and cachedependendies, check out Vince’s blog.

Check it out!

Web Part fetching items from a SPWeb object, looping all SPList objects and displaying the ItemCount property.

If there isn’t a cache object present, the iteration of the lists will be done immediately
image

If there is a cache object present, it will fetch the information from the cached object instead of iterating the lists, saving us some resources 
image

Code It!

This is the full code of the simple sample cache Web Part:

using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using Microsoft.SharePoint;
using System.Web;
namespace Zimmergren.WebParts.SampleCachePart
{
    public class SimpleCache : System.Web.UI.WebControls.WebParts.WebPart
    {
        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            base.Render(writer); 

            List<SPList> lists = new List<SPList>();
            string status = ""; 

            if (HttpRuntime.Cache["SimpleSampleCache"] == null)
            {
                status = "The following items are <strong>NOT</strong> fetched from the cache<br/><br/>"; 

                SPWeb web = SPContext.Current.Web;
                foreach (SPList list in web.Lists)
                    lists.Add(list); 

                HttpRuntime.Cache.Add("SimpleSampleCache",
                lists,
                null,
                DateTime.MaxValue,
                TimeSpan.FromMinutes(10),
                System.Web.Caching.CacheItemPriority.Default, null);
            }
            else
            {
                status = "The following items <strong>ARE</strong> fetched from the cache!<br/><br/>";
                lists = (List<SPList>)HttpRuntime.Cache["SimpleSampleCache"];
            } 

            writer.Write(status);
            foreach (SPList l in lists)
                writer.WriteLine(l.Title + " - " + l.ItemCount + " items<br/>");
        } 
    }
}

Summary & Download!

I always use the HttpRuntime or HttpContext objects to store and read my cached objects. You can of course use the built-in caching functionality of WSS 3.0 Web Parts if you feel that you have the need for it. However this approach works everytime, everywhere. Not just for Web Parts of course, but for any kind of ASP.NET hooked application.

You can download the sample project [here]

Thanks for tuning in,
Cheers


Published: Oct-07-08 | 11 Comments | 0 Links to this post

SharePoint .NET 3.5 auto-configuration – escape the manual overhead!

I have previously talked about how you manually can configure your SharePoint environment to enable .NET 3.5. My approach were to always do this manually, but it seems that there’s a sweet feature for this purpose created, automating this process.

As a tip from Jeremy Thake, I’m posting the link to a CodePlex project called “Features” which obviously have a feature that deals with configuring your web application without any manual steps like modifying xml snippets in web.config.

Even though the manual steps I’ve provided in my blogpost only takes about 1-2 minutes to perform, this approach eliminates the human facor – which often is the cause to a lot of headaches!

Thanks for the tip Jeremy, and hope everyone else will find this interesting aswell!

More on .NET 3.5 to come later.


Published: Sep-30-08 | 6 Comments | 0 Links to this post

How to: LINQ with SharePoint - .NET 3.5 Framework with SharePoint Part 2

Author: Tobias Zimmergren
Blog: http://www.zimmergren.net

Introduction

In my previous article title "How to: Get up and running with .NET 3.5 in your SharePoint environment" I talked about how you can manually set up your SharePoint environment for use with Microsoft .NET Framework 3.5.

In this article I will talk about how you can incorporade some of the technologies used in .NET 3.5 to query a SharePoint list. More precisely, I will talk about how you easily can use LINQ from the .NET 3.5 framework to get started with .NET 3.5 in SharePoint.

A more in-depth article might be posted later, but this one is simply providing simple code instructions to what a Web Part that utilizes LINQ can look like!

Prerequisites

In order to follow along with this walkthrough, you should have the following bulletpoints checked:

Creating a custom Web Part which utilizes .NET 3.5 in SharePoint

The code below will give you the heads up on how to fetch the SPListItem objects from an SPList object and sort them alphabetically. You can of course use the 'where'-clause with LINQ aswell to filter out which objects (SPListItem objects) to fetch from the SPList.

Example task list to fetch the data from:
image

Using the following code, I've used LINQ to retreive all the list items and sort them ascending by title!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;

namespace Zimmergren.net35.LINQWebPartSample
{
    public class SPLINQ : System.Web.UI.WebControls.WebParts.WebPart
    {
        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            base.Render(writer);
            SPList taskList = SPContext.Current.Web.Lists["Tasks"]; 

            // Get items, order by title alphabetically and assign to taskListItems
            var taskListItems = from SPListItem tItem in taskList.Items
                                orderby tItem.Title
                                ascending select tItem;
 

            foreach (SPListItem taskItem in taskListItems)
                writer.WriteLine(taskItem.Title + "<br/>\n");
        }
    }
}

The bold text in the above code block is the LINQ statement to fetch all items in the SPList and order them ascending by Title. You can of course make much more complex queries using LINQ in order to fetch other objects based on different criteria.

This will produce a simple output like this, sorting the items alphabetically:
image

Summary and Download

This was a very (very) basic and simple example of how you can use any .NET 3.5 technology to get started with some new cool stuff.

I utilized a basic LINQ expression in this article which of course can be heavily modified if you want to retreive other things from your list(s). Perhaps a future article will take on some more advanced LINQ expressions in conjunction with SharePoint?! :-)

Anyway, you can download the sample project from here: Zimmergren.net35.LINQWebPart.zip

Thanks for tuning in and please leave a comment

As always, there's plenty of readers but few people showing their appreciation through the comments - please leave a comment :)

Hope it helps :-)


Published: Sep-28-08 | 15 Comments | 0 Links to this post

U2U CAML Query Builder - New version out

I just read Karine Bosch's blog where she posted some news on the U2U CAML Query Builder, so I just ought to get your attention in that direction.

Check the detailed updates out in Karine's blog:
http://www.u2u.info/Blogs/karine/Lists/Posts/Post.aspx?List=d35935e0%2D8c0e%2D4176%2Da7e8%2D2ee90b3c8e5a&ID=30

Cheers


Published: Jul-26-08 | 2 Comments | 0 Links to this post

Infrastructure updates for MOSS 2007 and WSS 3.0

As some of you might already know, Microsoft released their infrastructure updates to MOSS and WSS yesterday (2008-07-15).

Instead of yabbing along here about the actual updates, I'm simply going to provide you with links to the downloads and documents, so you can read it yourself.

Sidenote: Just installed the update for WSS 3.0 on my blog, running WSS 3.0 SP1 - and so far so good.

32-bit downloads

Download 32-bit: Infrastructure Update for Windows SharePoint Services 3.0 (KB951695)
Download 32-bit: Infrastructure Update for Microsoft Office Servers (KB951297)

64-bit downloads

Download 64-bit: Infrastructure Update for Windows SharePoint Services 3.0 (KB951695)
Download 64-bit: Infrastructure Update for Microsoft Office Servers (KB951297)

Information about the updates

Documentation: Infrastructure Update for Windows SharePoint Services 3.0
Documentation: Infrastructure Update for Microsoft Office Servers

Get going :)


Published: Jul-16-08 | 3 Comments | 0 Links to this post
 Next >>