Wednesday, January 28, 2009

How ASP.NET drop down controls Filter the Data View in SharePoint Designer?

To begin, create two lists in the browser: Categories with a field of CategoryName and Products with a field of CategoryName as well (I use a Lookup field type for simplicity, but it doesn't have to be - this code does a simple string compare). Then fill out the lists so you have more than one item in each.

This tip also assumes you already know how to open your Site in SPD and you have created a new page. I realize it is a lot of steps, but I believe they are pretty straight forward as you perform them...

Place your page into design view
Click Task panes :: Data Source Library
Click the data source called Categories
Click to Insert Data Source Control
If prompted, click “Yes” to display the control
Place insertion point below this control
Click Insert :: ASP.NET Controls > Drop Down List
Click the Smart Panel :: Choose Data Source...
Choose the Data Source (for example, SPListDataSource1)
Tell it to display "CategoryName" (or the field that contains your category name)
Make its value "CategoryName"
Click OK
Click to select the dropdownlist control
Click the Smart Panel :: AutoPostBack == True

For getting a default value like "Please select a item..." ?
1. Select the control
2. click on Task Panes > Tag Properties
3. change the AppendDataBoundItem to be True
4. Click the Smart Panel > Edit Items
5. click "Add"
6. type "Please select a item..." for the Text property
7. Click "OK"

Drag and drop the Products list onto a page to create a Data View
Clck the Smart Panel :: Edit Columns to add and remove the columns you want to display
Click Data View :: Filter
Create the following filter:
Field: CategoryName (or the field that contains your category name)
Comparison: Equals
Value: [Create a new Parameter]
Param Name: Param1
Source: Control
Control ID: DropDownList1
Default: Beverages
Click OK
Click OK again
Save the page
Preview in the browser
Make a change to the DropDownList control

Monday, January 26, 2009

How to add a new choice field using WSS Object Model?

SPList list = site.Lists["Demo"];
StringCollection choices = new StringCollection();
choices.Add("AP");
choices.Add("MP");
choices.Add("HP");
list.Fields.Add("State", SPFieldType.Choice, false, false, choices);
SPFieldChoice fldChoice = (SPFieldChoice)list.Fields["State"];
fldChoice.EditFormat = SPChoiceFormatType.Dropdown;
fldChoice.Update();
list.Update();