View Single Post
  #7 (permalink)  
Old 07-26-2007, 11:21 AM
ncushing ncushing is offline
Administrator
 
Join Date: Mar 2007
Location: Prezza Technologies
Posts: 172
Default

Shane,

Here is some commented code that illustrates the full process of Authenticating a user, loading a survey, and creating/sending an invitation. Thank you for your patience while I put this together...If you have further questions, please do not hesitate to ask. Due to post size limits, I'll break it up into two posts.

Code Part 1
Code:
    using System;
    using System.Data;
    using System.Security.Principal;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;

    using Checkbox.Users;
    using Checkbox.Forms;
    using Checkbox.Panels;
    using Checkbox.Security;
    using Checkbox.Invitations;
    using Checkbox.Messaging.Email;

    /// <summary>
    /// Authenticate a user, load a survey, create an invitation, add panelists
    /// to the invitation, then send the invitation.
    /// </summary>
    private void CreateAndSendInvitation()
    {
        //Initialize the user manager, only needs to be done once per
        // IIS Application lifetime.
        UserManager.Initialize();

        //Authenticate with a user that has sufficient access to send
        // invidations.
        UserManager.AuthenticateUser("admin", "admin");

        //Select a survey to use for the invitation
        // The query will return a data set with information for all
        //  response templates that the logged-in principal has the 
        //  specified permission on.  In this case, the principal
        //  is in the "System Administrator" role so the permission
        //  passed is not used.
        DataSet rtData = ResponseTemplateManager.GetAvailableResponseTemplates("Form.Administer");

        ResponseTemplate surveyToCreateInvitationFor = null;

        if (rtData.Tables.Count > 0)
        {
            DataRow[] rtDataRows = rtData.Tables[0].Select();

            if (rtDataRows.Length > 0)
            {
                surveyToCreateInvitationFor = ResponseTemplateManager.GetResponseTemplate((int)rtDataRows[0]["ResponseTemplateID"]);
            }
        }

        //The survey was successfully loaded, now try to create an invitation
        if (surveyToCreateInvitationFor != null)
        {
            //Create the object
            Invitation invitation = new Invitation();

            //Associate the invitation with the survey
            invitation.SetParentID(surveyToCreateInvitationFor.ID.Value);

            //Customize the invitation template, which is where the body, from address, format
            // etc. are configured
            invitation.Template.Format = MailFormat.Text;
            invitation.Template.FromAddress = "surveyadmin@mycompany.com";
            invitation.Template.FromName = "Survey Administrator";
            invitation.Template.Subject = "An invitation to take a survey.";

            //Set the invitation body to include the URL for the survey.  Replace
            // spaces in the guid to help avoid any url encoding issues.
            // Also, in order to track the invitation responses, we need to add an
            // @@InvitationID parameter.
            invitation.Template.Body = "Please take the this survey: http://www.mycompany.com/survey.aspx?InvitatinID=@@InvitationID&s=" + surveyToCreateInvitationFor.GUID.ToString().Replace("-", "");

            //Save the invitation before we start to add panelists
            invitation.Save();


            //Now, let's add panelists.  An invitation supports types of panelists:
            // Users, Email Addresses, Email Lists, User Groups

            /********************* User Panelist ********************/
            List<IIdentity> userPanelistsToAdd = new List<IIdentity>();

            IIdentity panelistIdentity = UserManager.GetUserIdentity("inviteeUserName");

            if (panelistIdentity != null)
            {
                userPanelistsToAdd.Add(panelistIdentity);
                invitation.AddPanel(userPanelistsToAdd);
            }

            /********************* Email Address Panelist *************/
            List<string> emailAddressesToAdd = new List<string>();

            emailAddressesToAdd.Add("emailaddress1@mycompany.com");
            emailAddressesToAdd.Add("emailaddress2@mycompany.com");

            invitation.AddPanel(emailAddressesToAdd);

            /********************* Email List Panelist ****************/
            //EmailList panels are managed through the Users->Email Lists
            // section of Checkbox.
            DataSet emailListData = EmailListPanel.GetAvailableEmailLists();

            //Add a random email list
            if (emailListData.Tables.Count > 0)
            {
                DataRow[] emailListDataRows = emailListData.Tables[0].Select();

                if (emailListDataRows.Length > 0)
                {
                    int emailPanelID = (int)emailListDataRows[0]["PanelID"];

                    EmailListPanel panel = PanelManager.GetPanel(emailPanelID) as EmailListPanel;

                    if (panel != null)
                    {
                        invitation.AddPanel(panel);
                    }
                }
            }

            /********************* User Group Panelist ****************/
            //Add a user group panel to the invitation
            //Again, since we are an admin user, the actual permissions don't matter, but
            // if a non-admin user were creating the invitation, the appropriate permission
            // would be "View Group Membership" which is "Group.View"
            Group[] userGroups = Group.GetGroups(PermissionJoin.Any, "Group.View");

            //Pick a random group to add...
            if (userGroups.Length > 0)
            {
                invitation.AddPanel(userGroups[0]);
            }

            //Now that all the panelists have been added, save the invitation.  If it is not
            // saved, errors will result when sending
            invitation.Save();
Reply With Quote