May 4, 2013

Custom values on Choice column - Part 2

In Part 1 here of this article, we understood how the default choices work in OTB Create Column page (FldNew.aspx) in SharePoint 2010.
Now, its time to start the fun. Let's see examples of how we can show custom choice values:

First, remember to always make a backup copy of any OTB file before customizing it.

Example 1: Change static text values in resource file
Open the wss.en-US.resx (located in C:\inetpub\wwwroot\wss\VirtualDirectories\80\App_GlobalResources directory) using notepad or Visual Studio. Search for the keys, and replace the default values with your custom values such as (X, Y, and Z).

And this is how it will look like when creating new column:

There are only 3 keys to be used here. The downside of changing values for these 3 keys is that this will impact other pages, for example these keys are used in qstedit.aspx page used in survey lists. 
You can create new keys with custom values inside wss.en-US.resx and reference them through a a GetGlobalResourceObject()  in FldNew.aspx page.

Example 2: Custom static choices on page:
You can define your static strings directly without the need to resources file.
Edit FldNew.aspx, and replace the whole original inline code (highlighted yellow portion in Part 1) with this:

            "January" + "\r\n" + "Febraury" + "\r\n" + "March"+ "\r\n" +
            "April" + "\r\n" + "May" + "\r\n" + "June" + "\r\n" +
            "July" + "\r\n" + "August" + "\r\n" + "September" + "\r\n" +
            "October" + "\r\n" + "November" + "\r\n" + "December"

And this is how it will look like:

Example 3: Custom dynamic strings:
We can provide list of numbers from 0 to 9. Put the following code instead of the inline code (highlighted yellow portion in Part 1) in page. Set the rows parameter inside the textarea tag to expand the choices box to show more values as required.

        string ChoicesTxt = "0";
        for (int i=1; i<10; i++)
            ChoicesTxt += "\r\n" + i.ToString();
       // write the choices string into idChoices control on page

 And it will like this:

Example 4: Custom data from SharePoint list:
Create a custom list named "MyChoices" and add your choice values in Title field. Adjust the permissions to make it readable by all farm users or elevate the privileges through code. Replace the idChoices inline code (highlighted yellow portion) with the following:

        string ChoicesTxt = "";
        bool  _CustomChoicesError = false;
            SPWeb web = SPContext.Current.Web;
            SPList MyChoicesList = web.Lists.TryGetList("MyChoices");
            if (MyChoicesList != null)
                // if lists exists, read all the items
                SPListItemCollection MyChoices = MyChoicesList.Items;
                foreach (SPListItem choice in MyChoices)
                    ChoicesTxt += choice.Title + "\r\n";  
                _CustomChoicesError = true; // list does not exist
        catch (Exception ex)
            _CustomChoicesError = true;
        // in case of error, revert back to original choices
        if (_CustomChoicesError)
            ChoicesTxt = "Enter Choice #1\r\nEnter Choice #2\r\nEnter Choice #3\r\n";
        // write the choices string into idChoices control on page

The list (left), and how the values will look like (right):

If the 'MyChoices' list is deleted or an error occurred during data retrieval, then the page will display the original values.
Note: When you create a column using custom choice values, change value of any of the items temporarily and then undo the change. This is to trigger clearOutDefaultWithCheck() for the default value control to show the first value (its default behavior is to take its value from the resources files). Also you can do this by adding more code to handle this.

I hope you enjoyed with these ideas about customizing the choice values across your farm.

No comments:

Post a Comment