Wednesday, May 30, 2012

Open the Edit Form of External List from BCS Profile Page

Recently I had a business scenario, where the user wanted to use a Business data in not traditional way:

  • To find a particular entity instance with not using of search – the external system contains 20+ millions of entries, the crawl time (full and incremental) will be significant and have to be processed daily
  • To edit an entity instance with no browsing of External List – there is no way to find the particular item in the EL

Finally I designed the following solution:

  • Developed a BCS .net assembly connector in Visual Studio, which connects to Oracle database. It contains a few entities, but for simplicity we will focus on only one – Customer. It has three operations – Finder, Specific Finder and Updater and identifier UNID (string)
  • After deployment on my SPS 2010 I created a profile page in the standard way, through BDC service application’s management page in CA
  • Meanwhile I created a small configurable web part which accepts two parameters (text boxes) Customer_ID and Date (because of the business users), executes a SQL query against the Oracle and returns the UNID of the only record. The web part redirects the user to the profile page, created in the previous step. Actually this small component plays as an “item picker” which opens the item’s profile page!
  • Additionally I created an external list with view and edit forms

So, the biggest question was HOW once landed on the profile page the user can jump directly to the Edit form ??? Where is the problem? The edit form expects a parameter BcsIdentity instead of UNID and there is not a normal way to get it.

  • I added a BCS action (not external list custom action) from BDC service application’s management page in CA. Named it “EDIT” and the URL was the URL of my EditForm.aspx http://myportal/List/MyExternalList/EditForm.aspx?UNID={0}, where UNID is the identifier of my entity
  • The action is visible/accessible from the profile page. Clicking on it the user jumps to EditForm.aspx, but with wrong parameter ?UNID=123456, instead of ?ID=<BcsIdentity> and the form is empty
  • I developed a second web part, which reads the UNID parameter from the query string and encodes it in “a BCS way”

SmileSmileSmile

string unid = qstr["UNID"];
object[] identifiers = { unid };
string identifiersEnc = EntityInstanceIdEncoder.EncodeEntityInstanceId(identifiers);
string newUrl = string.Format("{0}&{1}={2}&source={3}", Page.Request.Url.ToString(),newQstrParamName, identifiersEnc, SPContext.Current.Site.Url);
Page.Response.Redirect(newUrl, true);

Finally the code redirects the browser to the same page (EditForm.aspx) but with newly added parameter ID=”myEncodedIdentifier” and the Edit form work as expected!

Special thanks to Pradeep Kamalakumar, for his significant help!