Converting Data Type of Promoted Fields with Custom Event Handlers for MOSS


Hi Again,

In my previous post titled "WORKING WITH INFOPATH FORMS AND INFOPATH SERVICES", I tried to build a guide for publishing InfoPath forms as content types and promoting few fields so that they can appear in list views. I hope the previous post was useful and helpful. Please provide your comments on that, if you think any change will make it more beneficial for all of us.

Anyways, moving on with this post, toady’s agenda is…

  • Playing with InfoPath’s promoted fields.
  • Converting DataType of promoted fields.
  • Working with an event handler.

Scenario:

The scenario is when we promote fields (text boxes) from InfoPath, they are promoted as single line of text. My problem was I wanted to sum the totals of all the form reimbursements from my side, as this is possible in list views, I just need to leverage my promoted fields to act like currency field instead of single line of text, so that I can apply out of box formula on it.

Requirements:

  1. Change the data type of promoted fields from "SINGLE LINE OF TEXT" to "CURRENCY".
  2. Apply out of box "SUM" formula to it.

Solution:

After facing this problem, I surf for hours and hours and didn’t find an appropriate solution for this. I consulted a Sr. SharePoint Consultant,
Mr. Yasir Jerry (My Mentor — I LOVE YOU, MAN J), currently working in KSA, who proposed making and attaching an event handler to my SharePoint list. So here’s what I did.

  1. Go to your list in which you have your content type.
  2. Make a new view of that list and make a new field in it of currency type.
  3. Make a simple Class Library using VS 2005 and give ref of SharePoint.
  4. Make a class in it which is inherited from SPItemEventReceiver (as we are woking with List Items so need to inherit this class from there).
  5. Overrides two methods, these will fire when item is added and updated.

    public class ItemHandler : SPItemEventReceiver

    {

    public override void ItemAdded(SPItemEventProperties properties)

    {

    base.ContextEvent(properties);

    SPListItem aItem = properties.ListItem;

    aItem["Name of New Currency Field"] = aItem["Name of promoted Field"];

    aItem.Update();

    }

     

    public override void ItemUpdated(SPItemEventProperties properties)

    {

    base.ContextEvent(properties);

    SPListItem aItem = properties.ListItem;

    aItem["Name of New Currency Field "] = aItem["Name of promoted Field "];

    aItem.Update();

    }

    }

     

  6. Build the project for any errors.
  7. Sign this assembly a strong name.
  8. Build it again, and place it in GAC (Global Assembly Cache, by opening c:Windowsassembly and drag drop that DLL file from BIN folder of your class library project)
  9. Attach the custom Event Handler with list.
  10. Go to your view, add your new currency field to view and in TOTAL SECTION selects the field then select SUM.
  11. Make this field as Default View.

You are done! Enjoy J

Conclusion:

There is no way that you change the data type of the promoted field. The only way is write custom event handler that fires on Item Change and Update and assign the value to other data type field. I hope this post will also be useful, Please provide your comments so that I make it more beneficial for people.

Advertisements

1 Comment

  1. jo

    Or…
    SPList list = web.Lists[listname];
    for (int i = 0; i < list.Fields.Count; i++)
    {
    if (list.Fields[i].InternalName == list.ContentTypes[contenttypename].Fields[fieldname].InternalName)
    {

    list.Fields[i].ReadOnlyField = false;
    list.Fields[i].AllowDeletion = true;
    list.Fields[i].Update();
    list.Fields[i].Type = SPFieldType.Currency;
    list.Fields[i].Update();
    SPFieldCurrency cur = (SPFieldCurrency)list.Fields[i];
    cur.DisplayFormat = SPNumberFormatTypes.TwoDecimals;
    cur.CurrencyLocaleId = 1033;
    list.Update();
    }
    }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: