Creating a Fixlet with an action using C#

You can do the same as the previous example with any flavor of C as well as Perl. This is the C# equivalent of the previous script:

using System;
using System.Collections.Generic;
using System.Text;
using BESAPILib;

namespace BESAPITest
{
  class Program
  {
    static void Main(string[] args)
    {
       try
       {                               
        string username = "bigfix";
        string password = "bigfix";
        string server   = "bes-server-hostname";

        int siteID = -2147481618;
        int FixletID = 32;
        int action = 0;

        BESAPILib.FixletMessage Fixlet = new BESAPILib.FixletMessage();
                  Fixlet.SetServer( server );
        Fixlet.Load( siteID, FixletID, username, password );
        if ( Fixlet.DiagnosticMessage.Length != 0 )
            throw new Exception( Fixlet.DiagnosticMessage );

        string settingsXML =
               "<?xml version=\"1.0\"?>\n" +
               "<ActionSettings>\n" +
                   "<Settings>\n" +
                   "<ActionUITitle>title</ActionUITitle>\n" +
                   "<PreActionShowUI>true</PreActionShowUI>\n" +
                   "<PreAction>\n" +
                       "<Text>preaction description</Text>\n" +
                       "<AskToSaveWork>true</AskToSaveWork>\n" +
                       "<ShowActionButton>true</ShowActionButton>\n" +
                       "<ShowCancelButton>true</ShowCancelButton>\n" +
                       "<DeadlineBehavior>ForceToRun</DeadlineBehavior>\n" +
                       "<DeadlineType>Absolute</DeadlineType>\n" +
                       "<DeadlineOffset>PT23H58M54.000000S</DeadlineOffset>\n" +
                       "<ShowConfirmation>true</ShowConfirmation>\n" +
                       "<Confirmation>confirmation message</Confirmation>\n" +
                    "</PreAction>\n" +
                    "<HasRunningMessage>true</HasRunningMessage>\n" +
                    "<RunningMessage>\n" +
                        "<Text>running text</Text>\n" +
                    "</RunningMessage>\n" +
                    "<HasTimeRange>false</HasTimeRange>\n" +
                    "<HasStartTime>true</HasStartTime>\n" +
                    "<StartDateTimeOffset>-PT1M6.000000S
                     </StartDateTimeOffset>\n" +
                    "<HasEndTime>true</HasEndTime>\n" +
                    "<EndDateTimeOffset>P1DT23H58M54.000000S
                     </EndDateTimeOffset>\n" +
                    "<HasDayOfWeekConstraint>false</HasDayOfWeekConstraint>\n" +
                    "<ActiveUserRequirement>RequireUser
                     </ActiveUserRequirement>\n" +
                    "<ActiveUserType>LocalUsers</ActiveUserType>\n" +
                    "<HasWhose>false</HasWhose>\n" +
                    "<Reapply>false</Reapply>\n" +
                    "<HasReapplyLimit>false</HasReapplyLimit>\n" +
                    "<HasReapplyInterval>false</HasReapplyInterval>\n" +
                    "<HasRetry>false</HasRetry>\n" +
                    "<HasTemporalDistribution>false</HasTemporalDistribution>\n" +
                    "<PostActionBehavior Behavior=\"Nothing\">
                                   </PostActionBehavior>\n" +
                    "<IsOffer>true</IsOffer>\n" +
                    "<OfferCategory>offer category</OfferCategory>\n" +
                    "<OfferDescriptionHTML>offer description
                    </OfferDescriptionHTML>\n" +
                    "</Settings>" +
               "</ActionSettings>";

    BESAPILib.IXMLDOMDocument actionXMLDocument = Fixlet.get_ActionXML
              ( action,System.Text.Encoding.Unicode.GetBytes( settingsXML ) );
    if ( Fixlet.DiagnosticMessage.Length != 0 )
       throw new Exception( Fixlet.DiagnosticMessage );

    string targetXMLA =
        "<?xml version=\"1.0\"?>" +
        "<BESActionTarget>" +
           "<ComputerName>a</ComputerName>" +
           "<ComputerName>b</ComputerName>" +
           "<ComputerName>c</ComputerName>" +
        "</BESActionTarget>";

    string targetXMLB =
        "<?xml version=\"1.0\"?>" +
        "<BESActionTarget>" +
             "<ComputerID>34</ComputerID>" +
             "<ComputerID>12704810</ComputerID>" +
        "</BESActionTarget>";

    string targetXMLC =
        "<?xml version=\"1.0\"?>" +
        "<BESActionTarget>" +
            "<CustomRelevance>exists file \"c:\\virus\"</CustomRelevance>" +
        "</BESActionTarget>";

    BESAPILib.XMLImporter xmlImporter = new BESAPILib.XMLImporter();
            xmlImporter.SetServer( server );
    int actionID = xmlImporter.ImportAction( System.Text.Encoding.Unicode.GetBytes
                                                       ( actionXMLDocument.xml ), 
                                             System.Text.Encoding.Unicode.GetBytes
                                                       ( targetXMLA ), 
                                             (UInt32) Fixlet.siteID, 
                                             (UInt32) Fixlet.FixletID, 
                                             username, 
                                             password );
    if ( xmlImporter.DiagnosticMessage.Length != 0 )
        throw new Exception( xmlImporter.DiagnosticMessage );

     System.Console.WriteLine( "ActionID: " + actionID );
   }
   catch ( System.Exception e )
   {
        System.Console.WriteLine( e.Message );
   }
  }
 }
}