Sharepoint 2010 add a choice field - sharepoint-2010

I have to add a choice field (dropdown) to a sharepoint list. I'm using the SharePoint API in Visual Studio.
Here is me code. I have tried to run this and there are no errors but it doesn't work.
private void addChoiceField(SPFeatureReceiverProperties properties, String _listName, String _fieldName)
{
using (SPWeb _web = properties.Feature.Parent as SPWeb)
{
SPList _list = _web.Lists.TryGetList(_listName);
writeLog(properties, "list name:" + _listName);
SPFieldChoice _fieldDD = (SPFieldChoice)_list.Fields[_fieldName];
writeLog(properties, "fieldname:" + _fieldName);
if (_fieldName == "State")
{
_fieldDD.Choices.Clear();
_fieldDD.Choices.Add("Gesloten-Verloren");
_fieldDD.Choices.Add("Analyse nodig");
_fieldDD.Choices.Add("Onderhandeling of revise");
_fieldDD.Choices.Add("Presentatie of demo");
_fieldDD.Choices.Add("Voorstel voor prijsofferte");
_fieldDD.Choices.Add("Prospect");
_fieldDD.Choices.Add("Waardevol");
_fieldDD.Choices.Add("Gesloten-Gewonnen");
_fieldDD.Update();
}
}
}
Does anyone knows what's wrong or how to add a choicefield in a different way using the API?

You need to call
_list.Fields.CreateNewField
to actually add it to the list instead of trying to pull an existing field from the list.
Here is a link to the method description: http://msdn.microsoft.com/en-us/library/microsoft.harepoint.spfieldcollection.createnewfield.aspx

Related

How do I retrieve a particular piece of metadata of a selected List Item in Visual Studio (Sharepoint)?

I'm doing up a web part using visual studio for my sharepoint site and I require part of it to retrieve documents which are related to the selected one.
Each document which I've created on the sharepoint site for testing purposes has its own metadata and I wish to retrieve the documents which have a particular piece of metadata (say, 'file name' or 'written by' etc.)
hence in the method which I've coded for this particular function, I've managed to retrieve the metadata collectively into a hashtable (or so I think i have) but I can't seem to extract a single piece of metadata out of it.
protected void displayFileNames(object sender, EventArgs e) {
String selectedDocLib = TypeOfDocument.SelectedValue;
objCurrentWeb = SPContext.Current.Web;
SPListCollection objDocLibList = objCurrentWeb.GetListsOfType(SPBaseType.DocumentLibrary);
String tempStr = "";
SPList tempDocLib;
Hashtable hT = new Hashtable();
foreach (SPList docLibList in objDocLibList) {
tempStr = docLibList.Title;
if (tempStr.Equals(selectedDocLib)) {
tempDocLib = objDocLibList.TryGetList(selectedDocLib);
foreach (SPListItem objTempDocLibItem in tempDocLib.Items) {
hT = objTempDocLibItem.Properties;
int isOriginal = hT.GetHashCode();
ListBox1.Items.Add(objTempDocLibItem.Name);
}
}
}
}
Please help me out here!!
Thank you!!!
Hailey

Get List Item by GUID

I am working on sharepoint-2010 and I am new to sharepoint.
I want to access List Item by Guid. So, how can I access.
Please provide some reference or some class file to access that.
Thanks in advance.
Use the SPList.GetItemByUniqueId method:
using (SPSite site = new SPSite(url))
{
using (SPWeb web = site.OpenWeb())
{
SPList list = web.Lists[listTitle];
SPListItem item = list.GetItemByUniqueId(itemGuid);
}
}

Add item to Sharepoint Survey list

I have a survey list in sharepoint and I'd like to add an answer to the survey using c# code, this I think could be interpreted as add an item to the list so I tried the next but is not working.
How can this be done?
using (SPSite site = new SPSite("http://user-pc"))
{
using (SPWeb web = site.RootWeb)
{
try
{
SPList olist = web.Lists["JobSchedulle"]; //<- Name of the survey in sharepoint 2010
SPListItem item = olist.Items.Add();
item[5] = "good";// <- this survey only has one question, and is a text option ..
item.Update();
}
catch {
Console.ReadLine();
}
}
}
I found the error - the code works, but I was accessing item[5] (with the wrong index).

unable to update listitem external data column sharepoint 2010

I try update Extenal data column but it doesn't work, new value is not stored. (new value is visible on details form but not on list, rehreshing external data type does not return related external column values)
using (SPSite oSiteCollection = new SPSite("site.com"))
{
using (SPWeb oWebsite = oSiteCollection.OpenWeb("site.com"))
{
using (SPWeb oWebsiteRoot = oSiteCollection.RootWeb)
{
SPList docLib = oWebsiteRoot.Lists["list name"];
SPListItemCollection items = docLib.Items;
foreach (SPListItem item in items)
{
//item["n"] is external column data field
item["n"] = item["notice"].ToString();
item.UpdateOverwriteVersion();
}
}
}
}
Check using item.Update() insted of item.UpdateOverwriteVersion()
This has to do with the field type and is quite complicated to get right.
There is a free external data field migration/copy tool here:
http://rrfreeman.blogspot.com/2013/06/bcs-bdc-external-data-lookup-field.html
I included source code and links to relevant articles.

How do I make the "change order" button appear in SharePoint 2010?

How do I make the "change order" button appear in SharePoint 2010?
I have followed a guide that allowed me to add OrderedList="TRUE" to my list template. This makes it possible to select "Allow users to order items in this view" for my view. But the change order button is still missing. Any idears on what I am missing?
I am using SharePoint 2010 and the guide is from 2006, so that might explain why it doesn't just work.
The guide from tech-archive.net.
Not sure if you tried this already, but in SP 2007 after you deploy your list adding the OrderedList=TRUE attribute, you still need to modify the view and under sorting you'll see a new option "Allow user to sort items in this view". The "Change Order" button doesn't appear until you set that option to "Yes".
I created a little console app to help me set the OrderedList attribute.
class Program {
public static SPSite GetAdminSPSite() {
SPSite spsite = null;
SPSecurity.RunWithElevatedPrivileges(delegate() {
spsite = new SPSite("http://sharepointdev");
});
return spsite;
}
static void Main(string[] args) {
if (args.Length != 2) {
Console.WriteLine("Missing sitename parameter and the list name.");
return;
}
string sitename = args[0];
string listname = args[1];
using (SPSite site = GetAdminSPSite()) {
using (SPWeb web = site.OpenWeb("ClientSites/" + sitename)) {
SPList list = web.Lists[listname];
list.Ordered = true;
list.Update();
}
}
}
}
Once that is run then you need to modify the view as #Jeff Smith says.