Programming, technology, and CRM – from a Belgian programmer exiled to Missouri
  • rss
  • Home
  • Soft Gallery
    • autosvnbackup.sh
    • VBScript Snippets
  • Contact Me
  • Welcome

Creating and Displaying a Group Programmatically

Nicolas Galler | June 12, 2008

1. The Players

GroupContext.GetGroupContext() is used to retrieve the class holding the info about what group the user currently has selected for each entity.  It is stored in the session.  GroupContext.GetGroupContext() is currently hard-coded to reference the HTTP context so do not call it outside of the web client.

GroupContext has an "EntityGroupInfo" object for each entity.  This EntityGroupInfo is a sort of cache for the currently selected group as well as a frontend for group selection.  It does not have a direct relationship to the GroupInfo class which actually represents a specific group.

GroupTranslator is the COM object that is responsible for translating XML to Delphi blob and vice versa.

2. Creating a Group

Saving a new group is relatively easy.  There is a GroupInfo.Save method which saves a group and returns the group id.  It does not work outside of the web client though so I just used the GroupTranslator directly.

try
{
    String groupXml = GroupInfo.GetBlankGroupXML("Contact");
    // this retrieves the condition as a subquery
    // (QueryBuilder is another piece I have that builds the query - anything that will
    // return a query string will work, though)
    String condition = ((QueryBuilder)group.CreateKeyFieldQuery(false)).GetSqlQuery(true);
    GroupInfo ginfo = new GroupInfo();
    ginfo.GroupXML = groupXml;
    ginfo.GroupName = groupName;
    ginfo.AddLookupCondition("CONTACT:CONTACTID", " IN ", "(" + condition + ")");

    // remove the existing group
    _groupManager.DeleteGroup("Contact", groupName, "ADMIN");

    // we could just use ginfo.Save here but it doesnt work without an active web session
    XmlDocument doc = new XmlDocument();
    doc.LoadXml(ginfo.GroupXML);
    XmlElement pluginDataNode = (XmlElement)doc.SelectSingleNode("SLXGroup/plugindata");
    pluginDataNode.Attributes["name"].Value = groupName;
    pluginDataNode.Attributes["displayname"].Value = groupName;
    pluginDataNode.Attributes["id"].Value = "";
    doc.SelectSingleNode("SLXGroup/groupid").InnerText = "";
    String groupId = translator.SaveGroup(doc.OuterXml, GroupInfo.ConnectionString);
}
finally
{
    System.Runtime.InteropServices.Marshal.ReleaseComObject(translator);
}

3. Displaying the Group

If you try to display the group by redirecting the browser to Contact.aspx?gid=…., there is a good chance they will get a NullReferenceException at that point.  This is because the GroupContext has a cache of the active groups for the entity and this cache is not automatically rebuilt.

The following code will take care of clearing this.  Note that I have to catch the NullReferenceException because of the way GroupContext works outside of the web client and I have this class unit-tested.  The call to ClearCache and the setting of the CurrentTable are the other tricky parts necessary.

try
{
    if (GroupContext.GetGroupContext() != null)
    {
        LOG.Debug("Setting group context");
        GroupContext.GetGroupContext().CurrentTable = "CONTACT";
        var groupCache = GroupContext.GetGroupContext().GetGroupInfoForTable("CONTACT");
        groupCache.ClearCache();
        groupCache.CurrentID = groupId;
    }
    else
    {
        LOG.Debug("Group Context is not available.");
    }
}
catch (NullReferenceException)
{
    // ignore those because GroupContext is f. up
}

After that you can redirect to "Contact.aspx?gid=" + groupId and have the new group displayed.  Or maybe just redirect to Contact.aspx since we set the current group id (didn’t test that one).

4. Displaying a Temporary Group

There is a SetCurrentGroupAsLookupResult method in the EntityGroupInfo object, however I could not get it to work.

I am out of time for today but would love to know if anyone figures this one out.

Categories
Saleslogix
Comments rss
Comments rss
Trackback
Trackback

« Web Services in Saleslogix Strongly Typed Databindings in SlxWeb »

Leave a Reply

Click here to cancel reply.

Categories

  • Experiments (4)
  • Interesting (1)
  • MSCRM (1)
  • Programming (60)
  • Rant (3)
  • Saleslogix (34)
  • Tricks (8)
  • Uncategorized (30)

Post History

  • 2010
    • January (3)
    • March (3)
    • April (2)
    • August (2)
  • 2009
    • March (2)
    • April (1)
    • May (3)
    • June (3)
    • July (1)
    • September (3)
    • October (2)
    • December (5)
  • 2008
    • January (9)
    • February (4)
    • March (9)
    • April (1)
    • May (5)
    • June (8)
    • July (1)
    • August (2)
    • September (1)
    • November (1)
    • December (3)
  • 2007
    • January (3)
    • February (7)
    • March (1)
    • April (3)
    • May (6)
    • June (2)
    • July (1)
    • August (2)
    • September (5)
    • October (3)
    • November (5)
    • December (4)
  • 2006
    • January (2)
    • September (1)
    • November (3)
    • December (4)
  • 2005
    • April (1)

Meta

  • Log in
  • Entries RSS
  • Comments RSS
  • WordPress.org
rss Comments rss valid xhtml 1.1 design by jide powered by Wordpress get firefox