Hot answers tagged spfield
14
It sounds like you've gotten two concepts slightly muddled. There are three entities that are important here:
A field/column on a list (SPField and its derivatives, including SPFieldUser)
A row of data (SPListItem)
A data item (string, SPUser, etc. according to field type)
Each row of data (List Item) has a data item for each of the fields, but fields ...
7
There are two possibilities here:
1) You need to determine if field is completely custom (added to list by a user from GUI). You can use SourceId property for this purpose. You will get Url, starting with "http://schemas.microsoft.com" for standard fields, and some random GUIDs for custom fields.
Sample code:
SPField field = // get your field from a list ...
6
A better way of doing this is the SPFieldLookupValue class. MSDN has a good example.
In short, something like:
SPListItem lookedUpItem = GetItemSomeHow();
newItem[lookupField] = new SPFieldLookupValue( lookedUpItem.ID, lookedUpItem.Title);
newItem.Update();
6
You can add fields programmatically using list.Fields.AddFieldAsXml, providing CAML field definition with resource tokens in it.
Or you can wrap SetValueForUICulture in a nice helper method, and gain a capability to use it for any SPUserResource object. We're using this approach at work, and I would not say, that it is unclear or smth like this :) It's ok, ...
3
Could be Double or Decimal or Integer.
Use the ParseAndSetValue method to make sure it converts it to Double (see here http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spfieldnumber.parseandsetvalue.aspx)
3
SPFieldChoice has a property named FieldRenderingControl. Using reflector you can find this code in the FieldRedneringControl property:
....
switch (this.EditFormat)
{
case SPChoiceFormatType.Dropdown:
control = new DropDownChoiceField();
break;
case SPChoiceFormatType.RadioButtons:
control = new RadioButtonChoiceField();
...
3
I've had success by not using .ID, but using InternalName with .ContainsFieldWithStaticName(). I also check Hidden and CanBeDeleted on the field as these are more system fields and not custom ones, so I'm not worried about them.
Sample below
SPWeb web = SPContext.Current.Web;
foreach (SPList list in web.Lists)
{
foreach (SPListItem item in list.Items)
...
3
Use the ID of the lookup target:
using(SPSite site = new SPSite("http://test.dev.com"))
using (SPWeb web = site.OpenWeb())
{
SPList list = web.Lists["Suppliers"];
SPListItem item = list.AddItem();
item[SPBuiltInFieldId.Title] = "Test";
item["Product"] = 1; // Use the ID; get it from ...
2
One approach might be Gunnar Peipman's - use reflection to bypass the SetCustomProperty field entirely. It has worked well for me a few times.You may need to serialize/deserialize data in these prope
private void SetFieldAttribute(string attribute, string value)
{
Type baseType;
BindingFlags flags;
MethodInfo mi;
baseType = typeof(MyCustomField);
flags = ...
2
I have tried this code snippet (from console application):
using(SPSite site = new SPSite("http://mycoolsite"))
{
using(SPWeb web = site.OpenWeb())
{
SPList list = web.Lists["My List"];
SPField fldTitle = list.Fields["Title"];
fldTitle.Required = false;
fldTitle.Title = "New title";
...
2
To set a taxonomy field you should use TaxonomyFieldValue or TaxonomiFieldValueCollection
You first need to find the term in your term store:
TaxonomyField taxonomyField = item.Fields[field] as TaxonomyField;
TaxonomyFieldValue taxValues = new TaxonomyFieldValue(taxonomyField);
TaxonomySession session = new ...
2
It sounds like you're using the ItemUpdated event. If you used ItemUpdating I believe you could modify the AfterProperties instead of performing a second update.
This may be of use to you: NBSP: Event Receivers
2
John,
Never tried this myself, but a comment is made http://msdn.microsoft.com/en-us/library/ms437580.aspx stating
"In order to allow enter multiple values into field with Type="UserMulti"
you must set attribute Mult="TRUE"."
2
You need to return the field value in HTML/Plain text format and update it. Here you go:
SPSite site = new SPSite("http://site"))
{
using (SPWeb web = site.OpenWeb())
{
SPList list = web.Lists["YourList"];
SPListItem item= list.GetItemById(1);
SPFieldMultiLineText multilineField = ...
2
I believe with that technique you are creating a Field object for that list but it does not actually add a column, similar to adding columns to a Datatable().
For creating fields via code I typically go with
//Add Column to list
//GUID SPList.Fields.ADD(name,type,required);
list.Fields.Add("Field Name", SPFieldType.Choice, false);
//Get Field ...
1
You need to specifically add the column to the list view. For example,
//Get list object
SPList list = oWeb.Lists[listName];
//Get the view
SPView defaultView = list.DefaultView;
//Add the desired fields
defaultView.ViewFields.Add(libraryNameField.Name);
//Update the view
defaultView.Update();
//Update the List -- Not entirely sure this step is ...
1
In the second content type which you may have created for the ListB specify the Inherits attribute to False for ContentType property. An then in FieldRefs section rename the Status field.
<ContentType ID="CTID"
Name="CTName"
Group="CTGroup"
Description="CTDescription"
...
1
Ok, I've solved it. I am not sure why, but when I use User1 account, I need to explicitly specify the fields I want to get when I am retrieving SPItem object. So, previously I have had instantiated the item object with the following line:
this.item = this.list.Items.GetItemById(Convert.ToInt32(itemId, CultureInfo.InvariantCulture));
But, in order to have ...
1
Did not get your code clearly, what is fieldId and how are you getting this fieldId? my guess is location is already there in the content type you can not add the same field again, create a new field if you wish, in that case your code will be (after foreach) -
string fldName = fields.Add(fieldToAdd.Title, fieldToAdd.Type, fieldToAdd.Required);
SPField ...
1
Your missing adding the field as fieldlink to the Lists version for the content type
var ct = list.ContentTypes["NameOfCT"];
var fieldLink = new SPFieldLink(newField);
ct.FieldLinks.Add(fieldLink);
ct.Update()
When you're using content types in a list only the fields corresponding to the content type is shown in forms. But all fields can be shown in ...
1
Lookup fields (which user extends) supports AllowMultipleValues.
Then you can do:
var multi = field is SPFieldMultiChoice ||
(field is SPFieldLookup && (field as SPFieldLookup).AllowMultipleValues);
Same for Taxonomy
1
Found a proper javascript client object model solution for my problem:
Here is the solution:
this.listFields = list.get_fields();
ctx.load(this.listFields);
And then in the function: onQuerySucceeded
var fieldEnumerator = this.listFields.getEnumerator();
while (fieldEnumerator.moveNext()) {
var field = fieldEnumerator.get_current();
...
1
I don't think what you're attempting to do is possible with the native SharePoint field types.
Normally Custom Field Types define a FieldEditorUserControl that lives in %SharePoint folder%/TEMPLATE/CONTROLTEMPLATES. This defines what to show to the users when adding/editing a field type.
Some of the native field types have controls in there ...
1
Use the build-in webdeveloper tool in the browser (F12) and inspect the element.
I'm pretty sure it's just border around the contextmenu div.
Add the below to your CSS file and brand it. Beware that this will change it for all contextmenus on your site using your CSS file, so you might want to narrow it down to your custom list.
Over all:
.s4-ctx{ }
...
1
The string property you are looking for is SPField.AuthoringInfo. Unfortunately this is a readonly property. But the value of this property is stored in the SchemaXml property of the field. So you can set this value by altering the SchemaXml.
using (SPSite s = new SPSite("http://MyWeb"))
{
using (SPWeb web = s.OpenWeb())
{
SPList l = ...
1
Short answer: No.
The only (out of the box) ways to do this are:
Lookup field
(PRO: behaves as a choice field when rendered)
(CON: only works within 1 site only)
Managed metadata, adding the value as a synonym
(PRO: works everywhere)
(CON: is type ahead, so looks different from choice field)
1
I have exactly same problem in sharepoint foundation 2010. So far i think it is bug in sharepoint. FormFields some how changes listid and itemid in contextes of other controls when Formfield is rendered. Note that this happens only when it rendered, so if you put two formfields on one page but one of them have visible attribute=false, other formfield will be ...
1
Using Resource File could be a good choice but it won't allow the content editors to enter the tranlations. Chris Brian talk about the language store which is quite flexible and easy to implement and solution is available at codeplex too
1
Have a look at the property TitleResource of your field. Through this property you can set your field names for each culture. MSDN Description of SetValueForUICulture
1
its difficult to say why you're getting the exception (some casting issue somewhere) , but i've reworked two lines of your code which might help:
Instead of iterating through all fields in the List, Iterate through all fields in the List item
Indexers is a Exception nightmare in c#, try to use a method which should always return something and not throw ...
Only top voted, non community-wiki answers of a minimum length are eligible

