The following code does some interesting stuff:
- Creates a document library in code
- Sets the document library title, enables versioning and allows content types in the document library
- Finds a site content type and associates it with the document library
- Changes the content type order so it contains just my content type
- Sets the default page of the site to the default view of the document library
- Adds some of my content type fields to the default view
public class ProjectSiteReceiver : SPFeatureReceiver
{
private string _documentLibraryTitle = "Project Documents";
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
using (SPWeb web = (SPWeb)properties.Feature.Parent)
{
SPListTemplateType templateType = SPListTemplateType.DocumentLibrary;
Guid listId = web.Lists.Add(_documentLibraryTitle, null, templateType);
SPList list = web.Lists[listId];
list.Title = _documentLibraryTitle;
list.EnableVersioning = true;
list.ContentTypesEnabled = true;
list.Update();
SPContentType contentType = web.GetContentType("Project Document");
SPContentType listContentType = list.ContentTypes.Add(contentType);
listContentType.Update();
List<SPContentType> contentTypeList = new List<SPContentType>();
contentTypeList.Add(listContentType);
list.RootFolder.UniqueContentTypeOrder = contentTypeList;
list.RootFolder.Update();
SPFolder webFolder = web.RootFolder;
webFolder.WelcomePage = list.DefaultView.Url;
webFolder.Update();
SPView view = list.DefaultView;
view.ViewFields.Add("Project ID");
view.ViewFields.Add("Project Type");
view.Update();
}
}
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
using (SPWeb web = (SPWeb)properties.Feature.Parent)
{
SPList list = web.GetListByName(_documentLibraryTitle);
list.Delete();
}
}
public override void FeatureInstalled(SPFeatureReceiverProperties properties)
{
}
public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
{
}
}
PS: Sorry GetContentType and GetListByName are my own custom extension methods. There isn't much to them really, just better error handling then using the default SharePoint API methods with square brackets.
PPS: You also need to set the welcome page back to the default in the FeatureDeactivating method:
SPFolder webFolder = web.RootFolder;
webFolder.WelcomePage = "default.aspx";
webFolder.Update();
No comments:
Post a Comment