Note that there are some explanatory texts on larger screens.

plurals
  1. POSharepoint add a field to all lists/document libraries in a site collection
    primarykey
    data
    text
    <p>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.</p> <p>Code that works for one list:</p> <pre><code> 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) { } } </code></pre> <p>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.</p> <pre><code>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(); } } } } </code></pre> <p>Helper methods</p> <pre><code>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(); } </code></pre> <p>Hope any any one of you are smarter then me here :)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload