Showing posts with label Begginer Guide. Show all posts
Showing posts with label Begginer Guide. Show all posts

Thursday, August 12, 2010

How to set values for a checkbox choice list field? (SPFieldMultiChoice)

This is a question I just recieved through my "contact" form for this blog. The person (Chris) who asked the question gave a wrong email address, so I will post the answer publicly since I believe this can help other starting sharepoint developers - especially since I saw some wrong tips coming up in google when you search for it. So here is the right way to do it.

Question: "Hi, thanks a mil for those snippets!One question: I'm trying to programatically add selected items of a checkBoxList to a SPListItem, but am not sure in what format it wants it. i.e I've added all items as you suggested - newItem["Name"] = "blah" etc, but when it comes to the field that requires the selected checkboxlist values, I'm stuck."

Answer: to set the value for a check box choice list (a field of type choice, with multiple choices allowed - showing checkboxes), you should use the SPFieldMultiChoiceValue class to set the value. This class allows you to construct the list of choices you want to set for the column by adding the string values to it. Here is an example on how to use it:

SPList sampleList =web.Lists["test"];
SPFieldMultiChoiceValue val = new SPFieldMultiChoiceValue();
val.Add("choice1");
val.Add("choice3");
SPListItem newItem = sampleList.Items.Add();
newItem["Title"] = "This is the new item";
newItem["test"] = val;
newItem.Update();

Monday, April 30, 2007

Explanation for sharepoint begginers - the difference between web parts and lists (my two web parts are showing the same data)

Did you try adding another web part to a page and it displays the same data as another web part on the page? Here is the answer.

As part of my habit of posting in this site an answer for any question I see more than once in the forums, I want to share with you an explanation I use in the forums to let sharepoint begginers realize what is the difference between a web part and a list.

The confusion sounds like this:
"I'm a newbie to the SharePoint world so if this is a really amateur question, you know why. I am currently tasked with setting up the SharePoint site for my department. I have tried to put two instances of the Links web part on the top level of my site. It will allow me to do this but when I make a change to one of the web parts the change also occurs in the other Links web part." (taken from an msdn forum post)

To which I reply:
You are confusing "web parts" and "lists"
A "web part" is a mechanism to display data, while a "list" is like a small database - a mechanism to store data.

What you did, is have one list (links, or contacts) and two web part displaying the same list - same data.

If you want two different sets of data then the easy way would be to create another list, and add a web part to look into that list.

Another option is to create a field in the list by the name (for example) "show in web part 1" of type boolean, and then configure the two webparts that show the same information to show based on that field (this method is known as a filter).