n
n
n
nnWhilenworking with SharePoint 2010 one of my fried came across a scenario where henneeded to modify the home page of team site using a feature. Feature shouldndelete the existing content in out of the box team site’s home page and add newnweb parts.
n
nThoughnthis sounds very simple in first shot but practically little tricky, that’snwhat I experienced.
n
nSharePointn2010 has come up with new features and one of them is modification in teamnsites. If you observe that when a new out of the box team site is created,nthere is no default.aspx instead default page is replaced by Home.aspx.nHome.aspx is a wiki page and resides in the Site Pages library of team site.
n
nAsnthis is a wiki page so it’s good that this supports direct editing and you cannadd web parts directly in the rich text area using UI. But when it comes tonadding web parts to this page programmatically then problem begins.
n
n
n
nWaldek Mastykarz has given excellent workaroundnto this problem where he has shown how to add a web part in Publishing PagenContent of publishing page. We can usensimilar technique to add web parts to wiki page as well, only difference is ofnfield name of wiki page.
n
nIn thenexample I have added two list view web parts in Home.aspx page , one reside atnleft and other one is at right of page. This can be done with simple HTMLnmarkup where <td> and <tr> are defined with certain widths.
n
n
n
nIn myncase Ultimate output should be like this HTML
n
n//<divnclass=\”ms-rtestate-read ms-rte-wpbox\”ncontentEditable=\”false\”>
n
n// <table id=\”layouts table\”nstyle=\”width:100%\”>
n
n// <tr style=\”vertical-align:ntop;\”>
n
n// <td style=\”width=66.6%\”>
n
n// <divnclass=\”ms-rtestate-read {0}\”nid=\”div_{0}\”></div>
n
n// </td>
n
n// <tdnstyle=\”width=33.3%\”>
n
n
n// </td>
n
n// </tr>
n
n// </table>
n
n//</div>
n
n
n
nInnexample above {0} and {1} would be replaced by GUIDs of web parts.
n
nI haventried this with console application for POC but one can add this in featurenreceiver.
n
n
n
n
n
n
n
nusing (SPSite site = newSPSite(“http://yoursite/”))
n
n{
n
nusing (SPWeb _web = site.OpenWeb(“/yourteamsite”))
n
n {
n
nSPListn_list1 = _web.Lists.TryGetList(“List1”);
n
nSPListn_list2 = _web.Lists.TryGetList(“List2”);
n
nSPListn_sitePages = _web.Lists.TryGetList(“SitenPages”);
n
n
n
nSPFilen_file = _web.GetFile(_web.Url + “/SitePages/Home.aspx”);
n
n
n
nifn(_list1 != null&& _list2 != null)
n
n {
n
nXsltListViewWebPartn_list1View = newXsltListViewWebPart();
n
n _list1View.ListId = _list1.ID;
n
n _list1View.ViewGuid =n_list1.DefaultView.ID.ToString(“B”).ToUpper();
n
n _list1View.XmlDefinition =n_list1.DefaultView.GetViewXml();
n
n
n
nXsltListViewWebPartn_list2View = newXsltListViewWebPart();
n
n _list2View.ListId = _list2.ID;
n
n _list2View.ViewGuid =n_list2.DefaultView.ID.ToString(“B”).ToUpper();
n
n _list2View.XmlDefinition =n_list2.DefaultView.GetViewXml();
n
n
n
nifn(_file != null)
n
n {
n
nusing (SPLimitedWebPartManager _mgr =n_web.GetLimitedWebPartManager(_file.Url, System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared))
n
n {
n
nGuidnstorageKey1 = Guid.NewGuid();
n
nstringnwpId = String.Format(“g_{0}”, storageKey1.ToString().Replace(‘-‘, ‘_’));
n
n _list1View.ID = wpId;
n
n
n
nGuidnstorageKey2 = Guid.NewGuid();
n
nstringn_liWPID = String.Format(“g_{0}”, storageKey2.ToString().Replace(‘-‘, ‘_’));
n
n _list2View.ID = _liWPID;
n
n _list2View.ID = _liWPID;
n
n
n
n//DeletingnWeb Parts
n
nfor (int i = _mgr.WebParts.Count – 1; i > 0; i–)
n
n {
n
n n_mgr.DeleteWebPart(_mgr.WebParts[i]);
n
n }
n
n
n
n//AddingnWeb Part
n
n _mgr.AddWebPart(_list1View, “wpz”, 0);
n
n _mgr.AddWebPart(_list2View, “wpz”, 1);
n
n
n
n//DeletingnWiki Content
n
n _file.Item[SPBuiltInFieldId.WikiField]n= string.Empty;
n
n _file.Item.UpdateOverwriteVersion();
n
n
n
nstringnmarker = String.Format(CultureInfo.InvariantCulture, “<div class=\”ms-rtestate-readnms-rte-wpbox\” contentEditable=\”false\”><tablenid=\”layouts table\” style=\”width:100%\”><trnstyle=\”vertical-align: top;\”><td style=\”width:66.6%\”><divnclass=\”ms-rtestate-read {0}\” id=\”div_{0}\”></div></td><tdnstyle=\”width:33.3%\”><div class=\”ms-rtestate-readn{1}\”nid=\”div_{1}\”></div></td></tr></table></div>”,nstorageKey1.ToString(“D”), _nstorageKey2.ToString(“D”));
n
n
n
n
n
n_file.Item[SPBuiltInFieldId.WikiField] = marker;
n
n n_file.Item.UpdateOverwriteVersion();
n
n
n
nConsole.WriteLine(“done”);
n
n }
n
n}
n
n
n
n }
n
n
n
n }
n
n }
n
n
n
nConsole.ReadLine();
n
n }
n
n
n