How to Create a Blog Site with a Feature Receiver

So there I was a few months ago asked to work on a SharePoint 2010 project. The project was to add a blogging site to an existing public website. The site uses SharePoint 2010. One of my first tasks was to create a feature receiver to accomplish the creation of the blog site. Having never written a feature receiver, I found it to be a very interesting experience.
The Steps
Here are the steps of what my feature receiver had to do.
1. Create a blog publishing site off of the main site collection. So the URL would be www.mainsite.com/blog.
So in the feature activated method, we set a class level variable to the SPWeb and call the other methods in the class.
public override void FeatureActivated(SPFeatureReceiverProperties properties) { //initialization SPWeb web = ((SPSite)properties.Feature.Parent).RootWeb; this.CreateBlogSite(web); this.CreateSiteColumns(web); this.CreateContentTypesAndLists(web); this.CreateBloggingPages(web); }
2. Create the blog site if it does not already exist.
private void CreateBlogSite(SPWeb web) { var blogWeb = web.Webs.Where(p => p.Name.ToLower() == "blog"); if (!blogWeb.Any()) { this._blogSite = RP.SPWebUtility.CreatePublishingSubSite(web, Constants.BlogSubSiteUrl, Constants.BlogSubSiteName, Constants.SharepointBlogSiteTemplate); } else { this._blogSite = blogWeb.First(); } }
3. Create new site columns for the blog information. In my case there were two columns, body and description.
private void CreateSiteColumns(SPWeb web) { if (!web.Fields.ContainsField(Constants.SiteColumnBlogPostBody)) { RP.SPWebUtility.CreateSiteColumn(web, Constants.SiteColumnBlogPostBody, Constants.ContentTypeGroup, SPFieldType.Note, false); } if (!web.Fields.ContainsField(Constants.SiteColumnBlogPostDescription)) { RP.SPWebUtility.CreateSiteColumn(web, Constants.SiteColumnBlogPostDescription, Constants.ContentTypeGroup, SPFieldType.Note, false); } }
4. Create a new blog content type and add it to the out of the box post list. I also set properties on the post list and add other existing fields.
private void CreateContentTypesAndLists(SPWeb web) { var postsList = this._blogSite.Lists.TryGetList(Constants.BlogPostsListName); string[] sparkBlogColumns = { Constants.SiteColumnBlogPostBody, Constants.SiteColumnBlogPostDescription, Constants.SiteColumnCapabilityLookup, Constants.SiteColumnDepartmentLookup, Constants.SiteColumnExpertLookup, Constants.SiteColumnProjectLookup, Constants.SiteColumnTopicLookup, Constants.SiteColumnSEOTitle, Constants.SiteColumnSEOKeywords, Constants.SiteColumnSEODescription }; var sparkBlogPostContentType = web.ContentTypes[Constants.ContentTypeSparkBlogPost]; if (sparkBlogPostContentType == null) { sparkBlogPostContentType = RP.SPWebUtility.CreateContentType(web, Constants.ContentTypeBlogPost, Constants.ContentTypeSparkBlogPost, Constants.ContentTypeIdSparkBlogPost, Constants.ContentTypeGroup, sparkBlogColumns); RP.SPListUtility.AssociateListWithContentTypes(postsList, sparkBlogPostContentType); if (postsList != null) { postsList.ContentTypesEnabled = true; postsList.Fields[Constants.SiteColumnBlogCategory].Hidden = true; postsList.Fields[Constants.SiteColumnBlogCategory].Update(); postsList.ContentTypes[Constants.ContentTypeBlogPost].Hidden = true; postsList.ContentTypes[Constants.ContentTypeBlogPost].Update(); postsList.Fields[Constants.SiteColumnExpertLookup].Required = true; postsList.Fields[Constants.SiteColumnExpertLookup].Update(); postsList.Update(); } } }
5. Create the blog home page and blog post page and add the appropriate web parts to the pages.
private void CreateBloggingPages(SPWeb web) { ////Home Page PublishingWeb pubweb = PublishingWeb.GetPublishingWeb(this._blogSite); var layout = pubweb.GetAvailablePageLayouts().FirstOrDefault(l => l.Name == Constants.PageFileNameBlogPageLayout); var url = pubweb.Url; if (pubweb.GetPublishingPage(string.Format("{0}{1}{2}", url, "/pages/", Constants.PageFileNameBlogHome)) == null) { PublishingPage homePage = pubweb.AddPublishingPage(Constants.PageFileNameBlogHome, layout); this._blogSite.AddWebpartsToPage(homePage.Url, ZoneLeft, new[] { WebPartFactory.BlogPostList("Blog Post List") }); this._blogSite.AddWebpartsToPage(homePage.Url, ZoneRight, new[] { WebPartFactory.PromotionalBox("Our Community", RenderMode.Standard, false) }); pubweb.DefaultPage = homePage.ListItem.File; pubweb.Update(); homePage.CheckIn("Checked in by Blog feature activation"); homePage.ListItem.File.Publish("Published by Blog feature activation"); } ////Post Page if (pubweb.GetPublishingPage(string.Format("{0}{1}{2}", url, "/pages/", Constants.PageFileNameBlogPost)) == null) { PublishingPage blogPage = pubweb.AddPublishingPage(Constants.PageFileNameBlogPost, layout); this._blogSite.AddWebpartsToPage(blogPage.Url, ZoneHeader, new[] { WebPartFactory.BlogHeader("Blog Header") }); this._blogSite.AddWebpartsToPage(blogPage.Url, ZoneTopLeft, new[] { WebPartFactory.BlogPost("Blog Post Body") }); this._blogSite.AddWebpartsToPage(blogPage.Url, ZoneLeft, new[] { WebPartFactory.BlogComments("Blog Post Comments"), }); this._blogSite.AddWebpartsToPage(blogPage.Url, ZoneRight, new[] { WebPartFactory.BlogAboutTheAuthor("About The Author"), WebPartFactory.ShareThis("Share This Blog Post") }); blogPage.CheckIn("Checked in by Blog feature activation"); blogPage.ListItem.File.Publish("Published by Blog feature activation"); } }
The Conclusion
Creating a blog site with pages is easy as 152 lines of code. It works well and runs really fast when being activated.