1: public override void FeatureActivated(SPFeatureReceiverProperties properties)
2: {
3: try
4: {
5: using (SPWeb web = ((SPSite)properties.Feature.Parent).OpenWeb())
6: {
7: //create the travel system subsite, its groups, and pages.
8: CreateSiteAndPages(web);
9:
10: //strip perm's on layouts so that regular content authors/editors cannot create instances of our custom page layouts
11: //(if permission has been removed the page won't show up in the CreatePage list of layouts.
12: StripLayoutPermissions(web);
13: }
14: }
15: catch (Exception e)
16: {
17: System.Diagnostics.EventLog.WriteEntry(this.ToString(), "An error occurred during the FeatureActivated event." +
18: Environment.NewLine + e.ToString(), System.Diagnostics.EventLogEntryType.Error);
19: }
20: }
21:
22: private void CreateSiteAndPages(SPWeb web)
23: {
24: //create the travel system site collection.
25: SPWeb aisSystemWeb = web.Webs.Add(@SiteUrl,SiteName, "AIS System Site Architecture Prototype", Convert.ToUInt16(1033), web.WebTemplate, true, false);
26:
27: //create site groups (if they don't already exist) and assign proper permissions.
28: CreateAllSiteGroups(web, aisSystemWeb );
29:
30: PublishingWeb aisSystemPublishingWeb = PublishingWeb.GetPublishingWeb(aisSystemWeb );
31: //create the root level pages.
32: CreateAndPublishPage(aisSystemPublishingWeb , "AisHome.aspx", "AIS System Home", true);
33: }
34:
35: //create a single publishing page for the specified page layout.
36: private PublishingPage CreateAndPublishPage(PublishingWeb publishingWeb, string layoutName, string pageName, string title, bool inludeInNav)
37: {
38: //get a list containing all my custom page layouts
39: Dictionary<string, PageLayout> layouts = GetPageLayouts(publishingWeb);
40:
41: if (!layouts.ContainsKey(layoutName.ToUpper()))
42: {
43: System.Diagnostics.EventLog.WriteEntry(this.ToString(), String.Format("Unable to create page because the layout '{0}' was not found.", layoutName),
44: System.Diagnostics.EventLogEntryType.Error);
45: return null;
46: }
47:
48: PageLayout layout = layouts[layoutName.ToUpper()];
49:
50: if (pageName == null)
51: pageName = layoutName;
52:
53: PublishingPage page = publishingWeb.GetPublishingPages().Add(pageName, layout);
54: page.Title = title;
55: page.IncludeInCurrentNavigation = inludeInNav;
56: page.IncludeInGlobalNavigation = false;
57: page.Update();
58: page.CheckIn("");
59: page.ListItem.File.Publish("");
60: try
61: {
62: page.ListItem.File.Approve("");
63: }
64: catch (SPException) { }//not all sites require content approval, but I don't know how to check for this :-)
65: return page;
66: }
67:
68: private Dictionary<string, PageLayout> GetPageLayouts(PublishingWeb web)
69: {
70: //get a (hardcoded) list of all my custom page layout names.
71: Dictionary<string, string> allLayoutNames = GetPageLayoutNameLookupList();
72: Dictionary<string, PageLayout> layouts = new Dictionary<string, PageLayout>();
73: //this is the content type id for the 'Page' content type
74: const string PageLayoutContentId = "0x010100C568DB52D9D0A14D9B2FDCC96666E9F2007948130EC3DB064584E219954237AF39";
75: SPContentTypeId pageContentTypeId = new SPContentTypeId(PageLayoutContentId);
76:
77: foreach (PageLayout layout in web.GetAvailablePageLayouts(pageContentTypeId))
78: {
79: if( allLayoutNames.ContainsKey(layout.Name.ToUpper()))
80: layouts.Add(layout.Name.ToUpper(), layout);
81: }
82:
83: return layouts;
84: }
85:
86: private void StripLayoutPermissions(SPWeb web)
87: {
88: Dictionary<string, string> allLayoutNames = GetPageLayoutNameLookupList();
89: PublishingWeb pubWeb = PublishingWeb.GetPublishingWeb(web);
90: SPContentTypeId pageContentTypeId = new SPContentTypeId(PageLayoutContentId);
91:
92: foreach (PageLayout layout in pubWeb.GetAvailablePageLayouts(pageContentTypeId))
93: {
94: if (allLayoutNames.ContainsKey(layout.Name.ToUpper()))
95: {
96: SPListItem item = layout.ListItem;
97: item.BreakRoleInheritance(false);
98: }
99: }
100: }