onsdag den 15. juli 2015

Deploying Custom Console Tasks Using VSAE

Not too long ago I showed how to deploy your custom code to Service Manager using a script. What I later found out is that VS can do it all for you with the help of VSAE.
I will base the code off of my previous blog post, and I suggest you read it before continuing. Also get acquainted with VSAE in Authoring Type Projections in VSAE. I will not repeat some of the principles mentioned there.

First we open the solution that contains your code. Add an additional project to this solution and select the "Service Manager 2012 R2 Management Pack" template (or whatever version that suits your environment). You will need to add references to the following management packs
  • Microsoft.EnterpriseManagement.ServiceManager.UI.Console
  • ServiceManager.ConfigurationManagement.Library
  • ServiceManager.WorkItem.Library
They can be found in ex. the library folder of the Authoring Tool installation path.

We also add the dll we are coding as a reference. When added select it and click F4 (properties) and change Package To Bundle to True. This means it will be included in the .mpb file we are building.

Add a new item and select "Empty Management Pack Fragment". We will call it ConsoleTask.mpx. It doesn't matter what you call all of these mpx-files. The names will not appear anywhere in the final product.

Copy paste in the code below:

<ManagementPackFragment SchemaVersion="SM2.0" xmlns:xsd="">
    <!-- provide an ID for the category -->
    <Category ID="CB.ConsoleTask.CustomWISearch.Category" 
      <!-- this is the same name as the VS project or the management pack ID found in project properties -->
      <!-- you can get the public key token using sn.exe, read more at -->
      <!-- provide an ID for the console task -->
      <!-- provide a Target for the console task -->
      <ConsoleTask ID="CustomSearchTask.ConsoleTask" 
          <!--Name of the assembly file without extension -->
          <Argument Name="Assembly">CB.SCSM.CustomWISearch</Argument>
          <!-- classname of your ConsoleCommand task including namespace -->
          <Argument Name="Type">CB.SCSM.CustomWISearch.CustomSearchTask</Argument>
      <!-- Show in Work Item root -->
      <FolderItem ElementID="CustomSearchTask.ConsoleTask" 
      <!-- Show in CI root -->
      <FolderItem ElementID="CustomSearchTask.ConsoleTask"


    ID is normally not used if ever
    Filename including extension (.dll)
    QualifiedName - include PublicKeyToken if you sign the assembly (strongly suggested)
    <Assembly ID="Assembly.ConsoleTask.CustomWISearch" 
              QualifiedName="CB.SCSM.CustomWISearch, Version=, Culture=neutral, PublicKeyToken=098dab1c6092cc7a" />

It works right off the bat with the Custom Task for Work Item Search in Service Manager as mentioned earlier. And should be fairly easy to change as needed using the provided comments.

We will also do abit of "localization". I haven't found the way to do localization in custom console tasks, so this is just one way to do it. If using my code here is a nice optimization for connecting to the management server. I haven't gotten around changing the downloadeable source yet.

//Connect to the server
Microsoft.EnterpriseManagement.UI.Core.Connection.IManagementGroupSession session = (Microsoft.EnterpriseManagement.UI.Core.Connection.IManagementGroupSession)FrameworkServices.GetService<IManagementGroupSession>();
if(session == null)
    //Get the server name to connect to
    String strServerName = Registry.GetValue("HKEY_CURRENT_USER\\Software\\Microsoft\\System Center\\2010\\Service Manager\\Console\\User Settings", "SDKServiceMachine", "localhost").ToString();
    // connect
    _mg = new EnterpriseManagementGroup(strServerName);
    _mg = session.ManagementGroup;

It piggybacks the existing connection in place of connecting a new one.

Create two more empty management pack fragments and paste in the code below. It doesn't matter what you call them, I would suggest something along the lines of LocalizationLANG.mpx where LANG is the language of the displaystrings contained.

<ManagementPackFragment SchemaVersion="SM2.0" xmlns:xsd="">
      <StringResource ID="SearchDialogTitle.StringResource"/>
      <StringResource ID="SearchDialogSearchBtn.StringResource"/>
    <LanguagePack ID="ENU" IsDefault="true">
        <DisplayString ElementID="CB.ConsoleTask.CustomWISearch">
        <DisplayString ElementID="CustomSearchTask.ConsoleTask">
        <DisplayString ElementID="SearchDialogTitle.StringResource">
        <DisplayString ElementID="SearchDialogSearchBtn.StringResource">

The other one you can fill in your language of choice.

In the project properties  we go to build and check "Generate sealed and signed management pack" and select the .snk file to use for signing the management pack. In the Management group tab add and set the proper management group as default.

And finally in the Deployment tab you check auto-increment version, start action must be "Deploy projects to default management group only" and projects to deploy is "Deploy StartUp projects only". The startup project can be selected in solution properties.

You should be good to go. First build using ctrl+shift+b (or right click solution and select build solution). Resolve any errors. When error-free (let me know in the comments below if I missed something) we can deploy the project by hitting F5.

Søg i denne blog