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:
SPHttpUtility.HtmlEncode(
"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"
,Response.Output);
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.
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;
try
{
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";
}
}
else
_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
SPHttpUtility.HtmlEncode(ChoicesTxt,Response.Output);
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