Update for MyMasters Solution -16th Nov 2011

Hi Everyone recently i uploaded a new version (1.1) of MyMasters solution.

the solution now covering fallowing questions’ answers :

* How to deploy a custom wellcome page to Personal Site ?
* How to add custom webparts to a page ?
* How to use XsltListViewWebPart and add a document library as a webpart ?

You can download new version from CodePlex site of MyMasters solution:
http://mymasters.codeplex.com/

Please visit related post about MyMasters:
https://blog.bugrapostaci.com/2011/10/24/attach-custom-master-page-to-personal-site-with-featurestabling-for-sharepoint-2010/

Here is a code sample for above questions :
as you guess CreateWellcomePage function has been calling in Feature Reciever class of MyMasters Feature.

  1. private void CreateWellcomePage(SPFeatureReceiverProperties properties)
  2.         {
  3.             SPSite site = (SPSite)properties.Feature.Parent;
  4.             SPWeb web = site.OpenWeb();
  5.             PublishingSite pSite = new PublishingSite(site); //Get Publishing Site Object
  6.             SPContentType ctype = pSite.ContentTypes[“Welcome Page”]; //Get builtin “Wellcome Page” content type
  7.             PageLayoutCollection pageLayouts = pSite.GetPageLayouts(ctype, true); //Get builtin pagelayouts for this content type
  8.             PageLayout pageLayout = pageLayouts[0]; //Select first one which is BlankWebPartPage pagelayout
  9.             PublishingWeb pWeb = PublishingWeb.GetPublishingWeb(web); //Get Publishing Web.
  10.             PublishingPageCollection pPages = pWeb.GetPublishingPages(); // Get Page Collection
  11.             //TempFile for changing default page
  12.             string tempFileName = “TempFileForChangeWellcomePage.aspx”;
  13.             PublishingPage tempPage = null;
  14.             try
  15.             {
  16.                 //Get if there is an existing wellcome.aspx
  17.                 PublishingPage pPageExist = pPages[pWeb.Url + “/Pages/Wellcome.aspx”];
  18.                 if (pPageExist != null)
  19.                 {
  20.                     //Check wellcome.aspx is default page
  21.                     if (pWeb.DefaultPage.UniqueId == pPageExist.ListItem.File.UniqueId)
  22.                     {
  23.                         //if it is a default page we have to create a template page because the default page is not deleted.
  24.                         //Create a temporary page
  25.                         tempPage = pPages.Add(tempFileName, pageLayout);
  26.                         tempPage.ListItem.File.CheckIn(“Chk”);
  27.                         tempPage.ListItem.File.Publish(“Pub”);
  28.                         //Set this temporary file as default page so we can able to delete existing wellcome.aspx
  29.                         pWeb.DefaultPage = tempPage.ListItem.File;
  30.                         pWeb.Update();
  31.                     }
  32.                     //Delete Existing Wellcome.aspx
  33.                     if (pPageExist.ListItem.File.CheckedOutByUser != null)
  34.                     {
  35.                         pPageExist.ListItem.File.UndoCheckOut();
  36.                     }
  37.                     pPageExist.CheckOut();
  38.                     pPageExist.ListItem.Delete();
  39.                     pWeb.Update();
  40.                 }
  41.             }
  42.             catch(Exception ex)
  43.             {
  44.             }
  45.             //Create a new Wellcome.aspx file
  46.             PublishingPage pPage = pPages.Add(“Wellcome.aspx”, pageLayout);
  47.             SPListItem newpage = pPage.ListItem;
  48.             newpage[“Title”] = “Wellcome to my personal site”;
  49.             //Add need webparts to page.
  50.             using (SPLimitedWebPartManager wpMgr = web.GetLimitedWebPartManager(pPage.Url, PersonalizationScope.Shared))
  51.             {
  52.                 //Get WebPart Catalog
  53.                 SPList webPartCatalog = web.GetCatalog(SPListTemplateType.WebPartCatalog);
  54.                 foreach (SPListItem item in webPartCatalog.Items)
  55.                 {
  56.                     if (item.DisplayName == “LatestBlogPostsPublic”)
  57.                     {
  58.                         string fileName = string.Format(“{0}/{1}”, item.Web.Url, item.File.Url);
  59.                         XmlTextReader reader = new XmlTextReader(new StringReader(item.Web.GetFileAsString(fileName)));
  60.                         string error;
  61.                         System.Web.UI.WebControls.WebParts.WebPart wx = wpMgr.ImportWebPart(reader, out error);
  62.                         wpMgr.AddWebPart(wx, “Header”, Convert.ToInt32(0));
  63.                     }
  64.                 }
  65.                 //Add existing Document Library views
  66.                 SPList SharedDocumentsList = web.Lists[“Shared Documents”];
  67.                 SPView SharedDocumentsListView = SharedDocumentsList.Views[0];
  68.                 SPList PersonalDocumentsList = web.Lists[“Personal Documents”];
  69.                 SPView PersonalDocumentsListView = PersonalDocumentsList.Views[0];
  70.                 XsltListViewWebPart wp = new XsltListViewWebPart();
  71.                 wp.ListId = SharedDocumentsList.ID;
  72.                 wp.Title = “Shared Documents”;
  73.                 wp.ChromeType = PartChromeType.TitleOnly;
  74.                 wp.ViewGuid = SharedDocumentsListView.ID.ToString();
  75.                 wp.XmlDefinition = SharedDocumentsListView.GetViewXml();
  76.                 wpMgr.AddWebPart(wp, “Header”, Convert.ToInt32(1));
  77.                 XsltListViewWebPart wp2 = new XsltListViewWebPart();
  78.                 wp2.ListId = PersonalDocumentsList.ID;
  79.                 wp2.Title = “Personal Documents”;
  80.                 wp2.ChromeType = PartChromeType.TitleOnly;
  81.                 wp2.ViewGuid = PersonalDocumentsListView.ID.ToString();
  82.                 wp2.XmlDefinition = PersonalDocumentsListView.GetViewXml();
  83.                 wpMgr.AddWebPart(wp2, “Header”, Convert.ToInt32(2));
  84.             }
  85.             newpage.File.CheckIn(“Administravily Check in”);
  86.             newpage.File.Publish(“Administravily Published”);
  87.             //set wellcome.aspx as default page.
  88.             pWeb.DefaultPage = newpage.File;
  89.             pWeb.Update();
  90.             //delete if tempfile is exists.
  91.             if (tempPage != null)
  92.             {
  93.                 tempPage.ListItem.Delete();
  94.             }
  95.             pWeb.Update();
  96.         }

Quick Tip: Adding content database with stsadm

It is important when the content database need to upgrade the gui interface can not able to complete this operation.you have to use stsadm for doing that.

Command:
stsadm -o addcontentdb -url “<siteURL>” -databasename “<databaseName>” -databaseserver “<SQLServerName>”

Example
stsadm -o addcontentdb -url “http://blog.bugrapostaci.com” -databasename WSS_Content_Blog -databaseserver “postpoint2003”

You can get detail information from MSDN:
http://technet.microsoft.com/en-us/library/cc263422(office.12).aspx