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

Thursday, November 14, 2013

Presentation–Logical architecture considerations for SharePoint 2013

Recently I did a presentation at Sri Lanka SharePoint forum regarding how to design the logical architecture for new and existing SharePoint 2013 environments.

1450839_10151871096182530_689902513_n

There are some best practices influenced by SharePoint online regarding the logical architecture. We discussed how web applications, sites and other components are created in SharePoint online and how we can create a similar environment.

Monday, November 4, 2013

Encrypt values of a column - SQL Server

We can use T-SQL EncryptByKey method to encrypt data in a specific column and DecryptByKey method to decrypt that. In this post I’ll explain how to use those methods.
As the first task we will create a symmetric key and a certificate as below.
USE Staff;
GO

CREATE MASTER KEY ENCRYPTION BY PASSWORD = 'Passwordabcd';
GO
CREATE CERTIFICATE Prod_Cert WITH SUBJECT = 'Key Protection';
GO

CREATE SYMMETRIC KEY My_Key WITH
    KEY_SOURCE = 'This is the symmetric key',
    ALGORITHM = AES_256, 
    IDENTITY_VALUE = 'This is the symmetric key'
    ENCRYPTION BY CERTIFICATE Prod_Cert;
GO

-- Create a column in which to store the encrypted data.
ALTER TABLE dbo.Employee
ADD EncryptedAge varbinary(128); 
GO
After that we can insert/update data to the column we just created while encrypting the data.
USE Staff;
GO

OPEN SYMMETRIC KEY My_Key
   DECRYPTION BY CERTIFICATE Prod_Cert;

UPDATE dbo.Employee
SET EncryptedAge
    = EncryptByKey(Key_GUID('My_Key'), 'my new value');
GO
Values in the column are encrypted as below.

image
Use below query to decrypt the value
OPEN SYMMETRIC KEY My_Key
   DECRYPTION BY CERTIFICATE Prod_Cert;
GO

SELECT Name, EncryptedAge 
AS 'Encrypted Age',
CONVERT(varchar, DecryptByKey(EncryptedAge)) 
AS 'Decrypted Age'
FROM dbo.Employee;

GO
You can get the decrypted value as below

image