<SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel:=True)> _ <SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel:=True)> _ Public Class SPField
Dim instance As SPField
[SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel=true)] [SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel=true)] public class SPField
Use the Fields property of either the SPList class or the SPListItem class to return an SPFieldCollection object that represents the collection of fields for a list or list item. Use an indexer to return a single field from this collection. For example, if the collection is assigned to a variable named collFields , use collFields[index] in C#, or collFields(index) in Visual Basic, where index is either the index number of the field in the collection or the display name of the field.
collFields[
]
collFields(
)
If you set properties of the SPField class and its inheriting classes, you must call the Update for changes to take effect in the database.
The following code example iterates through all the lists in all the subsites under a site and, if it finds a list with a specified name, updates the title, default value, and description for a field.
This example requires using directives (Imports in Visual Basic) for the Microsoft.SharePoint and Microsoft.SharePoint.Utilities namespaces.
Dim siteCollection As SPSite = SPContext.Current.Site Dim sites As SPWebCollection = siteCollection.AllWebs("Site_Name").Webs Dim site As SPWeb For Each site In sites Dim lists As SPListCollection = site.Lists Dim i As Integer For i = 0 To lists.Count - 1 If lists(i).Title = "List_Name" Then Dim fieldChange As SPField = lists(i).Fields("Field_Name") fieldChange.DefaultValue = "Default_Value" fieldChange.Description = "Description" fieldChange.Title = "New_Field_Title" fieldChange.Update() End If Next i Next site
SPSite oSiteCollection = SPContext.Current.Site; using (SPWeb oWebsite = oSiteCollection.AllWebs["Site_Name"]) { SPFieldCollection collFields = oWebsite.Lists["List_Name"].Fields; for (int intIndex = 0; intIndex < collFields.Count; intIndex++) { string[] strRefFields = collFields[intIndex].FieldReferences; if (strRefFields != null) { for (int intStrPos = 0; intStrPos < strRefFields.Length; intStrPos++) { Label1.Text += collFields[intIndex].InternalName + " :: " + SPEncode.HtmlEncode(strRefFields[intStrPos]) + "<BR>"; } } } }
Certain objects implement the IDisposable interface, and you must avoid retaining these objects in memory after they are no longer needed. For information about good coding practices, see Best Practices: Using Disposable Windows SharePoint Services Objects.
Visual Basic example doesn't pay attention to my language filter. I have my filter set to C# only, but the example for Visual Basic appears, too.
It might have something to do with the text--it is listed as " [Visual Basic]", while the VB code blocks that are correctly filtered out are listed as "Visual Basic" (i.e. without the square brackets). Just a guess...