Tuesday, November 19, 2013

Add content and web parts to wiki page programmatically

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

  1. public override void FeatureActivated(SPFeatureReceiverProperties properties)
  2. {
  3.     var web = properties.Feature.Parent as SPWeb;
  4.     web.AllowUnsafeUpdates = true;
  5.  
  6.     //Add Shared Documents webpart
  7.     var sharedDocuments = web.Lists.TryGetList("Shared Documents");
  8.     var sharedDocWP = new XsltListViewWebPart();
  9.     sharedDocWP.ListId = sharedDocuments.ID;
  10.     sharedDocWP.Title = "Shared Documents";
  11.     sharedDocWP.ChromeType = PartChromeType.TitleOnly;
  12.     sharedDocWP.ID = Guid.NewGuid().ToString();
  13.     var homePage = web.GetFile("SitePages/Home1.aspx");
  14.  
  15.     // Replace the current wiki page content
  16.     GenerateNewWikiContent(homePage, sharedDocWP);
  17.     web.Update();
  18. }

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.

  1. public static void GenerateNewWikiContent(SPFile wikiFile, WebPart webpart)
  2. {
  3.     var stringBuilder = new StringBuilder("");
  4.     var pageColumn1 = new StringBuilder("");
  5.     var pageColumn2 = new StringBuilder("");
  6.     stringBuilder.Append("<table id='layoutsTable' style='width: 100%'><tbody><tr style='vertical-align:top'>");
  7.  
  8.     //Content for column 1
  9.     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'>");
  10.     pageColumn1.Append(" <h1 class='ms-rteElement-H1B'  style='margin-bottom: 0px;'><span><span>Welcome to your site!</span></span></h1>");
  11.     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>");
  12.     pageColumn1.Append("<p/><br/>");
  13.  
  14.     //Deleting existing Wiki Content
  15.     wikiFile.Item[SPBuiltInFieldId.WikiField] = string.Empty;
  16.     wikiFile.Item.UpdateOverwriteVersion();
  17.  
  18.     using (var limitedWebPartManager = wikiFile.GetLimitedWebPartManager(PersonalizationScope.Shared))
  19.     {
  20.         var storageKey = Guid.NewGuid();
  21.         var strStorageKey = "g_" + storageKey.ToString().Replace('-', '_');
  22.  
  23.         webpart.ID = strStorageKey;
  24.         limitedWebPartManager.AddWebPart(webpart, "wpz", 0);
  25.         var webPart = string.Format(CultureInfo.InvariantCulture,
  26.                                     "<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>",
  27.                                     new object[] { storageKey.ToString("D") });
  28.         pageColumn1.Append(webPart);
  29.         pageColumn1.Append("</div></div></td>");
  30.  
  31.         //Content for column 2
  32.         pageColumn2.Append(
  33.             "<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'>");
  34.         pageColumn2.Append("<p class='ms-rteThemeForeColor-5-5' style='text-align:left;font-size:10pt;' >");
  35.         pageColumn2.Append(
  36.             "<img style='margin:5px;' alt='People collaborating' src='/_layouts/images/homepageSamplePhoto.jpg' />");
  37.         pageColumn2.Append("</p>");
  38.  
  39.         pageColumn2.Append("</div></div></td>");
  40.  
  41.         stringBuilder.Append(pageColumn1.ToString());
  42.         stringBuilder.Append(pageColumn2.ToString());
  43.         stringBuilder.Append("</tr></tbody></table><span id='layoutsData' style='display: none'>false,false,2</span>");
  44.         wikiFile.Item[SPBuiltInFieldId.WikiField] = stringBuilder.ToString();
  45.         wikiFile.Item.UpdateOverwriteVersion();
  46.         wikiFile.Item.Update();
  47.     }
  48. }

Following is the page output

image

No comments: