Peter Walke

in

November 2009 - Posts

Extending the K2 Black Pearl TaskList Webpart – Show or Hide Visibility based upon SharePoint group membership

The Black Pearl TaskList web part is a web part that can get users up and running quickly to view what outstanding tasks on his or her plate.  If you need to extend it, it might not be readily apparent how to do it.      I needed to extend the Webpart to either show or hide itself based upon whether a user belonged to a specific group. 

Step 1:  Finding the TaskList dll

I did some digging in SharePoint designer and found the .webpart file for the web part.  It lives at catalogs/wp/K2TaskList.WebPart.   Its webpart assembly is

SourceCode.Workflow.TaskList, Version=4.0.0.0, Culture=neutral, PublicKeyToken=16a2c5aaaa1b130d

It looks like a gac assembly.  I figure I can just add it as a reference and then inherit from it.  I go to add the reference, and the assembly doesn’t show up as an available assembly:

image

(Tangent: It does surprise me that all of K2’s dll’s are in the namespace SourceCode.  I’m surprised it’s SourceCode.Workflow.Wizards instead of K2.Workflow.Wizards).

I figured I would get around this by <cough>hack</cough> copying the dll to a known location and referring to the dll directly instead of from the GAC. 

I found the dll  in c:\windows\assembly in windows explorer.  I then fired up cmd.exe and navigated to c:\windows\assmebly and typed:

dir *tasklist*.* /s

I found where the dll was living (C:\Windows\assembly\GAC_MSIL\SourceCode.Workflow.TaskList\4.0.0.0__16a2c5aaaa1b130d on my machine) and copied it to my lib folder.  I then added a reference to this dll and was on my way to step 2:

Step 2: Inherit from TaskListWebPartFactory and override the CreateChildControls Method

Create a class that inherits from TaskListWebPartFactory. 

Next, override the CreateChildControls method.  Inside this method, grab the current user and loop though his or her groups.  If the user belongs to the correct group, show the control, otherwise leave its visibility set to false.

protected override void CreateChildControls()
       {
           SPWeb currentWeb = SPContext.Current.Web;
           this.Visible = false;
           foreach (SPGroup group in currentWeb.CurrentUser.Groups)
           {
               if (group.Name == EDITOR_GROUP_NAME) //Specify the group name you want to check
               {
                   this.Visible = true ;
                   break;
               }
           }
           currentWeb.Dispose();
           base.CreateChildControls();
       }

Step 3: Create the supporting elements, features and .webpart file

The rest of this task is pretty straight forward.  Create a feature, elements and .webpart file to provision your new class.   I placed these in the proper 12 hive structure in a class library project and then used WSPBuilder to package it all up. I’ve listed the files below for reference.

Feature.xml

<?xml version="1.0" encoding="utf-8" ?>
<Feature xmlns="http://schemas.microsoft.com/sharepoint/"
         Id="YOURGUIDHERE"
         Title="Custom K2 WebParts"
         Scope="Site"
         Hidden="False"
         Version="1.0.0.0">

  <ElementManifests>
    <ElementManifest Location="elements.xml" />
    <ElementFile Location="TaskList.webpart" />
  </ElementManifests>

</Feature>

Elements.xml

 

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

tasklist.webpart

<?xml version="1.0" encoding="utf-8" ?>
<webParts>
  <webPart xmlns="http://schemas.microsoft.com/WebPart/v3">
    <metaData>
      <type name="CustomWebParts.SP.Assets.WebParts.TaskListWebPart, CustomWebParts.SP, Version=1.0.0.0, Culture=neutral, PublicKeyToken=e485f8a858b358c3"/>
      <importErrorMessage>Error importing the Web Part.</importErrorMessage>
    </metaData>
    <data>
      <properties>
        <property name="Title" type="string">TaskList Web Part</property>
        <property name="ChromeType" type="chrometype">None</property>
      </properties>
    </data>
  </webPart>
</webParts>

Wrapping it all up

This was a pretty straight forward customization by inheriting from a class and overriding its methods.  The challenge came in finding which dll to inherit from (SP Designer is your friend) and grabbing a dll out of the gac that didn’t want to come (command line to the recue).

Technorati Tags: ,,