Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I am provisioning a SharePoint site column using the object model and an xml string as follows:

using (SPSite site = new SPSite("http://myserver"))
        {
            using (SPWeb web = site.OpenWeb())
            {
                string schemaXML = "<?xml version=\"1.0\" encoding=\"utf-16\"?>" +
                                   "<Field ID=\"b6299a44-4a49-4f52-b2df-b938c15bf927\" Name=\"MyField\" Type=\"DateTime\" Required=\"True\" ReadOnly=\"False\" DisplayName=\"My Field\"" +
                                   " StaticName=\"MyField\" ShowInNewForm=\"TRUE\" ShowInDisplayForm=\"TRUE\" ShowInEditForm=\"TRUE\" ShowInListSettings=\"TRUE\" ShowInVersionHistory=\"TRUE\" ShowInViewForms=\"TRUE\" />";

                web.Fields.AddFieldAsXml(schemaXML);
                web.Update();
            }
        }

As you can see I have set the 'Required' attribute to be true as I want to force that the Site Column should always contains data.

However after I run this code and look at the settings of the Site Column it is NOT set to required. I have used SharePoint Manager and switched it to 'required' in the settings page and it does change the 'required' attribute. I can't see any obvious problems in my provisioning xml, can anyone shed any light on why this might be happening?

Thanks, Alex

share|improve this question
add comment

3 Answers 3

up vote 3 down vote accepted

I haven't had any issues with this in the past. Looking at some of my code vs your code. The only thing I'm really seeing is that for required you put in True instead of TRUE. See my code below that I know works:

<Field Type=\"Choice\" DisplayName=\"Target\" Required=\"TRUE\" Format=\"Dropdown\" FillInChoice=\"FALSE\" Name=\"Target\" ColName=\"nvarchar4\"><Default>None</Default><CHOICES><CHOICE>None</CHOICE><CHOICE>New Window</CHOICE></CHOICES></Field>

I'm also curious why you need the ID field specified.

share|improve this answer
    
Thanks for your reply, I will try and change TRUE to capitals and see if it makes a difference. Sorry to confuse - you dont need to specify the ID field I actually ripped this xml from some provisioning code that was already running. If you dont specify the ID I believe SharePoint will just create its own GUID. –  Anonymous Dec 16 '09 at 17:49
    
This is the right answer. Just tested it! –  Jaap Vossers Dec 16 '09 at 18:15
    
Nice! Thanks, for testing it Jaap. –  Steve Lineberry Dec 16 '09 at 19:02
    
good answer. so annoying with this inconsistency on True, TRUE, true across artifacts :-S –  Anders Rask Dec 16 '09 at 20:37
    
Thanks a bunch! I'll go back and make sure all my artifacts are set to TRUE, Cheers. –  Anonymous Dec 17 '09 at 9:25
add comment

Not a specific answer to your question but when xml is involved I use the UI to do what ever I need then use SPManager2007 (from codeplex) to examine the xml.

share|improve this answer
    
Thanks Wor, yes I was using SPManager to examine the xml - pretty useful tool –  Anonymous Dec 16 '09 at 17:43
add comment

EDIT: My theory below here is incorrect. Steve Lineberry came with the right answer. Required="TRUE" should be capitalized.

If I am right, "Required" is one of those properties that only applies to fields on a list.

Same counts for ShowInDisplayForm, ShowInNewForm, ShowInEditForm, etc.

The act of associating a ContentType with a List essentially copies the fields from the ContentType onto the list.

On my current project I am programmatically Creating an SPList, then I associate my ContentType with that SPList, then I get the SPField from SPList.Fields, then I set properties on the SPField and finally I call SPField.Update()

share|improve this answer
    
Good catch, I didn't notice he wasn't adding it to a list and only adding a site column. You are probably right. –  Steve Lineberry Dec 16 '09 at 17:29
    
I did wonder this - whether you can only set this for fields on a list although im confused as to why there is the option in the GUI to set "Required" for a site column on its own. After the above code I actually add the fields to a content type using an SPFieldLink which as I understand it has a required property so I will try to set this to true and see what happens. Cheers –  Anonymous Dec 16 '09 at 17:45
    
Good question. Either that UI doesn't make sense, or I don't make sense :) –  Jaap Vossers Dec 16 '09 at 17:52
    
Yeah unless you can only set that property on the site column settings page when the field has been added to a list. I'll try it and see what it does. –  Anonymous Dec 16 '09 at 17:56
    
Just did everything through the UI, and it is actually copying the Required property from the Site Content Type and enforcing when adding a new item. Perhaps is it BECAUSE I am doing it through the UI that some additional processing is done that you do not get when you do it 100% through the API? –  Jaap Vossers Dec 16 '09 at 17:58
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.