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'm programmatically creating Site Collections. The only thing I'd like to add to the newly created Site Collection are some Custom Columns that I've added manually in other examples..

The code I'm using is this:

 string SiteCollectionUrl = "/sites/Prova212222";

        Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(() =>
        {
            var Application = SPWebApplication.Lookup(new Uri("http://sp2010dev:44411/"));
            Application.Sites.Add(SiteCollectionUrl, "virtualsp\\Administrator", string.Empty, 1033, "STS#0", "virtualsp\\Administrator", "virtualsp\\Administrator", String.Empty);

            using (SPSite site = new SPSite("http://sp2010dev:44411" + SiteCollectionUrl))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    site.AllowUnsafeUpdates = true;
                    web.AllowUnsafeUpdates = true;

                    web.AvailableFields.Add("tt_content_id_padre", SPFieldType.Text, true);
                   //And so on for other columns
                }
            }
        });  

When I reach the Add method system raises this exception:

 This functionality is unavailable for field collections not associated with a list.

How can I solve this problem? I'm not interested in creating content type and so on, the only thing i need is the columns as I added manually!

Thank you

share|improve this question

3 Answers 3

up vote 2 down vote accepted

You should use SPWeb.Fields instead of SPWeb.AvailableFields

SPWeb.Fields are for the fields associated with this SPWeb
SPWeb.AvailableFields are for the fields available for this SPWeb or parents and is readonly

Here is an example of updating choices of a Choice Field using PowerShell:

$w = Get-SPWeb http://sp2010
$f = $w.Fields["pejaChoice"]
$f.Choices.Add("Testing")
$f.Update()
share|improve this answer
    
How can I modify the column? For example, if I've added a choice, how can I add the choices? I tried getting it from availableFields and modify (with update), but this cause an exception.. –  Ziba Leah Apr 3 '12 at 11:40
1  
You can't modify anything you get from AvailableFields. You have to get it from the right Fields (either on this web or on the parent where it was created) and then update it. So it's a good rule only to create fields at the RootWeb a Site collection (which is also the only thing that features allow) –  Per Jakobsen Apr 3 '12 at 12:00
1  
But if it's a field where you have the risk of having to add choices, then is should probably have been a lookup or taxonomy field. Choice fields are good for VERY STATIC choices. But hard to update, because you also have to update the choices in every list –  Per Jakobsen Apr 3 '12 at 12:12
    
But the first time I create a Choice, I must set the Choices.. right? –  Ziba Leah Apr 3 '12 at 12:46
1  
Yes when you create a choice field you should set the Choices. But I recommend that you create fields using xml not though code. –  Per Jakobsen Apr 3 '12 at 13:01

Use web.Fields.Add or better yet. Add them to the site collection fields like this: site.RootWeb.Fields.Add() and do a web.Update()

After that you can create ContentTypes, or add Fields to lists by getting the sitecolumns using web.AvailableFields["yourfield"].

share|improve this answer

Are you using any specific site template while creating the site, you may want to consider creating a feature that defines the site columns,

Then use the feature stapler to associate the site column feature with the site template.

This way you will be sure that the code may not fail for any reason.

Remember writing more code for doing this will increase the possiblity of failures.

share|improve this answer

Your Answer

 
discard

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.