Deleting Columns
If a site collection contains lists that contain a site column you want to delete, you cannot delete that site column. If you delete a column from a list, that column is also deleted from any list content type on that list. When you delete a column, all data associated with that column is also deleted.
Site column usage in sharepoint:
There are three ways to use site columns in sharepoint and they are:
1.Using the central administration tool of sharepoint.
2.Using the WSS object model through code snippet.
3.Using features(XML file).
Using features to add site columns in the sharepoint site.
Feature.xml
The feature.xml file is a general description of the feature and looks like this:
Description="Fields added through feature"
Version="12.0.0.0"
Scope="Site"
xmlns="http://schemas.microsoft.com/sharepoint/">http://schemas.microsoft.com/sharepoint/>
Site column feature xml
The file fields.xml looks like this:
Group="MyColumns"
Type="Choice"
DisplayName="Region"
SourceID=http://schemas.microsoft.com/sharepoint/v3/fields
StaticName="RegionFromFeature"
FillInChoice="FALSE">
Use STSADM command to install and activate the feature and do an IISRESET.
Commands are:
stsadm -o installfeature -filename fields\feature.xml
stsadm -o activatefeature -filename fields\feature.xml -url “Site Collection URL”
Method for using the WSS object model to add a new site column:
SPSite site=new SPSite(“http://servername/myPortal”)”;
SPWeb web=site.OpenWeb(“/Topics”);
String newFld=web.Fields.Add(“Location”,SPFieldType.Choice,True);
newFld.Choices.Add(“Hyderabad”);
newFld.Choices.Add(“Banglore”);
newFld.Choices.Add(“Jamshedpur”);
newFld.DefaultValue=”Jamshedpur”;
newFld.Update();
Method for using the WSS object model to add a site column to a list:
SPSite site=new SPSite(“http://servername/myPortal”);
SPWeb web=site.OpenWeb(“Topics/MOSS2007”);
SPList list=web.Lists[“Shared Documents”];
SPField myFld=web.AvailableFields[“MyField”];
List.Fields.Add(MyField);
Add site columns to Content type using WSS object model:
SPSite site = new SPSite("http://servername/myPortal");
SPWeb web = site.OpenWeb("/Topics");
// Create new content type
SPContentType documentContentType = web.AvailableContentTypes["Document"];
SPContentType newContentType = new SPContentType(documentContentType, web.ContentTypes, "Address Document");
web.ContentTypes.Add(newContentType);
newContentType = web.ContentTypes[newContentType.Id];
// Add FieldLink to content type
SPField myFld = web.AvailableFields["myField"];
SPFieldLink fieldLink = new SPFieldLink(myFld);
newContentType.FieldLinks.Add(fieldLink);
newContentType.Update(false);
The above code snippet shows how to add site columns to a content Type. Here just add a new SPFieldLink object to the FieldLinks collection of the contenttype.
No comments:
Post a Comment