When we provision sites and pages programmatically we may need to add or modify content to wiki pages as well. Although it is not a good practice to add web parts to the wiki field, some times it is the only option we have.
Following is a code sample where we can add content to a wiki page. We can use a feature receiver to do the task
- public override void FeatureActivated(SPFeatureReceiverProperties properties)
- {
- var web = properties.Feature.Parent as SPWeb;
- web.AllowUnsafeUpdates = true;
- //Add Shared Documents webpart
- var sharedDocuments = web.Lists.TryGetList("Shared Documents");
- var sharedDocWP = new XsltListViewWebPart();
- sharedDocWP.ListId = sharedDocuments.ID;
- sharedDocWP.Title = "Shared Documents";
- sharedDocWP.ChromeType = PartChromeType.TitleOnly;
- sharedDocWP.ID = Guid.NewGuid().ToString();
- var homePage = web.GetFile("SitePages/Home1.aspx");
- // Replace the current wiki page content
- GenerateNewWikiContent(homePage, sharedDocWP);
- web.Update();
- }
Following is the method to add html content and serialized web part to the page. We can use several StringBuilders to create the content and finally assign to the WikiField property.
- public static void GenerateNewWikiContent(SPFile wikiFile, WebPart webpart)
- {
- var stringBuilder = new StringBuilder("");
- var pageColumn1 = new StringBuilder("");
- var pageColumn2 = new StringBuilder("");
- stringBuilder.Append("<table id='layoutsTable' style='width: 100%'><tbody><tr style='vertical-align:top'>");
- //Content for column 1
- pageColumn1.Append("<td style='width: 66.6%'><div class='ms-rte-layoutszone-outer' style='width:100%'><div class='ms-rte-layoutszone-inner' style='min-height:60px;word-wrap:break-word'>");
- pageColumn1.Append(" <h1 class='ms-rteElement-H1B' style='margin-bottom: 0px;'><span><span>Welcome to your site!</span></span></h1>");
- pageColumn1.Append("<p>Add a new image, change this welcome text or add new lists to this page by clicking the edit button above. You can click on Shared Documents to add files or on the calendar to create new team events. Use the links in the getting started section to share your site and customize its look.</p>");
- pageColumn1.Append("<p/><br/>");
- //Deleting existing Wiki Content
- wikiFile.Item[SPBuiltInFieldId.WikiField] = string.Empty;
- wikiFile.Item.UpdateOverwriteVersion();
- using (var limitedWebPartManager = wikiFile.GetLimitedWebPartManager(PersonalizationScope.Shared))
- {
- var storageKey = Guid.NewGuid();
- var strStorageKey = "g_" + storageKey.ToString().Replace('-', '_');
- webpart.ID = strStorageKey;
- limitedWebPartManager.AddWebPart(webpart, "wpz", 0);
- var webPart = string.Format(CultureInfo.InvariantCulture,
- "<div class='ms-rtestate-read ms-rte-wpbox' contentEditable='false'><div class='ms-rtestate-read {0}' id='div_{0}'></div><div style='display:none' id='vid_{0}'/></div>",
- new object[] { storageKey.ToString("D") });
- pageColumn1.Append(webPart);
- pageColumn1.Append("</div></div></td>");
- //Content for column 2
- pageColumn2.Append(
- "<td style='width: 33.3%'><div class='ms-rte-layoutszone-outer' style='width: 100%'><div class='ms-rte-layoutszone-inner' style='min-height: 60px; word-wrap: break-word'>");
- pageColumn2.Append("<p class='ms-rteThemeForeColor-5-5' style='text-align:left;font-size:10pt;' >");
- pageColumn2.Append(
- "<img style='margin:5px;' alt='People collaborating' src='/_layouts/images/homepageSamplePhoto.jpg' />");
- pageColumn2.Append("</p>");
- pageColumn2.Append("</div></div></td>");
- stringBuilder.Append(pageColumn1.ToString());
- stringBuilder.Append(pageColumn2.ToString());
- stringBuilder.Append("</tr></tbody></table><span id='layoutsData' style='display: none'>false,false,2</span>");
- wikiFile.Item[SPBuiltInFieldId.WikiField] = stringBuilder.ToString();
- wikiFile.Item.UpdateOverwriteVersion();
- wikiFile.Item.Update();
- }
- }
Following is the page output
No comments:
Post a Comment