Thursday, July 10, 2008

Adding data to List Choice column programatically

Hi All,

Although this might be a very simple post to blog but just for information i want to share it.

May times you require to add data into choice column of list.

Let's say you have 5 languages in the list choice column

you have C#, VB.NET, ASP.NET, JAVA, COBOL in the column and you want C# and JAVA to be selected, then here is a simple way to do it.


using(SpSite objsite = new SPSite("<your_site>");
{
using(SPWeb objWeb = objsite .OpenWeb())
{

SPList objList = objWeb.Lists["<your_list>"];
SPListItem objItem = objList .Items.Add();
objItem["Languages"] = ";#C#;#Java";
objItem .Update();

}

}

That's it. Very simple.

10 comments:

yelina said...

how can i add item to choice fild(dropdown list)of list programmaatically using objlistitem.Add()of sharepoint object model

Jayesh Prajapati said...

Hi yelina,
suppose options in your dropdown field is 1,2,3,4
you can add or set value in dropdown field by.
SPListItem item=list.Itesm.Add();
item["DropdownField"]="2";
this way you can set value of 2 in your dropdown field.even you can set value which is not in dropdown field suppose you want value 50 to set you can set it but while you edit this list item in edit mode will not appear in dropdown option.
item["DropdownField"]="50";
so you can't change schema of dropdown but still you can change value.

Anonymous said...

hello,
sorry, but I am a complete newbie to Sharepoint ..
Where do you put this code ? In which file?

thx,

Anonymous said...

Finally! THANK YOU both. Yelina for asking the question, and Jayesh for answering. I've been trying to find this answer for ever. Thinking that it had to do with indexers or something crazy of that nature. But you can just set the text option. Thank You!

Anonymous said...

I am new to sharepoint, I have created a list of areas I want to choose from, but when they choose one are i would a list to come up that breaks down that area and they can choose more than option,I dont want these 'breakdown' lists to show other than when the specific area has been choosen.

SharePoint Kings said...

Anonymous,

can you describe your requirement in detail, with example?

Anonymous said...

this is a good article.
but can i get these values from another list values to populate this drop down list instead of doing this manually.
on other words, how could i lookup across sites.
thanks for your help.

SharePoint Kings said...

Anonymous,

Lookup column is doing the thing exactly what you are looking for.

this is just a sample how programmatically get/set value in the field

Anonymous said...

Hi,

i get an "argumentException; Value does not fall within the expected range." on this line --> item["DropdownField"]="2"

What can it be?

SharePoint Kings said...

@Anonymous,

'DropdownField' means name of the choice field.




Share your SharePoint Experiences with us...
As good as the SharePointKings is, we want to make it even better. One of our most valuable sources of input for our Blog Posts comes from ever enthusiastic Visitors/Readers. We welcome every Visitor/Reader to contribute their experiences with SharePoint. It may be in the form of a code stub, snippet, any tips and trick or any crazy thing you have tried with SharePoint.
Send your Articles to sharepointkings@gmail.com with your Profile Summary. We will Post them. The idea is to act as a bridge between you Readers!!!

If anyone would like to have their advertisement posted on this blog, please send us the requirement details to sharepointkings@gmail.com