Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8593923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:05:45+00:00 2026-06-12T00:05:45+00:00

I got it working with adding it to a specified list, but I want

  • 0

I got it working with adding it to a specified list, but I want to go trough all lists in the site collection. I just cant find out whats making this not work.

Code that works for one list:

    void btnAdd_Click(object sender, EventArgs e)
    {
        try
        {
            using (SPSite site = new SPSite("http://asdf:150/sites/test"))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPList list = web.Lists["test8"];

                    //Needed?
                    site.AllowUnsafeUpdates = true;
                    web.AllowUnsafeUpdates = true;

                    string mainLocation = list.Fields.Add("New Field", SPFieldType.Text, false, false, null);
                    readOnly(list, mainLocation);

                    //Add to default view (will be removed after testing)
                    SPView view = list.DefaultView;
                    defaultView(view, "New Field");
                }
            } 
        }
        catch (Exception ex)
        {
        }
    }

I want to add the field to all lists in the site collection, but I dont know what I could be doing wrong, this is what I have so far.

void btnAdd_Click(object sender, EventArgs e)
        {
            using (SPSite site = new SPSite("http://asdf:150/sites/test"))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    //Needed?
                    site.AllowUnsafeUpdates = true;
                    web.AllowUnsafeUpdates = true;
                    try
                    {
                        SPListCollection lists = web.Lists; // ??
                        foreach (SPList list in lists)
                        {
                            string mainLocation = list.Fields.Add("New Field", SPFieldType.Text, false, false, null);
                            readOnly(list, mainLocation);

                            //Add to default view (will be removed after testing)
                            SPView view = list.DefaultView;
                            defaultView(view, "New Field");
                        }
                    }
                    catch (Exception)
                    {
                        // some exception handling
                    }
                    finally
                    {
                        //web.Dispose();
                    }
                }
            }
        }

Helper methods

void readOnly(SPList list, string name)
{
    SPField listField = list.Fields.GetFieldByInternalName(name);
    listField.ReadOnlyField = true;
    listField.Update();
}

void defaultView(SPView view, string field)
{
    view.ViewFields.Add(field);
    view.Update();
}

Hope any any one of you are smarter then me here 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T00:05:47+00:00Added an answer on June 12, 2026 at 12:05 am

    I run your code and you have 2 main problems.
    1. Default view can bu null in some lists. So you should check it.

    SPView view = list.DefaultView;
    if(view != null)
    {
        defaultView(view, "New Field");
    }
    

    2. SPListCollection is modified, when you make changes to list. You should have
    temporary IEnumerable for iterating through lists.

    using System.Linq;
    
    SPListCollection listCollection = web.Lists;
    IEnumerable<SPList> lists = listCollection.Cast<SPList>();
    foreach (SPList list in lists)
    {
    

    3. Do not ignore Excaptions. Add some code to catch block, that can help you.

    catch (Exception ex)
    {
        Console.WriteLine(ex.ToString());
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got this answer working but I'd like to extend it: jQuery UI Datepicker
I've got a nice drop down list working which is getting populated from a
I've got (working) application done with ttk. It uses self-created module for showing a
At the moment, I've got working: public void logowanie() { int x=5,y=5; ... }
Update: Got this working by disabling CodeIgniter Profiler which was interfering with the Ajax
I got a working server in C# and a working client in Java (Android).
Has anyone got this working in a web application? No matter what I do
I've got Drupal working on a shared host, and I uploaded some modules from
I've got ELMAH working on my (Cassini) development server, and was quite happy with
I installed TeamCity and got it working against my project. However, I have since

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.