Skip to content

Instantly share code, notes, and snippets.

@nicocrm
Created November 10, 2015 15:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nicocrm/770af2394c7f4614edc5 to your computer and use it in GitHub Desktop.
Save nicocrm/770af2394c7f4614edc5 to your computer and use it in GitHub Desktop.
InforCRM Quick Search - For InforCRM 8.2
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="QuickSearch.ascx.cs" Inherits="SmartParts_Contact_QuickSearch" %>
<%@ Register Assembly="Sage.SalesLogix.Web.Controls" Namespace="Sage.SalesLogix.Web.Controls.PickList" TagPrefix="SalesLogix" %>
<%@ Register Assembly="Sage.SalesLogix.Web.Controls" Namespace="Sage.SalesLogix.Web.Controls" TagPrefix="SalesLogix" %>
<%@ Register Assembly="Sage.SalesLogix.Web.Controls" Namespace="Sage.SalesLogix.Web.Controls.Lookup" TagPrefix="SalesLogix" %>
<%@ Register Assembly="Sage.SalesLogix.HighLevelTypes" Namespace="Sage.SalesLogix.HighLevelTypes" TagPrefix="SalesLogix" %>
<style type="text/css">
.date input {
width: 7em;
}
span[slxcompositecontrol] select {
width: 98%;
}
</style>
<asp:UpdatePanel runat="server" ChildrenAsTriggers="true">
<ContentTemplate>
<asp:Label runat="server" ID="lblSearch" Text="Quick Search" Font-Bold="true"></asp:Label><br />
Phone#<br />
<asp:TextBox runat="server" ID="txtPhone"></asp:TextBox>
<br />
Zip Code<br />
<asp:TextBox runat="server" ID="txtZip"></asp:TextBox>
<br />
Customer Last Name<br />
<asp:TextBox runat="server" ID="txtLastName"></asp:TextBox>
<br />
<asp:CheckBox runat="server" ID="chkWithinGroup" Text="Within Current Group" Checked="true" />
<br />
<div style="text-align: left">
<asp:Button runat="server" ID="btnSearch" Text="Search" CssClass="slxbutton" OnClick="btnSearch_Click" />
</div>
</ContentTemplate>
</asp:UpdatePanel>
using System;
using System.Web.UI;
using Sage.SalesLogix.Client.GroupBuilder;
using Sage.Platform.Application;
using Sage.Platform.Security;
using System.Linq;
using System.Xml.Linq;
using System.Xml;
using System.Text;
public partial class SmartParts_Contact_QuickSearch : System.Web.UI.UserControl
{
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
btnSearch.Click += new EventHandler(btnSearch_Click);
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
if (!IsPostBack)
DataBind();
}
protected void btnSearch_Click(object Sender, EventArgs e)
{
string quickSearchGid = GroupInfo.GetGroupIdFromNameFamilyAndType("Quick Search", "Contact", 8);
string currentGroupId = GroupContext.GetGroupContext().CurrentGroupID;
if (ViewState["OriginalGroupId"] == null || currentGroupId != quickSearchGid)
ViewState["OriginalGroupId"] = currentGroupId;
String userid = ApplicationContext.Current.Services.Get<IUserService>().UserId;
// if they happen to have navigated to the Quick Search group explicitly we don't want to use it as base for the search!
bool withinCurrentGroup = chkWithinGroup.Checked && (String)ViewState["OriginalGroupId"] != quickSearchGid;
// parse original group
XDocument groupXml = GetBaseGroup(userid, (String)ViewState["OriginalGroupId"], withinCurrentGroup);
// add quick search conditions
SaveGroupConditions(groupXml);
// save as "Quick Search" group
String gid = SaveGroup(groupXml, "Quick Search", userid, quickSearchGid);
if (quickSearchGid == currentGroupId)
{
// just need to refresh the data since we changed the condition
ScriptManager.RegisterClientScriptBlock(Page, GetType(), "QuickSearch",
"dijit.byId('list').refreshList();",
true);
}
else
{
// need to switch to the quick search group
ScriptManager.RegisterClientScriptBlock(Page, GetType(), "QuickSearch",
"Sage.Services.getService('ClientGroupContext').setCurrentGroup('" + gid + "');",
true);
}
}
private void SaveGroupConditions(XDocument groupXml)
{
XElement conditionsNode = groupXml.Root.Element("conditions");
String fromSQL = groupXml.Root.Element("fromsql").Value;
int conditionNum = 0;
if (conditionsNode.Elements().Count() > 0)
{
conditionsNode.Elements().Last().Element("connector").ReplaceNodes(new XCData("AND"));
}
//Phone conditions
conditionNum += AddCondition(conditionsNode, "A1", "CONTACT", "WORKPHONE", " LIKE ", txtPhone.Text, conditionNum == 0);
//conditionNum += AddCondition(conditionsNode, "A1", "CONTACT", "MOBILE", "=", txtPhone.Text, conditionNum == 0);
//conditionNum += AddCondition(conditionsNode, "A1", "CONTACT", "HOMEPHONE", "=", txtPhone.Text, conditionNum == 0);
//Zip condition
conditionNum += AddCondition(conditionsNode, "A2", "ADDRESS", "POSTALCODE", "=", txtZip.Text, conditionNum == 0);
//Last Name condition
conditionNum += AddCondition(conditionsNode, "A1", "CONTACT", "LASTNAME", " LIKE ", txtLastName.Text, conditionNum == 0);
if (conditionNum > 0)
// closing paren
conditionsNode.Elements().Last().Element("rightparens").ReplaceNodes(new XCData(")"));
XAttribute cntAtr = conditionsNode.Attribute("count");
if (cntAtr == null)
conditionsNode.Add(new XAttribute("count", conditionsNode.Elements().Count().ToString()));
else
cntAtr.Value = conditionsNode.Elements().Count().ToString();
}
private int AddCondition(XElement conditionsNode, string tblAlias, string path, string field, string op, object value, bool addLeftParen)
{
return AddCondition(conditionsNode, tblAlias, path, field, op, value, addLeftParen ? 1 : 0, 0, false, "AND");
}
private int AddCondition(XElement conditionsNode, string tblAlias, string path, string field, string op, object value, int addLeftParen, int addRightParen, bool isliteral, String connector)
{
if (value == null ||
(value is String && value.Equals("")))
return 0;
if (op == " LIKE ")
value += "%";
conditionsNode.Add(new XElement("condition",
new XElement("datapath", new XCData(path +
(path.Contains(":") ? "!" : ":") + field)),
// alias is not really relevant but I think it does need to be unique
new XElement("alias", new XCData(tblAlias + "." + field)),
// note that displayname will usually be overwritten by the client
new XElement("displayname", new XCData(field)),
new XElement("displaypath", new XCData(field)),
new XElement("fieldtype", GetFieldType(value).ToString()),
new XElement("operator", new XCData(op)),
new XElement("value", new XCData(FormatFieldValue(value))),
// this gets overwritten to "END" for last condition in group
new XElement("connector", new XCData(connector)),
new XElement("leftparens", new XCData(new String('(', addLeftParen))),
// we'll add a right one at the end for the last condition in the group
new XElement("rightparens", new XCData(new String(')', addRightParen))),
new XElement("isliteral", isliteral ? "true" : "false"),
new XElement("isnegated", "false"),
new XElement("casesens", "true")));
return 1;
}
private int GetFieldType(object value)
{
if (value is DateTime || value is DateTime?)
return 11;
else
return 1;
}
private String FormatFieldValue(object value)
{
if (value == null)
return "";
if (value is DateTime || value is DateTime?)
{
return ((DateTime)value).ToShortDateString();
}
return value.ToString();
}
/// <summary>
/// Retrieve the XML representation of the original group that was used for the lookup.
/// </summary>
/// <param name="userid"></param>
/// <returns></returns>
private static XDocument GetBaseGroup(String userid, string originalGroupId, bool withinCurrentGroup)
{
String gid = withinCurrentGroup ?
originalGroupId :
GroupContext.GetGroupContext().CurrentGroupInfo.DefaultGroupID;
String xml = null;
if (gid == GroupContext.LookupResultsGroupID)
{
// if they are on "Lookup Results" we may need to revert to the default group
xml = GroupContext.GetGroupContext().CurrentGroupInfo.LookupTempGroup.GroupXML;
if (String.IsNullOrEmpty(xml))
gid = GroupContext.GetGroupContext().CurrentGroupInfo.DefaultGroupID;
}
if (String.IsNullOrEmpty(xml))
xml = GroupInfo.GetGroupInfo(gid).GroupXML;
XDocument groupDoc = XDocument.Parse(xml);
if (!withinCurrentGroup)
{
var element = groupDoc.Descendants("conditions").FirstOrDefault();
if (element != null)
element.RemoveNodes();
}
return groupDoc;
}
/// <summary>
/// Save group to db
/// </summary>
/// <param name="group"></param>
/// <param name="groupName"></param>
/// <param name="userid"></param>
/// <param name="groupId">Group id - blank to save as new group</param>
/// <returns></returns>
private String SaveGroup(XDocument group, String groupName, String userid, String groupId)
{
group.Root.Element("groupid").SetValue(groupId);
group.Root.Element("plugindata").Attribute("id").SetValue(groupId);
group.Root.Element("plugindata").Attribute("name").SetValue(groupName);
group.Root.Element("plugindata").Attribute("displayname").SetValue(groupName);
StringBuilder buf = new StringBuilder();
using (XmlWriter xmlWriter = XmlWriter.Create(buf))
{
group.WriteTo(xmlWriter);
xmlWriter.Flush();
}
GroupInfo ginfo = new GroupInfo();
ginfo.GroupXML = buf.ToString();
ginfo.GroupID = groupId;
ginfo.GroupName = groupName;
ginfo.Save();
Application[ginfo.GroupID] = null;
return ginfo.GroupID;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment