Adding a Button to Generate an SData feed based on Current Context using the .NET API

1 minute read time.

For an introduction to this code please see the article "Adding a Button to Generate an SData feed based on Current Context using the ASP/COM API"

The code listed below is the C# equivalent.

[code language="csharp"]
using Sage.CRM.WebObject;
using Sage.CRM.Utils;

namespace SDataFeed
{
class OpportunityListSData : Web
{
public override void BuildContents()
{
//Code assumes that this is for the Opportunity Entity
//Assign action code that page should return to. 184 is the OpportunityList

string strOriginalSystemAction = "184";

//Get the details of the server, installname, Session ID and whether HTTPS is used.
string strHostName = Dispatch.Host;
string strInstallName = Dispatch.InstallName.ToLower();
string strHTTP;
if (Dispatch.ServerVariable("HTTPS") == "off")
{
strHTTP = "http";
}
else
{
strHTTP = "https";
}
string strSID = Dispatch.QueryField("SID");

//Check which context the Opportunity List is being called from.
//Establish foreign key name and value.
string strForeignKeyName;
int intForeignKeyValue;
int iDomKey = Keys[(int)Sage.KeyList.DominantKey];
switch (iDomKey)
{
case 1:
strForeignKeyName = "oppo_primarycompanyid";
intForeignKeyValue = Keys[(int)Sage.KeyList.CompanyId];
//intForeignKeyValue = (int)Sage.KeyList.CompanyId;
break;
case 2:
strForeignKeyName = "oppo_primarypersonid";
intForeignKeyValue = Keys[(int)Sage.KeyList.PersonId];
break;
case 4:
strForeignKeyName = "oppo_assigneduserid";
intForeignKeyValue = Keys[(int)Sage.KeyList.UserId];
break;
case 5:
strForeignKeyName = "oppo_channelid";
intForeignKeyValue = Keys[(int)Sage.KeyList.ChannelId];
break;
default:
strForeignKeyName = "oppo_assigneduserid";
intForeignKeyValue = Keys[(int)Sage.KeyList.UserId];
break;
}

//Build SData URL
string strSdataUrl = strHTTP + "://" + strHostName + "/sdata/" + strInstallName + "j/sagecrm/-/opportunity?where=" + strForeignKeyName +" eq " + intForeignKeyValue + " &SID=" + strSID;

//Create prompt for user to copy SData URL and return to Original System action
AddContent("");
AddContent("");
}
}
}
[/code]