<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="https://www.w3.org/2005/Atom" xmlns:sy="https://purl.org/rss/1.0/modules/syndication/" xmlns:dc="https://purl.org/dc/elements/1.1/" xmlns:content="https://purl.org/rss/1.0/modules/content/" xmlns:wfw="https://wellformedweb.org/CommentAPI/" xmlns:slash="https://purl.org/rss/1.0/modules/slash/"
  version="2.0">
  <channel>
    <title>Random Musings of Jeremy Jameson</title>
    <atom:link
      href="https://docs.microsoft.com/archive/blogs/jjameson/feed.xml"
      rel="self"
      type="application/rss+xml" />
    <link>https://docs.microsoft.com/archive/blogs/jjameson/feed.xml</link>
    <description />
    <lastBuildDate>Wed, 29 Jul 2015 21:04:43 GMT</lastBuildDate>
    <language>en-US</language>
    <sy:updatePeriod>hourly</sy:updatePeriod>
    <sy:updateFrequency>1</sy:updateFrequency>
    <item>
      <title>Last Day with Microsoft</title>
      <link>https://docs.microsoft.com/archive/blogs/jjameson/last-day-with-microsoft</link>
      <pubDate>Fri, 02 Sep 2011 15:43:00 GMT</pubDate>
      <dc:creator><![CDATA[Jeremy Jameson - MSFT]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/jjameson/2011/09/02/last-day-with-microsoft/</guid>
      <description><![CDATA[From: Jeremy Jameson  Sent: Friday, September 02, 2011 3:01 PM To: [snip, snip] Subject: Last day...]]></description>
      <content:encoded><![CDATA[<p><strong>From:</strong> Jeremy Jameson <br /> <strong>Sent:</strong> Friday, September 02, 2011 3:01 PM<br /> <strong>To:</strong> [snip, snip]<br /> <strong>Subject:</strong> Last day with Microsoft</p>
<p>Today is my last day with Microsoft and it is with a heavy heart that I now say farewell to past and present teammates, coworkers, and managers.</p>
<p>The last 11 years have been an incredible chapter in my life. Together we have overcome numerous challenges, helped create amazing solutions for our customers, and enjoyed numerous &ldquo;highs&rdquo; in the world of technology (and, quite frankly, perservered through the occasional &ldquo;low&rdquo; as well).</p>
<p>I&rsquo;ve learned more in the past decade of my life than perhaps any other, and I wouldn&rsquo;t trade my decision to join Microsoft for anything (especially considering it put me on the path to meeting my wife and eventually to our daughter). In my time here, I&rsquo;ve worked with some of the smartest and most talented people I&rsquo;ve ever known.</p>
<p>I&rsquo;m not really sure what the next 11 years hold in store for me, but the mere thought that at the end of it, my daughter will be learning how to drive gives me great pause (not to mention the fact that I will then be over 50).</p>
<p>After today, you can reach me via <a href="mailto:{firstname}_{lastname}@hotmail.com">{firstname}_{lastname}@hotmail.com</a> or using my new business email (see below).</p>
<p>Sometimes when I rise out of bed much too early in the morning (such as today) and the world is quiet, I close my eyes and clearly see memories from years ago as if they just happened yesterday. This morning, I was remembering my second day at Microsoft when Ron Stutz told me to head over to J.D. Edwards and help a couple of other Microsoft consultants replace Site Server on their &ldquo;Knowledge Garden&rdquo; solution with &ldquo;Tahoe&rdquo;. [For those too young to remember, &ldquo;Tahoe&rdquo; was the code name for SharePoint Portal Server 2001.]</p>
<p>Since I am far better with technology than I am with prose, I&rsquo;ll leave you with the following to express how much I appreciate our time together and sincerely hope our paths cross again...</p>
<blockquote>
<p class="directQuote">Eventually, all things merge into one, and a river runs through it. The river was cut by the world&rsquo;s great flood and runs over rocks from the basement of time. On some of the rocks are timeless raindrops. Under the rocks are the words, and some of the words are theirs.</p>
<p class="directQuote">I am haunted by waters.</p>
<p>&ndash; Norman Maclean, A River Runs Through It</p>
</blockquote>
<p><br /><br />Jeremy Jameson<br /> [snip, snip]</p>]]></content:encoded>
    </item>
    <item>
      <title>New Blog Location - https://www.technologytoolbox.com/blog/jjameson</title>
      <link>https://docs.microsoft.com/archive/blogs/jjameson/new-blog-location-httpwww-technologytoolbox-comblogjjameson</link>
      <pubDate>Fri, 02 Sep 2011 13:59:38 GMT</pubDate>
      <dc:creator><![CDATA[Jeremy Jameson - MSFT]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/jjameson/2011/09/02/new-blog-location-httpwww-technologytoolbox-comblogjjameson/</guid>
      <description><![CDATA[As I mentioned in my previous post, today is my last day with Microsoft.
I still have a lot of work...]]></description>
      <content:encoded><![CDATA[<p>As I mentioned in my <a href="https://blogs.msdn.com/b/jjameson/archive/2011/08/22/leaving-microsoft.aspx">previous post</a>, today is my last day with Microsoft.</p>
<p>I still have a lot of work to do in setting up my new website, but at this point, I've completed enough to continue blogging the "Random Musings of Jeremy Jameson":</p>
<blockquote><a href="https://www.technologytoolbox.com/blog/jjameson">https://www.technologytoolbox.com/blog/jjameson</a></blockquote>
<p>If you subscribed to my MSDN blog in the past, then I encourage you to update your RSS feed by clicking the <strong>Subscribe</strong> link on my new blog.</p>
<p>The blog will be in constant flux over the next week or so as I work on branding, layout, etc. So, as they say, please "pardon my dust" ;-)</p>]]></content:encoded>
    </item>
    <item>
      <title>Leaving Microsoft</title>
      <link>https://docs.microsoft.com/archive/blogs/jjameson/leaving-microsoft</link>
      <pubDate>Mon, 22 Aug 2011 07:03:48 GMT</pubDate>
      <dc:creator><![CDATA[Jeremy Jameson - MSFT]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/jjameson/2011/08/22/leaving-microsoft/</guid>
      <description><![CDATA[Last Thursday, I informed my manager that I have decided to leave Microsoft to pursue other...]]></description>
      <content:encoded><![CDATA[<p>Last Thursday, I informed my manager that I have decided to leave Microsoft to pursue other opportunities.</p>
<p>My last day will be September 2nd -- just a few days shy of my 11 year anniversary date with the company (my first day was September 5, 2000).</p>
<p>You might be wondering why I would choose to forgo a steady paycheck, excellent benefits, and four weeks vacation per year -- especially in this economy. (I know my mother certainly questions my sanity at the moment. Fortunately my wife, on the other hand, sincerely understands my decision and respects it -- even though it means that we will need to "cinch up our belts" for some time while I build up my own business.)</p>
<p>It certainly wasn't an easy decision, but it has been one that I've been considering for quite some time.</p>
<p>The reality is that being a Principal Consultant with Microsoft Consulting Services is very challenging. However, if you know me at all, then you know that one of the things I enjoy most is overcoming challenges -- so my decision certainly wasn't based on "burnout" or the desire to get out of the consulting business.</p>
<p>It also wasn't that I was unable to find billable work (in other words, meet my "utilization target" -- which, by the way, was 119% for this past fiscal year). The truth is, prior to my current project (which started in late May), I billed the full rate for a Principal Consultant (i.e. $308.75/hr) at a <em>single</em> customer for 18 months straight -- and no, that wasn't commissioned through one long contract, but rather the result of numerous extensions (made possible by continually delivering a release every six to eight weeks).</p>
<p>Nor was it that I had to travel full-time to work with customers (although that is becoming the norm for many MCS consultants I know). On the contrary, I've been working from home the vast majority of the last five years (since my daughter was born).</p>
<p>So, then why am I leaving Microsoft?</p>
<p>The short answer is that I'm an entrepreneur at heart with a strong passion for leveraging software to help businesses thrive, and while I could have stayed with Microsoft Consulting Services for the foreseeable future, I believe the time is right for change.</p>
<p>If you've engaged my services through Microsoft in the past, then you know that, working together, we can achieve great things. No...not <em>great</em> things, <em>amazing</em> things -- in incredibly short periods of time. If we haven't worked together in the past, but you are looking for top-notch SharePoint/.NET/SQL Server/TFS consulting services, then let's connect so we can discuss how I can help you maximize your investment in Microsoft technology.</p>
<p>I've got a lot to do over the next couple of weeks before my last day -- such as ensuring a smooth transition on my current project, obtaining a business email address other than <a href="mailto:jjameson@microsoft.com">jjameson@microsoft.com</a>, and finding a new home for my blog (which I promise will see significantly more posts over the coming year than in years past).</p>
<p>I started researching alternative blog services this past weekend, and I'll be sure to update this blog once I know the new location for "Random Musings of Jeremy Jameson."</p>
<p>Until then, you can always reach me via my LinkedIn profile:</p>
<blockquote><a href="https://www.linkedin.com/in/jeremyjameson">https://www.linkedin.com/in/jeremyjameson</a></blockquote>]]></content:encoded>
    </item>
    <item>
      <title>Using the SharePoint API to Configure an Expiration Policy on a Document Library</title>
      <link>https://docs.microsoft.com/archive/blogs/jjameson/using-the-sharepoint-api-to-configure-an-expiration-policy-on-a-document-library</link>
      <pubDate>Thu, 05 May 2011 07:58:56 GMT</pubDate>
      <dc:creator><![CDATA[Jeremy Jameson - MSFT]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/jjameson/2011/05/05/using-the-sharepoint-api-to-configure-an-expiration-policy-on-a-document-library/</guid>
      <description><![CDATA[While it typically takes less than a minute or two to configure an expiration policy on a SharePoint...]]></description>
      <content:encoded><![CDATA[<p>While it typically takes less than a minute or two to configure an expiration policy on a SharePoint document library, there may still be reasons why you want to do this using the SharePoint object model instead.</p>
<p>For example, suppose I have a "Temporary Files" document library for which I want the files to be deleted approximately one day after they are added to the library.</p>
<p>The following C# code shows how to configure an expiration policy on the specified document library (passed as an <strong>SPList </strong>object):</p>
<div class="codeBlock">
<pre><code><span style="color: #0000ff;">using</span> Microsoft.Office.RecordsManagement.InformationPolicy;
<span style="color: #0000ff;">using</span> Microsoft.SharePoint;
...
<span style="color: #0000ff;">        private const string</span> expirationPolicyFeatureId =
            <span style="color: #a31515;">"Microsoft.Office.RecordsManagement.PolicyFeatures.Expiration"</span>;

        <span style="color: #0000ff;">private static void</span> ConfigureExpirationPolicy(
            <span style="color: #2b91af;">SPList</span> tempFilesLibrary)
        {
            <span style="color: #2b91af;">Debug</span>.Assert(tempFilesLibrary != <span style="color: #0000ff;">null</span>);

            <span style="color: #008000;">// HACK: Refresh the SPList object in order to avoid
            // the following error when calling Policy.CreatePolicy:
            //
            // "The object has been updated by another user since it was last
            // fetched."</span><span style="color: #2b91af;">
            SPList</span> tempFilesLibrary2 =
                tempFilesLibrary.ParentWeb.Lists[tempFilesLibrary.ID];
            
            <span style="color: #2b91af;">SPContentTypeId</span> documentContentTypeId =
                tempFilesLibrary2.ContentTypes.BestMatch(
                    <span style="color: #2b91af;">SPBuiltInContentTypeId</span>.Document);

            <span style="color: #2b91af;">SPContentType</span> documentContentType =
                tempFilesLibrary2.ContentTypes[documentContentTypeId];

            <span style="color: #2b91af;">Debug</span>.Assert(documentContentType != <span style="color: #0000ff;">null</span>);
            
            <span style="color: #2b91af;">Policy</span> policy = <span style="color: #2b91af;">Policy</span>.GetPolicy(documentContentType);

            <span style="color: #0000ff;">if</span> (policy == <span style="color: #0000ff;">null</span>)
            {
                <span style="color: #2b91af;">Policy</span>.CreatePolicy(documentContentType, <span style="color: #0000ff;">null</span>);
                policy = <span style="color: #2b91af;">Policy</span>.GetPolicy(documentContentType);
            }

            <span style="color: #0000ff;">if</span> (policy.Items[expirationPolicyFeatureId] == <span style="color: #0000ff;">null</span>)
            {
                <span style="color: #0000ff;">string</span> customData =
<span style="color: #a31515;">"&lt;data&gt;"</span>
    + <span style="color: #a31515;">"&lt;formula id=\"Microsoft.Office.RecordsManagement.PolicyFeatures.Expiration.Formula.BuiltIn\"&gt;"</span>
        + <span style="color: #a31515;">"&lt;number&gt;1&lt;/number&gt;"</span>
        + <span style="color: #a31515;">"&lt;property&gt;Created&lt;/property&gt;"</span>
        + <span style="color: #a31515;">"&lt;period&gt;days&lt;/period&gt;"</span>
    + <span style="color: #a31515;">"&lt;/formula&gt;"</span>
    + <span style="color: #a31515;">"&lt;action type=\"action\" id=\"Microsoft.Office.RecordsManagement.PolicyFeatures.Expiration.Action.MoveToRecycleBin\" /&gt;"</span>
+ <span style="color: #a31515;">"&lt;/data&gt;"</span>;

                policy.Items.Add(expirationPolicyFeatureId, customData);
            }
        }</code></pre>
</div>
<p>This code adds an expiration policy to the library if it hasn't yet been configured. If an expiration policy <em>has</em> been configured on the document library, then it it ignored (because the policy may have been modified by an administrator -- for example, to increase the time period used to determine which files to delete).</p>
<blockquote class="note">
<div class="noteTitle"><strong>Note</strong></div>
<div>
<p>As shown in the HACK comment above, I fetch a new <strong>SPList </strong>object in order to avoid the following error when configuring the expiration policy immediately after creating a new document library:</p>
<p class="directQuote errorMessage">The object has been updated by another user since it was last fetched.</p>
<p>This error occurred in Microsoft Office SharePoint Server (MOSS) 2007 -- although I just ran a quick test in SharePoint 2010 and it looks like this may no longer be an issue in the new version.</p>
</div>
</blockquote>
<p>In my next post, I'll explain more about storing files temporarily in SharePoint.</p>]]></content:encoded>
    </item>
    <item>
      <title>Missing thumbnail images in SharePoint?...You probably forgot to specify the "ContentType" property</title>
      <link>https://docs.microsoft.com/archive/blogs/jjameson/missing-thumbnail-images-in-sharepoint-you-probably-forgot-to-specify-the-contenttype-property</link>
      <pubDate>Mon, 02 May 2011 06:33:08 GMT</pubDate>
      <dc:creator><![CDATA[Jeremy Jameson - MSFT]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/jjameson/2011/05/02/missing-thumbnail-images-in-sharepoint-you-probably-forgot-to-specify-the-contenttype-property/</guid>
      <description><![CDATA[During the process of creating my previous post, I discovered the thumbnail images were not...]]></description>
      <content:encoded><![CDATA[<p>During the process of creating <a href="https://blogs.msdn.com/b/jjameson/archive/2011/05/02/web-standards-design-with-sharepoint-part-6.aspx">my previous post</a>, I discovered the thumbnail images were not rendering as expected for the custom images that I added to the out-of-the-box SharePoint /PublishingImages picture library (via a feature).</p>
<p>Here is the content of my original Elements.xml file (used to add the images to the library):</p>
<div class="codeBlock">
<pre><code><span style="color: #0000ff;">&lt;?</span><span style="color: #a31515;">xml</span><span style="color: #ff0000;"> version</span><span style="color: #0000ff;">=</span>"<span style="color: #0000ff;">1.0</span>"<span style="color: #ff0000;"> encoding</span><span style="color: #0000ff;">=</span>"<span style="color: #0000ff;">utf-8</span>"<span style="color: #0000ff;"> ?&gt;
&lt;</span><span style="color: #a31515;">Elements</span><span style="color: #ff0000;"> xmlns</span><span style="color: #0000ff;">=</span>"<span style="color: #0000ff;">https://schemas.microsoft.com/sharepoint/</span>"<span style="color: #0000ff;">&gt;
  &lt;</span><span style="color: #a31515;">Module</span><span style="color: #ff0000;">
    Name</span><span style="color: #0000ff;">=</span>"<span style="color: #0000ff;">HomeSiteImages</span>"
<span style="color: #ff0000;">    Url</span><span style="color: #0000ff;">=</span>"<span style="color: #0000ff;">PublishingImages</span>"
<span style="color: #ff0000;">    Path</span><span style="color: #0000ff;">=</span>"<span style="color: #0000ff;">HomeSiteConfiguration\PublishingImages</span>"<span style="color: #0000ff;">&gt;
    &lt;</span><span style="color: #a31515;">File</span><span style="color: #ff0000;"> Url</span><span style="color: #0000ff;">=</span>"<span style="color: #0000ff;">boat.jpg</span>"<span style="color: #ff0000;"> Type</span><span style="color: #0000ff;">=</span>"<span style="color: #0000ff;">GhostableInLibrary</span>"<span style="color: #0000ff;">/&gt;
    &lt;</span><span style="color: #a31515;">File</span><span style="color: #ff0000;"> Url</span><span style="color: #0000ff;">=</span>"<span style="color: #0000ff;">fame.jpg</span>"<span style="color: #ff0000;"> Type</span><span style="color: #0000ff;">=</span>"<span style="color: #0000ff;">GhostableInLibrary</span>"<span style="color: #0000ff;">/&gt;
    &lt;</span><span style="color: #a31515;">File</span><span style="color: #ff0000;"> Url</span><span style="color: #0000ff;">=</span>"<span style="color: #0000ff;">ropes.jpg</span>"<span style="color: #ff0000;"> Type</span><span style="color: #0000ff;">=</span>"<span style="color: #0000ff;">GhostableInLibrary</span>"<span style="color: #0000ff;">/&gt;
  &lt;/</span><span style="color: #a31515;">Module</span><span style="color: #0000ff;">&gt;
&lt;/</span><span style="color: #a31515;">Elements</span><span style="color: #0000ff;">&gt;</span></code></pre>
</div>
<p>To get the thumbnails to render as expected, I simply modified the Elements.xml file as follows:</p>
<div class="codeBlock">
<pre><code><span style="color: #0000ff;">&lt;?</span><span style="color: #a31515;">xml</span><span style="color: #ff0000;"> version</span><span style="color: #0000ff;">=</span>"<span style="color: #0000ff;">1.0</span>"<span style="color: #ff0000;"> encoding</span><span style="color: #0000ff;">=</span>"<span style="color: #0000ff;">utf-8</span>"<span style="color: #0000ff;"> ?&gt;
&lt;</span><span style="color: #a31515;">Elements</span><span style="color: #ff0000;"> xmlns</span><span style="color: #0000ff;">=</span>"<span style="color: #0000ff;">https://schemas.microsoft.com/sharepoint/</span>"<span style="color: #0000ff;">&gt;
  &lt;</span><span style="color: #a31515;">Module</span><span style="color: #ff0000;">
    Name</span><span style="color: #0000ff;">=</span>"<span style="color: #0000ff;">HomeSiteImages</span>"
<span style="color: #ff0000;">    Url</span><span style="color: #0000ff;">=</span>"<span style="color: #0000ff;">PublishingImages</span>"
<span style="color: #ff0000;">    Path</span><span style="color: #0000ff;">=</span>"<span style="color: #0000ff;">HomeSiteConfiguration\PublishingImages</span>"<span style="color: #0000ff;">&gt;
    &lt;</span><span style="color: #a31515;">File</span><span style="color: #ff0000;"> Url</span><span style="color: #0000ff;">=</span>"<span style="color: #0000ff;">boat.jpg</span>"<span style="color: #ff0000;"> Type</span><span style="color: #0000ff;">=</span>"<span style="color: #0000ff;">GhostableInLibrary</span>"<span style="color: #0000ff;">&gt;
      &lt;</span><span style="color: #a31515;">Property</span><span style="color: #ff0000;"> Name</span><span style="color: #0000ff;">=</span>"<span style="color: #0000ff;">ContentType</span>"
<span style="color: #ff0000;">        Value</span><span style="color: #0000ff;">=</span>"<span style="color: #0000ff;">$Resources:cmscore,contenttype_image_name;</span>"<span style="color: #0000ff;"> /&gt;
    &lt;/</span><span style="color: #a31515;">File</span><span style="color: #0000ff;">&gt;
    &lt;</span><span style="color: #a31515;">File</span><span style="color: #ff0000;"> Url</span><span style="color: #0000ff;">=</span>"<span style="color: #0000ff;">fame.jpg</span>"<span style="color: #ff0000;"> Type</span><span style="color: #0000ff;">=</span>"<span style="color: #0000ff;">GhostableInLibrary</span>"<span style="color: #0000ff;">&gt;
      &lt;</span><span style="color: #a31515;">Property</span><span style="color: #ff0000;"> Name</span><span style="color: #0000ff;">=</span>"<span style="color: #0000ff;">ContentType</span>"
<span style="color: #ff0000;">        Value</span><span style="color: #0000ff;">=</span>"<span style="color: #0000ff;">$Resources:cmscore,contenttype_image_name;</span>"<span style="color: #0000ff;"> /&gt;
    &lt;/</span><span style="color: #a31515;">File</span><span style="color: #0000ff;">&gt;
    &lt;</span><span style="color: #a31515;">File</span><span style="color: #ff0000;"> Url</span><span style="color: #0000ff;">=</span>"<span style="color: #0000ff;">ropes.jpg</span>"<span style="color: #ff0000;"> Type</span><span style="color: #0000ff;">=</span>"<span style="color: #0000ff;">GhostableInLibrary</span>"<span style="color: #0000ff;">&gt;
      &lt;</span><span style="color: #a31515;">Property</span><span style="color: #ff0000;"> Name</span><span style="color: #0000ff;">=</span>"<span style="color: #0000ff;">ContentType</span>"
<span style="color: #ff0000;">        Value</span><span style="color: #0000ff;">=</span>"<span style="color: #0000ff;">$Resources:cmscore,contenttype_image_name;</span>"<span style="color: #0000ff;"> /&gt;
    &lt;/</span><span style="color: #a31515;">File</span><span style="color: #0000ff;">&gt;
  &lt;/</span><span style="color: #a31515;">Module</span><span style="color: #0000ff;">&gt;
&lt;/</span><span style="color: #a31515;">Elements</span><span style="color: #0000ff;">&gt;</span></code></pre>
</div>
<p>By simply adding the <strong>ContentType </strong>property (and setting it to "Image"), SharePoint automatically creates the corresponding thumbnail for each image when adding the item to the picture library.</p>]]></content:encoded>
    </item>
    <item>
      <title>Web Standards Design with SharePoint, Part 6</title>
      <link>https://docs.microsoft.com/archive/blogs/jjameson/web-standards-design-with-sharepoint-part-6</link>
      <pubDate>Mon, 02 May 2011 05:28:00 GMT</pubDate>
      <dc:creator><![CDATA[Jeremy Jameson - MSFT]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/jjameson/2011/05/02/web-standards-design-with-sharepoint-part-6/</guid>
      <description><![CDATA[In part 5 of this series, I showed how you can render semantic HTML for SharePoint list items...]]></description>
      <content:encoded><![CDATA[<p>In <a href="https://blogs.msdn.com/b/jjameson/archive/2011/04/27/web-standards-design-with-sharepoint-part-5-a-k-a-rendering-semantic-html-using-the-xsltlistviewwebpart.aspx">part 5 of this series</a>, I showed how you can render semantic HTML for SharePoint list items (instead of the out-of-the-box table layout) using the new <strong><a href="https://msdn.microsoft.com/en-us/library/microsoft.sharepoint.webpartpages.xsltlistviewwebpart.aspx">XsltListViewWebPart</a> </strong>in SharePoint 2010.</p>
<blockquote class="note">
<div class="noteTitle"><strong>Note</strong></div>
<div>You can achieve similar results in Microsoft Office SharePoint Server (MOSS) 2007 using the <strong><a href="https://msdn.microsoft.com/en-us/library/microsoft.sharepoint.webpartpages.dataviewwebpart(v=office.12).aspx">DataViewWebPart</a></strong>.</div>
</blockquote>
<p>For those of you that haven't yet read my previous post, the goal was to take a SharePoint list like the one shown below...</p>
<div class="image"><img height="395" width="500" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/00/50/34/Images/Tugboat/0550.Specials-List.png" original-url="https://blogs.msdn.com/resized-image.ashx/__size/500x395/__key/communityserver-blogs-components-weblogfiles/00-00-00-50-34-Images-Tugboat/0550.Specials_2D00_List.png" class="screenshot" />
<div class="caption">Figure 1: Specials list</div>
<div class="imageLink"><a target="_blank" href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/00/50/34/Images/Tugboat/0550.Specials-List.png" original-url="https://blogs.msdn.com/cfs-filesystemfile.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-50-34-Images-Tugboat/0550.Specials_2D00_List.png">See full-sized image.</a> </div>
</div>
<p>...and subsequently render the list items as an HTML ordered list:</p>
<div class="codeBlock">
<pre><code><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">h2</span><span style="color: #0000ff;">&gt;</span>This Week's Specials<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">h2</span><span style="color: #0000ff;">&gt;
&lt;</span><span style="color: #800000;">ol</span><span style="color: #ff0000;"> class</span><span style="color: #0000ff;">='specials group'&gt;
    &lt;</span><span style="color: #800000;">li</span><span style="color: #ff0000;"> class</span><span style="color: #0000ff;">='group'&gt;
        &lt;</span><span style="color: #800000;">div</span><span style="color: #ff0000;"> class</span><span style="color: #0000ff;">='special'&gt;
            &lt;</span><span style="color: #800000;">div</span><span style="color: #ff0000;"> class</span><span style="color: #0000ff;">='special-img'&gt;
                &lt;</span><span style="color: #800000;">a</span><span style="color: #ff0000;"> href</span><span style="color: #0000ff;">='#'&gt;
                    &lt;</span><span style="color: #800000;">img</span><span style="color: #ff0000;"> src</span><span style="color: #0000ff;">='/PublishingImages/boat.jpg'</span><span style="color: #ff0000;"> alt</span><span style="color: #0000ff;">='coffee' /&gt;
                    &lt;</span><span style="color: #800000;">span</span><span style="color: #0000ff;">&gt;&lt;</span><span style="color: #800000;">strong</span><span style="color: #0000ff;">&gt;</span>Fisherman<span style="color: #ff0000;">&amp;#8217;</span>s Brew<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">strong</span><span style="color: #0000ff;">&gt;
                        &lt;</span><span style="color: #800000;">em</span><span style="color: #0000ff;">&gt;</span>$9.98 / lb.<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">em</span><span style="color: #0000ff;">&gt;&lt;/</span><span style="color: #800000;">span</span><span style="color: #0000ff;">&gt;&lt;/</span><span style="color: #800000;">a</span><span style="color: #0000ff;">&gt;
            &lt;/</span><span style="color: #800000;">div</span><span style="color: #0000ff;">&gt;
        &lt;/</span><span style="color: #800000;">div</span><span style="color: #0000ff;">&gt;
    &lt;/</span><span style="color: #800000;">li</span><span style="color: #0000ff;">&gt;
    &lt;</span><span style="color: #800000;">li</span><span style="color: #ff0000;"> class</span><span style="color: #0000ff;">='group'&gt;
        &lt;</span><span style="color: #800000;">div</span><span style="color: #ff0000;"> class</span><span style="color: #0000ff;">='special'&gt;
            &lt;</span><span style="color: #800000;">div</span><span style="color: #ff0000;"> class</span><span style="color: #0000ff;">='special-img'&gt;
                &lt;</span><span style="color: #800000;">a</span><span style="color: #ff0000;"> href</span><span style="color: #0000ff;">='#'&gt;
                    &lt;</span><span style="color: #800000;">img</span><span style="color: #ff0000;"> src</span><span style="color: #0000ff;">='/PublishingImages/ropes.jpg'</span><span style="color: #ff0000;"> alt</span><span style="color: #0000ff;">='coffee' /&gt;
                    &lt;</span><span style="color: #800000;">span</span><span style="color: #0000ff;">&gt;&lt;</span><span style="color: #800000;">strong</span><span style="color: #0000ff;">&gt;</span>Boathouse Bold<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">strong</span><span style="color: #0000ff;">&gt;
                        &lt;</span><span style="color: #800000;">em</span><span style="color: #0000ff;">&gt;</span>$12.50 / lb.<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">em</span><span style="color: #0000ff;">&gt;&lt;/</span><span style="color: #800000;">span</span><span style="color: #0000ff;">&gt;&lt;/</span><span style="color: #800000;">a</span><span style="color: #0000ff;">&gt;
            &lt;/</span><span style="color: #800000;">div</span><span style="color: #0000ff;">&gt;
        &lt;/</span><span style="color: #800000;">div</span><span style="color: #0000ff;">&gt;
    &lt;/</span><span style="color: #800000;">li</span><span style="color: #0000ff;">&gt;
    &lt;</span><span style="color: #800000;">li</span><span style="color: #ff0000;"> class</span><span style="color: #0000ff;">='group third'&gt;
        &lt;</span><span style="color: #800000;">div</span><span style="color: #ff0000;"> class</span><span style="color: #0000ff;">='special'&gt;
            &lt;</span><span style="color: #800000;">div</span><span style="color: #ff0000;"> class</span><span style="color: #0000ff;">='special-img'&gt;
                &lt;</span><span style="color: #800000;">a</span><span style="color: #ff0000;"> href</span><span style="color: #0000ff;">='#'&gt;
                    &lt;</span><span style="color: #800000;">img</span><span style="color: #ff0000;"> src</span><span style="color: #0000ff;">='/PublishingImages/fame.jpg'</span><span style="color: #ff0000;"> alt</span><span style="color: #0000ff;">='coffee' /&gt;
                    &lt;</span><span style="color: #800000;">span</span><span style="color: #0000ff;">&gt;&lt;</span><span style="color: #800000;">strong</span><span style="color: #0000ff;">&gt;</span>Deadly Decaf<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">strong</span><span style="color: #0000ff;">&gt;
                        &lt;</span><span style="color: #800000;">em</span><span style="color: #0000ff;">&gt;</span>$7.49 / lb.<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">em</span><span style="color: #0000ff;">&gt;&lt;/</span><span style="color: #800000;">span</span><span style="color: #0000ff;">&gt;&lt;/</span><span style="color: #800000;">a</span><span style="color: #0000ff;">&gt;
            &lt;/</span><span style="color: #800000;">div</span><span style="color: #0000ff;">&gt;
        &lt;/</span><span style="color: #800000;">div</span><span style="color: #0000ff;">&gt;
    &lt;/</span><span style="color: #800000;">li</span><span style="color: #0000ff;">&gt;
&lt;/</span><span style="color: #800000;">ol</span><span style="color: #0000ff;">&gt;</span></code></pre>
</div>
<p>After replacing the static HTML for the weekly specials section with an <strong>XsltListViewWebPart </strong>(and specifying the necessary XSL to transform the SharePoint list items into the desired HTML), the content on the Tugboat home page no longer needs to be updated directly. Instead, new items can be added to the custom <strong>Specials </strong>list (or existing items updated) in order to dynamically update the content on the home page.</p>
<p>However, as I mentioned at the end of my previous post, there's a problem. What happens if the number of items in the list is greater than the expected number? The following screenshot shows the updated home page content after adding a fourth item to the <strong>Specials </strong>list.</p>
<div class="image"><img height="342" width="500" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/00/50/34/Images/Tugboat/6332.Too-Many-Specials.png" original-url="https://blogs.msdn.com/resized-image.ashx/__size/500x0/__key/communityserver-blogs-components-weblogfiles/00-00-00-50-34-Images-Tugboat/6332.Too_2D00_Many_2D00_Specials.png" class="screenshot" />
<div class="caption">Figure 2: Too many items in the weekly specials section</div>
<div class="imageLink"><a target="_blank" href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/00/50/34/Images/Tugboat/6332.Too-Many-Specials.png" original-url="https://blogs.msdn.com/cfs-filesystemfile.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-50-34-Images-Tugboat/6332.Too_2D00_Many_2D00_Specials.png">See full-sized image.</a> </div>
</div>
<p>While we <em>could</em> modify the XSLT to only render the first three items from the SharePoint list, this really isn't the best implementation choice. A better way of limiting the number of list items rendered in the weekly specials section is to use a custom view on the underlying list (instead of rendering items based on the default <strong>All Items </strong>view).</p>
<p>Let's suppose that in addition to restricting the number of weekly specials displayed on the home page (to three items), we also want to show the most recent items first.</p>
<p>To achieve the two goals, start by creating a new view on the <strong>Specials </strong>list using the following parameters:</p>
<ul>
<li>Name: <strong>Most Recent Specials</strong></li>
<li>Web address of this view: <strong>MostRecent</strong></li>
<li>Columns: 
<ul>
<li><strong>Title</strong></li>
<li><strong>Unit Price</strong></li>
<li><strong>Unit of Measure</strong></li>
<li><strong>Price</strong></li>
<li><strong>Rollup Image</strong></li>
</ul>
</li>
<li>Sort 
<ul>
<li>First sort by the column: <strong>Modified</strong></li>
<li><strong>Show items in descending order</strong></li>
</ul>
</li>
<li>Item Limit 
<ul>
<li>Number of items to display: <strong>3</strong></li>
<li><strong>Limit the total number of items returned to the specified amount.</strong></li>
</ul>
</li>
</ul>
<p>Once we change the <strong>XsltListViewWebPart </strong>on the home page to use the <strong>Most Recent Specials </strong>view, the weekly specials section renders as expected regardless of the number of items in the <strong>Specials </strong>list.</p>
<div class="image"><img height="270" width="500" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/00/50/34/Images/Tugboat/2146.Specials-List-XsltListViewWebPart.png" original-url="https://blogs.msdn.com/resized-image.ashx/__size/500x270/__key/communityserver-blogs-components-weblogfiles/00-00-00-50-34-Images-Tugboat/2146.Specials_2D00_List_2D00_XsltListViewWebPart.png" class="screenshot" />
<div class="caption">Figure 3: Using the "Most Recent Specials" view (instead of "All Items") renders the home page as expected</div>
<div class="imageLink"><a target="_blank" href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/00/50/34/Images/Tugboat/2146.Specials-List-XsltListViewWebPart.png" original-url="https://blogs.msdn.com/cfs-filesystemfile.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-50-34-Images-Tugboat/2146.Specials_2D00_List_2D00_XsltListViewWebPart.png">See full-sized image.</a> </div>
</div>
<p>Now that we have the <strong>XsltListViewWebPart </strong>rendering the desired HTML, we can turn our attention to automatically creating the <strong>Specials </strong>list (as well as the custom view), populating the list with default items, and adding an instance of the <strong>XsltListViewWebPart </strong>to the home page.</p>
<h2>Creating the Custom List</h2>
<p>While you could create the <strong>Specials </strong>list via a feature using CAML, I prefer to create custom SharePoint lists using code instead. The reason is because business requirements invariably change over time and I find it much easier to start out with code since it seems almost inevitable that you'll be programmatically manipulating SharePoint lists anyway.</p>
<p>Here's the code I wrote to create automatically create and configure the new <strong>Specials </strong>list:</p>
<div class="codeBlock">
<pre><code><span style="color: #0000ff;">        private static void</span> ConfigureSpecialsList(
            <span style="color: #2b91af;">SPWeb</span> web)
        {
            <span style="color: #2b91af;">Debug</span>.Assert(web != <span style="color: #0000ff;">null</span>);

            <span style="color: #2b91af;">SPList</span> list = <span style="color: #2b91af;">SharePointListHelper</span>.EnsureList(
                web,
                <span style="color: #a31515;">"Lists/Specials"</span>,
                <span style="color: #2b91af;">SPListTemplateType</span>.GenericList,
                <span style="color: #a31515;">"Specials"</span>,
                <span style="color: #a31515;">"Items in this list appear in the \"This Week's Specials\""</span>
                    + <span style="color: #a31515;">" section of the site home page."</span>);

            <span style="color: #2b91af;">Debug</span>.Assert(list != <span style="color: #0000ff;">null</span>);

            <span style="color: #2b91af;">SPField</span> unitPriceField = <span style="color: #2b91af;">SharePointListHelper</span>.EnsureField(
                list,
                <span style="color: #a31515;">"UnitPrice"</span>,
                <span style="color: #a31515;">"Unit Price"</span>,
                <span style="color: #2b91af;">SPFieldType</span>.Currency,
                <span style="color: #0000ff;">true</span>);

            <span style="color: #2b91af;">StringCollection</span> unitOfMeasureChoices = <span style="color: #0000ff;">new</span><span style="color: #2b91af;"> StringCollection</span>();
            
            <span style="color: #0000ff;">const string</span> defaultUnitOfMeasureChoice = <span style="color: #a31515;">"/ lb."</span>;
            unitOfMeasureChoices.Add(defaultUnitOfMeasureChoice);
            
            unitOfMeasureChoices.Add(<span style="color: #a31515;">"ea."</span>);

            <span style="color: #2b91af;">SPFieldChoice</span> unitOfMeasureField =
                (<span style="color: #2b91af;">SPFieldChoice</span>) <span style="color: #2b91af;">SharePointListHelper</span>.EnsureField(
                    list,
                    <span style="color: #a31515;">"UnitOfMeasure"</span>,
                    <span style="color: #a31515;">"Unit of Measure"</span>,
                    <span style="color: #2b91af;">SPFieldType</span>.Choice,
                    <span style="color: #0000ff;">true</span>,
                    <span style="color: #0000ff;">string</span>.Empty,
                    unitOfMeasureChoices);

            unitOfMeasureField.DefaultValue = defaultUnitOfMeasureChoice;
            unitOfMeasureField.Update();

            <span style="color: #2b91af;">SPFieldCalculated</span> priceField =
                (<span style="color: #2b91af;">SPFieldCalculated</span>) <span style="color: #2b91af;">SharePointListHelper</span>.EnsureField(
                    list,
                    <span style="color: #a31515;">"Price"</span>,
                    <span style="color: #a31515;">"Price"</span>,
                    <span style="color: #2b91af;">SPFieldType</span>.Calculated);

            priceField.Formula =
                <span style="color: #a31515;">"= DOLLAR([Unit Price]) &amp; \" \" &amp; [Unit of Measure]"</span>;

            priceField.Update();

            <span style="color: #2b91af;">SPField</span> rollupImageField = web.AvailableFields[<span style="color: #2b91af;">FieldId</span>.RollupImage];
            rollupImageField.Required = <span style="color: #0000ff;">true</span>;

            <span style="color: #2b91af;">SharePointListHelper</span>.EnsureField(list, rollupImageField);

            <span style="color: #0000ff;">string</span>[] defaultViewFields = <span style="color: #0000ff;">new string</span>[] {
                    list.Fields[<span style="color: #2b91af;">SPBuiltInFieldId</span>.Title].Title,
                    unitPriceField.Title,
                    unitOfMeasureField.Title,
                    priceField.Title,
                    rollupImageField.Title
                };

            ConfigureViewsForSpecialsList(
                list,
                defaultViewFields);

            <span style="color: #0000ff;">if</span> (list.ItemCount == 0)
            {
                CreateDefaultItemsInSpecialsList(list);
            }
        }</code></pre>
</div>
<p>Note that I use a separate method to configure the default views for the new list:</p>
<div class="codeBlock">
<pre><code><span style="color: #0000ff;">        private static void</span> ConfigureViewsForSpecialsList(
            <span style="color: #2b91af;">SPList</span> list,
            <span style="color: #0000ff;">string</span>[] fields)
        {
            <span style="color: #2b91af;">Debug</span>.Assert(list != <span style="color: #0000ff;">null</span>);
            <span style="color: #2b91af;">Debug</span>.Assert(fields != <span style="color: #0000ff;">null</span>);
            <span style="color: #2b91af;">Debug</span>.Assert(fields.Length &gt; 0);

            <span style="color: #008000;">// Configure "All Items" view</span><span style="color: #2b91af;">
            SPView</span> allItems = <span style="color: #2b91af;">SharePointViewHelper</span>.EnsureView(
                list,
                <span style="color: #a31515;">"All Items"</span>,
                fields,
                <span style="color: #a31515;">"AllItems"</span>);

            <span style="color: #2b91af;">SharePointViewHelper</span>.EnsureViewFields(
                allItems,
                fields);
            
            <span style="color: #008000;">// Configure "Most Recent Specials" view</span><span style="color: #2b91af;">
            SPView</span> mostRecentSpecials = <span style="color: #2b91af;">SharePointViewHelper</span>.EnsureView(
                list,
                <span style="color: #a31515;">"Most Recent Specials"</span>,
                fields,
                <span style="color: #a31515;">"MostRecent"</span>);

            <span style="color: #2b91af;">SharePointViewHelper</span>.EnsureDefaultView(mostRecentSpecials);

            <span style="color: #2b91af;">SharePointViewHelper</span>.EnsureViewQuery(
                mostRecentSpecials,
                <span style="color: #a31515;">"&lt;OrderBy&gt;"</span>
                    + <span style="color: #a31515;">"&lt;FieldRef Name='Modified' Ascending='FALSE' /&gt;"</span>
                + <span style="color: #a31515;">"&lt;/OrderBy&gt;"</span>);

            <span style="color: #0000ff;">if</span> (mostRecentSpecials.Paged != <span style="color: #0000ff;">false</span>
                || mostRecentSpecials.RowLimit != 3)
            {
                mostRecentSpecials.Paged = <span style="color: #0000ff;">false</span>;
                mostRecentSpecials.RowLimit = 3;
                mostRecentSpecials.Update();
            }
        }</code></pre>
</div>
<h2>Adding the Default Items to the List</h2>
<p>I populate the default list items using a couple of other helper methods (but only if the list is empty upon activation of the feature -- in order to avoid adding duplicate items to the list):</p>
<div class="codeBlock">
<pre><code><span style="color: #0000ff;">        private static void CreateDefaultItemsInSpecialsList(
            <span style="color: #2b91af;">SPList</span> list)
        {
            <span style="color: #2b91af;">Debug</span>.Assert(list != null);

            CreateDefaultItemInSpecialsList(
                list,
                <span style="color: #a31515;">"Fisherman's Brew"</span>,
                9.98,
                <span style="color: #a31515;">"/ lb."</span>,
                <span style="color: #a31515;">"/PublishingImages/boat.jpg"</span>);

            CreateDefaultItemInSpecialsList(
                list,
                <span style="color: #a31515;">"Boathouse Bold"</span>,
                12.50,
                <span style="color: #a31515;">"/ lb."</span>,
                <span style="color: #a31515;">"/PublishingImages/ropes.jpg"</span>);

            CreateDefaultItemInSpecialsList(
                list,
                <span style="color: #a31515;">"Deadly Decaf"</span>,
                7.49,
                <span style="color: #a31515;">"/ lb."</span>,
                <span style="color: #a31515;">"/PublishingImages/fame.jpg"</span>);
        }</span></code></pre>
<pre>&nbsp;</pre>
<pre><code><span style="color: #0000ff;">        private static void</span> CreateDefaultItemInSpecialsList(
            <span style="color: #2b91af;">SPList</span> list,
            <span style="color: #0000ff;">string</span> title,
            <span style="color: #0000ff;">double</span> unitPrice,
            <span style="color: #0000ff;">string</span> unitOfMeasure,
            <span style="color: #0000ff;">string</span> rollupImageUrl)
        {
            <span style="color: #2b91af;">Debug</span>.Assert(list != <span style="color: #0000ff;">null</span>);
            <span style="color: #2b91af;">Debug</span>.Assert(<span style="color: #0000ff;">string</span>.IsNullOrEmpty(title) == <span style="color: #0000ff;">false</span>);
            <span style="color: #2b91af;">Debug</span>.Assert(unitPrice &gt; 0);
            <span style="color: #2b91af;">Debug</span>.Assert(<span style="color: #0000ff;">string</span>.IsNullOrEmpty(unitOfMeasure) == <span style="color: #0000ff;">false</span>);
            <span style="color: #2b91af;">Debug</span>.Assert(<span style="color: #0000ff;">string</span>.IsNullOrEmpty(rollupImageUrl) == <span style="color: #0000ff;">false</span>);

            <span style="color: #2b91af;">SPListItem</span> item = list.Items.Add();

            item[<span style="color: #2b91af;">SPBuiltInFieldId</span>.Title] = title;
            item[<span style="color: #a31515;">"Unit Price"</span>] = unitPrice;
            item[<span style="color: #a31515;">"Unit of Measure"</span>] = unitOfMeasure;

            <span style="color: #2b91af;">ImageFieldValue</span> rollupImage = <span style="color: #0000ff;">new</span><span style="color: #2b91af;"> ImageFieldValue</span>();
            rollupImage.ImageUrl = rollupImageUrl;

            item[<span style="color: #2b91af;">FieldId</span>.RollupImage] = rollupImage;

            item.Update();
        }</code></pre>
</div>
<p>There are certainly other ways to avoid adding duplicate items to a list, but this method is straightforward and sufficient for this scenario.</p>
<h2>Adding the XsltListViewWebPart to the Home Page</h2>
<p>In order to add the new <strong>XsltListViewWebPart </strong>to the home page, I modified the existing <strong>ConfigureHomeSiteDefaultPage </strong>method that I created previously:</p>
<div class="codeBlock">
<pre><code><span style="color: #0000ff;">        private static void</span> ConfigureHomeSiteDefaultPage(
            <span style="color: #2b91af;">SPWeb</span> homeWeb)
        {</code></pre>
<pre><code>            ...

            <span style="color: #008000;">// Configure Web Parts</span>
            SPWebPartPages.<span style="color: #2b91af;">SPLimitedWebPartManager</span> wpm =
                homeWeb.GetLimitedWebPartManager(
                    page.Url,
                    <span style="color: #2b91af;">PersonalizationScope</span>.Shared);

            <span style="color: #0000ff;">using</span> (wpm)
            {
                <span style="color: #0000ff;">string</span> zoneId = <span style="color: #a31515;">"TopLeftZone"</span>;
                <span style="color: #0000ff;">int</span> zoneIndex = 0;

                <span style="color: #2b91af;">SharePointWebPartHelper</span>.EnsureWebPart(
                    wpm,
                    <span style="color: #a31515;">"FindLocation"</span>,
                    <span style="color: #a31515;">"Tugboat_FindLocation.webpart"</span>,
                    zoneId,
                    <span style="color: #0000ff;">ref</span> zoneIndex);

                ConfigureSpecialsWebPart(wpm, zoneId, <span style="color: #0000ff;">ref</span> zoneIndex);

                ...
            }

            <span style="color: #2b91af;">SharePointPublishingHelper</span>.PublishPage(
                page,
                <span style="color: #a31515;">"Published by Tugboat.Web.HomeSiteConfiguration"</span>
                    + <span style="color: #a31515;">" feature."</span>);
            
        }</code></pre>
</div>
<p>The new <strong>ConfigureSpecialsWebPart </strong>method is shown below:</p>
<div class="codeBlock">
<pre><code><span style="color: #0000ff;">        private static void</span> ConfigureSpecialsWebPart(
            SPWebPartPages.<span style="color: #2b91af;">SPLimitedWebPartManager</span> wpm,
            <span style="color: #0000ff;">string</span> zoneId,
            <span style="color: #0000ff;">ref int</span> zoneIndex)
        {
            <span style="color: #2b91af;">Debug</span>.Assert(wpm != <span style="color: #0000ff;">null</span>);
            <span style="color: #2b91af;">Debug</span>.Assert(<span style="color: #0000ff;">string</span>.IsNullOrEmpty(zoneId) == <span style="color: #0000ff;">false</span>);

            <span style="color: #2b91af;">SPList</span> specialsList = wpm.Web.Lists[<span style="color: #a31515;">"Specials"</span>];
            <span style="color: #2b91af;">SPView</span> mostRecentSpecialsView =
                specialsList.Views[<span style="color: #a31515;">"Most Recent Specials"</span>];

            <span style="color: #2b91af;">WebPart</span> webPart = <span style="color: #2b91af;">SharePointWebPartHelper</span>.FindWebPartByTitle(
                wpm,
                <span style="color: #a31515;">"Specials"</span>);

            SPWebPartPages.<span style="color: #2b91af;">XsltListViewWebPart</span> specials =
                webPart <span style="color: #0000ff;">as</span> SPWebPartPages.<span style="color: #2b91af;">XsltListViewWebPart</span>;

            <span style="color: #0000ff;">if</span> (specials == <span style="color: #0000ff;">null</span>)
            {
                specials = <span style="color: #0000ff;">new</span> SPWebPartPages.<span style="color: #2b91af;">XsltListViewWebPart</span>();
                specials.Title = <span style="color: #a31515;">"Specials"</span>;

                specials.ListId = specialsList.ID;

                wpm.AddWebPart(specials, zoneId, zoneIndex);
                zoneIndex++;
            }
            <span style="color: #0000ff;">else</span>
            {
                specials.ListId = specialsList.ID;
            }

            specials.ChromeType = <span style="color: #2b91af;">PartChromeType</span>.None;
            specials.ViewGuid = mostRecentSpecialsView.ID.ToString();

            <span style="color: #2b91af;">Assembly</span> thisAssembly = <span style="color: #2b91af;">Assembly</span>.GetExecutingAssembly();

            specials.Xsl = <span style="color: #2b91af;">AssemblyHelper</span>.GetEmbeddedResourceAsString(
                thisAssembly,
                <span style="color: #a31515;">"Tugboat.Web.HomeSiteConfiguration.Xsl.Specials.xslt"</span>);

            wpm.SaveChanges(specials);

            specials.Dispose();
        }</code></pre>
</div>
<h2>Deploying the Sample Solution</h2>
<p>I've attached an updated version of the Tugboat solution in case you are interested in seeing the changes to render the weekly specials from a SharePoint list.</p>
<p>Here are the instructions to deploy the Tugboat sample to your own SharePoint environment. First, download the attachment and unzip the files. Then you simply need to create a few domain users and run a handful of PowerShell scripts, as described below.</p>
<h4>To deploy the Tugboat solution to SharePoint:</h4>
<ol>
<li>Create three service accounts for the Tugboat site: 
<ul>
<li><strong>{DOMAIN}\svc-web-tugboat-dev</strong> - used as the application pool identity for the new Tugboat site</li>
<li><strong>{DOMAIN}\svc-sp-psr-dev</strong> - object cache user account providing Full Read access to Web applications (<a href="https://technet.microsoft.com/en-us/library/ff758656.aspx">https://technet.microsoft.com/en-us/library/ff758656.aspx</a>)</li>
<li><strong>{DOMAIN}\svc-sp-psu-dev</strong> - object cache user account providing Full Control access to Web applications</li>
</ul>
</li>
<li>On the <strong>Start</strong> menu, click <strong>All Programs</strong>, click <strong>Microsoft SharePoint 2010 Products</strong>, right-click <strong>SharePoint 2010 Management Shell</strong>, and then click <strong>Run as administrator</strong>. If prompted by User Account Control to allow the program to make changes to the computer, click <strong>Yes</strong>.</li>
<li>From the Windows PowerShell command prompt, change to the directory containing the deployment scripts (e.g. C:\NotBackedUp\Tugboat\Main\Source\DeploymentFiles\Scripts), and run the following commands:<br />
<div class="consoleBlock">$env:TUGBOAT_URL = "<a href="https://tugboatcoffee-local">https://tugboatcoffee-local</a>"<br />$env:TUGBOAT_BUILD_CONFIGURATION = "Debug"<br />&amp; '.\Rebuild Web Application.ps1'</div>
</li>
</ol>
<blockquote class="note">
<div class="noteTitle"><strong>Note</strong></div>
<div>Technically, you don't have to set the environment variables (and use the "-dev" accounts). However, I recommend this in order to bypass SharePoint timer jobs when deploying the WSPs.</div>
</blockquote>
<p>At this point you should be able to modify your hosts file accordingly and browse to either <a href="https://www-local.tugboatcoffee.com">https://www-local.tugboatcoffee.com</a> (to view the site as an anonymous user) or <a href="https://tugboatcoffee-local">https://tugboatcoffee-local</a> (to view the site as an administrator).</p>
<h2>Additional Resources</h2>
<p>Here are some additional resources that you may find helpful for rendering SharePoint list items using the <strong>XsltListViewWebPart</strong>.</p>
<div class="reference"><cite>XsltListViewWebPart and Custom List Views</cite>
<div class="referenceLink"><a href="https://msdn.microsoft.com/en-us/library/ff806162.aspx">https://msdn.microsoft.com/en-us/library/ff806162.aspx</a></div>
</div>
<div class="reference"><cite>Overview of XSLT List View Rendering System</cite>
<div class="referenceLink"><a href="https://msdn.microsoft.com/en-us/library/ff604024.aspx">https://msdn.microsoft.com/en-us/library/ff604024.aspx</a></div>
</div>
<div class="reference"><cite>Examples of Input and Result Node Trees in XSLT Transformations</cite>
<div class="referenceLink"><a href="https://msdn.microsoft.com/en-us/library/ff602042.aspx">https://msdn.microsoft.com/en-us/library/ff602042.aspx</a></div>
</div>
<div class="reference"><cite>How to: Customize the Rendering of a Field on a List View</cite>
<div class="referenceLink"><a href="https://msdn.microsoft.com/en-us/library/ff606773.aspx">https://msdn.microsoft.com/en-us/library/ff606773.aspx</a></div>
</div>
<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Components.PostAttachments/00/10/15/99/81/Tugboat%20-%201.0.58.0.zip" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-components-postattachments/00-10-15-99-81/Tugboat-_2D00_-1.0.58.0.zip">Tugboat - 1.0.58.0.zip</a></p>]]></content:encoded>
    </item>
    <item>
      <title>Web Standards Design with SharePoint, Part 5 (a.k.a. Rendering Semantic HTML Using the XsltListViewWebPart)</title>
      <link>https://docs.microsoft.com/archive/blogs/jjameson/web-standards-design-with-sharepoint-part-5-a-k-a-rendering-semantic-html-using-the-xsltlistviewwebpart</link>
      <pubDate>Wed, 27 Apr 2011 08:33:00 GMT</pubDate>
      <dc:creator><![CDATA[Jeremy Jameson - MSFT]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/jjameson/2011/04/27/web-standards-design-with-sharepoint-part-5-a-k-a-rendering-semantic-html-using-the-xsltlistviewwebpart/</guid>
      <description><![CDATA[In part 4 of this series, I provided a sample SharePoint Server 2010 solution solution based on Dan...]]></description>
      <content:encoded><![CDATA[<p>In <a href="https://blogs.msdn.com/b/jjameson/archive/2011/04/02/web-standards-design-with-sharepoint-part-4.aspx">part 4 of this series</a>, I provided a sample SharePoint Server 2010 solution solution based on Dan Cederholm's <a href="https://www.handcraftedcss.com/">Tugboat sample site</a> (from his most recent book, <a href="https://amzn.com/0321643380">Handcrafted CSS : More Bulletproof Web Design</a>).</p>
<p>In that sample, however, all of the content on the home page of the site is rendered as static HTML. In other words, it doesn't truly leverage the power of SharePoint in order to make the site dynamic based on content stored throughout the site.</p>
<p>In this post, I'll show you how to take a small part of the home page (specifically <strong>This Week's Specials</strong>) and render it from items specified in a SharePoint list.</p>
<p>In case you aren't yet familiar with the Tugboat sample site, the following screenshot shows the site home page.</p>
<div class="image"><img height="500" width="246" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/00/50/34/Images/Tugboat/0361.Tugboat-Home.png" original-url="https://blogs.msdn.com/resized-image.ashx/__size/246x500/__key/CommunityServer-Blogs-Components-WeblogFiles/00-00-00-50-34-Images-Tugboat/0361.Tugboat_2D00_Home.png" />
<div class="caption">Figure 1: Tugboat - Home page</div>
<div class="imageLink"><a target="_blank" href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/00/50/34/Images/Tugboat/0361.Tugboat-Home.png" original-url="https://blogs.msdn.com/cfs-filesystemfile.ashx/__key/CommunityServer-Blogs-Components-WeblogFiles/00-00-00-50-34-Images-Tugboat/0361.Tugboat_2D00_Home.png">See full-sized image.</a> </div>
</div>
<p>When I put on my "SharePoint Architect" hat and look at the content in the <strong>This Week's Specials </strong>section, I see a SharePoint list with columns like <strong>Title</strong> (e.g. "Fisherman's Brew"), <strong>Price</strong> (e.g. "$9.98 / lb."), and <strong>Rollup Image</strong> (e.g. "/PublishingImages/boat.jpg").</p>
<p>Depending on the size of the fictitious Tugboat Coffee company, we might choose to use the Business Connectivity Services in SharePoint 2010 to pull this data from some external system. However, for the sake of this sample, let's assume that Tugboat isn't anywhere near being considered a threat to Starbucks, and we want to start out by managing the "weekly specials" using a simple SharePoint list.</p>
<p>Let's start by designing the custom SharePoint list used to specify the data for the weekly specials.</p>
<p>My decision to use the <strong>Title </strong>and <strong>Rollup Image </strong>columns should be relatively straightforward. When you create a custom list in SharePoint, you get a <strong>Title </strong>column by default (so it just makes sense to use that for the name of the product). Similarly, the <strong>Publishing Image </strong>column type (used for the <strong>Rollup Image </strong>column) provides the best user experience for selecting an image from the SharePoint site (as opposed to creating a custom column using the <strong>Hyperlink or Picture </strong>column type).</p>
<p>However, we need to think a little bit about the <strong>Price </strong>column. We could certainly choose to add a custom column using the <strong>Single line of text </strong>column type, which would contain values like "$9.98 / lb." However, besides putting an unnecessary burden on the people maintaining the data in the list -- because they would need to explicitly type in the currency symbol and unit of measure when adding new items to the list -- using a <strong>Single line of text </strong>column type for the price wouldn't really be leveraging the full power of SharePoint. It would also allow for mistakes like:</p>
<ul>
<li>"9.98 / lb." (no currency symbol)</li>
<li>"$9.98" (missing " / lb." from the price)</li>
<li>"$9.98/lb." (no spaces)</li>
<li>"$9.98 /lb." (inconsistent spacing)</li>
<li>"$9.98 / lb" (missing period on the abbreviation)</li>
<li>"$9.98 / bl." (yikes, what is a "bl."?!)</li>
</ul>
<p>Therefore, let's instead break the price into two parts: <strong>Unit Price </strong>and <strong>Unit of Measure</strong>. We can then use a calculated column for <strong>Price (</strong>in order to concatenate the values specified in the other two columns).</p>
<table cellspacing="0" class="accent1">
<caption>Data for "This Week's Specials"</caption><thead>
<tr>
<th>Title</th><th>Unit Price</th><th>Unit of Measure</th><th>Price</th><th>Rollup Image</th>
</tr>
</thead>
<tbody>
<tr>
<td>Fisherman's Brew</td>
<td>$9.98</td>
<td>/ lb.</td>
<td>$9.98 / lb.</td>
<td>/PublishingImages/boat.jpg</td>
</tr>
<tr>
<td>Boathouse Bold</td>
<td>$12.50</td>
<td>/ lb.</td>
<td>$12.50 / lb.</td>
<td>/PublishingImages/ropes.jpg</td>
</tr>
<tr>
<td>Deadly Decaf</td>
<td>$7.49</td>
<td>/ lb.</td>
<td>$7.49 / lb.</td>
<td>/PublishingImages/fame.jpg</td>
</tr>
</tbody>
</table>
<p>Here's a breakdown of the custom SharePoint list:</p>
<ul>
<li>Name: <strong>Specials</strong></li>
<li>Description:<strong> Items in this list appear in the "This Week's Specials" section of the site home page.</strong></li>
<li>Columns:
<ul>
<li><strong>Title</strong> (Single line of text, Required)</li>
<li><strong>Unit Price </strong>(Currency, Required)</li>
<li><strong>Unit of Measure </strong>(Choice, Required)
<ul>
<li>Choices:
<ul>
<li><strong>/ lb.</strong> (default value)</li>
<li><strong>ea.</strong></li>
</ul>
</li>
</ul>
</li>
<li><strong>Price </strong>(Calculated)
<ul>
<li>Formula:
<ul>
<li><strong>=DOLLAR([Unit Price]) &amp; " " &amp;[Unit of Measure]</strong></li>
</ul>
</li>
</ul>
</li>
<li><strong>Rollup Image </strong>(Publishing Image, Required)</li>
<li><strong>Created By </strong>(Person or Group)</li>
<li><strong>Modified By </strong>(Person or Group)</li>
</ul>
</li>
</ul>
<blockquote class="note important">
<div class="noteTitle"><strong>Important</strong></div>
<div>Even though the <strong>Unit Price </strong>column is defined using the <strong>Currency </strong>type and specifies a <strong>Currency format </strong>of <strong>$123,456.00 (United States)</strong>, you must specify the <strong>DOLLAR </strong>function in the formula for the calculated column. Otherwise, the values in the <strong>Price</strong> column would not appear as expected (e.g. "12.5 / lb.").</div>
</blockquote>
<blockquote class="note">
<div class="noteTitle"><strong>Note</strong></div>
<div>I chose to name the list "Specials" instead of "Weekly Specials" for a reason. If this were a real world solution, then there would be a significant chance the business rules would change over time (for example to show "Today's Specials" or "This Month's Specials"). By simply naming the list "Specials", we can avoid tightly coupling the implementation to the current business rules.</div>
</blockquote>
<p>Assuming you are proficient in SharePoint, within a matter of a few minutes you should be able to create the list and populate the sample data shown in the following screenshot.</p>
<div class="image"><img height="395" width="500" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/00/50/34/Images/Tugboat/0550.Specials-List.png" original-url="https://blogs.msdn.com/resized-image.ashx/__size/500x395/__key/communityserver-blogs-components-weblogfiles/00-00-00-50-34-Images-Tugboat/0550.Specials_2D00_List.png" class="screenshot" />
<div class="caption">Figure 2: Specials list</div>
<div class="imageLink"><a target="_blank" href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/00/50/34/Images/Tugboat/0550.Specials-List.png" original-url="https://blogs.msdn.com/cfs-filesystemfile.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-50-34-Images-Tugboat/0550.Specials_2D00_List.png">See full-sized image.</a> </div>
</div>
<p>The next step is to render the items in the list using the semantic markup previously specified using static HTML. In other words, instead of rendering the SharePoint list items in a table, we need to render them using an ordered list (along with the corresponding CSS class names):</p>
<div class="codeBlock">
<pre><code><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">h2</span><span style="color: #0000ff;">&gt;</span>This Week's Specials<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">h2</span><span style="color: #0000ff;">&gt;
&lt;</span><span style="color: #800000;">ol</span><span style="color: #ff0000;"> class</span><span style="color: #0000ff;">='specials group'&gt;
    &lt;</span><span style="color: #800000;">li</span><span style="color: #ff0000;"> class</span><span style="color: #0000ff;">='group'&gt;
        &lt;</span><span style="color: #800000;">div</span><span style="color: #ff0000;"> class</span><span style="color: #0000ff;">='special'&gt;
            &lt;</span><span style="color: #800000;">div</span><span style="color: #ff0000;"> class</span><span style="color: #0000ff;">='special-img'&gt;
                &lt;</span><span style="color: #800000;">a</span><span style="color: #ff0000;"> href</span><span style="color: #0000ff;">='#'&gt;
                    &lt;</span><span style="color: #800000;">img</span><span style="color: #ff0000;"> src</span><span style="color: #0000ff;">='/PublishingImages/boat.jpg'</span><span style="color: #ff0000;"> alt</span><span style="color: #0000ff;">='coffee' /&gt;
                    &lt;</span><span style="color: #800000;">span</span><span style="color: #0000ff;">&gt;&lt;</span><span style="color: #800000;">strong</span><span style="color: #0000ff;">&gt;</span>Fisherman<span style="color: #ff0000;">&amp;#8217;</span>s Brew<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">strong</span><span style="color: #0000ff;">&gt;
                        &lt;</span><span style="color: #800000;">em</span><span style="color: #0000ff;">&gt;</span>$9.98 / lb.<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">em</span><span style="color: #0000ff;">&gt;&lt;/</span><span style="color: #800000;">span</span><span style="color: #0000ff;">&gt;&lt;/</span><span style="color: #800000;">a</span><span style="color: #0000ff;">&gt;
            &lt;/</span><span style="color: #800000;">div</span><span style="color: #0000ff;">&gt;
        &lt;/</span><span style="color: #800000;">div</span><span style="color: #0000ff;">&gt;
    &lt;/</span><span style="color: #800000;">li</span><span style="color: #0000ff;">&gt;
    &lt;</span><span style="color: #800000;">li</span><span style="color: #ff0000;"> class</span><span style="color: #0000ff;">='group'&gt;
        &lt;</span><span style="color: #800000;">div</span><span style="color: #ff0000;"> class</span><span style="color: #0000ff;">='special'&gt;
            &lt;</span><span style="color: #800000;">div</span><span style="color: #ff0000;"> class</span><span style="color: #0000ff;">='special-img'&gt;
                &lt;</span><span style="color: #800000;">a</span><span style="color: #ff0000;"> href</span><span style="color: #0000ff;">='#'&gt;
                    &lt;</span><span style="color: #800000;">img</span><span style="color: #ff0000;"> src</span><span style="color: #0000ff;">='/PublishingImages/ropes.jpg'</span><span style="color: #ff0000;"> alt</span><span style="color: #0000ff;">='coffee' /&gt;
                    &lt;</span><span style="color: #800000;">span</span><span style="color: #0000ff;">&gt;&lt;</span><span style="color: #800000;">strong</span><span style="color: #0000ff;">&gt;</span>Boathouse Bold<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">strong</span><span style="color: #0000ff;">&gt;
                        &lt;</span><span style="color: #800000;">em</span><span style="color: #0000ff;">&gt;</span>$12.50 / lb.<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">em</span><span style="color: #0000ff;">&gt;&lt;/</span><span style="color: #800000;">span</span><span style="color: #0000ff;">&gt;&lt;/</span><span style="color: #800000;">a</span><span style="color: #0000ff;">&gt;
            &lt;/</span><span style="color: #800000;">div</span><span style="color: #0000ff;">&gt;
        &lt;/</span><span style="color: #800000;">div</span><span style="color: #0000ff;">&gt;
    &lt;/</span><span style="color: #800000;">li</span><span style="color: #0000ff;">&gt;
    &lt;</span><span style="color: #800000;">li</span><span style="color: #ff0000;"> class</span><span style="color: #0000ff;">='group third'&gt;
        &lt;</span><span style="color: #800000;">div</span><span style="color: #ff0000;"> class</span><span style="color: #0000ff;">='special'&gt;
            &lt;</span><span style="color: #800000;">div</span><span style="color: #ff0000;"> class</span><span style="color: #0000ff;">='special-img'&gt;
                &lt;</span><span style="color: #800000;">a</span><span style="color: #ff0000;"> href</span><span style="color: #0000ff;">='#'&gt;
                    &lt;</span><span style="color: #800000;">img</span><span style="color: #ff0000;"> src</span><span style="color: #0000ff;">='/PublishingImages/fame.jpg'</span><span style="color: #ff0000;"> alt</span><span style="color: #0000ff;">='coffee' /&gt;
                    &lt;</span><span style="color: #800000;">span</span><span style="color: #0000ff;">&gt;&lt;</span><span style="color: #800000;">strong</span><span style="color: #0000ff;">&gt;</span>Deadly Decaf<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">strong</span><span style="color: #0000ff;">&gt;
                        &lt;</span><span style="color: #800000;">em</span><span style="color: #0000ff;">&gt;</span>$7.49 / lb.<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">em</span><span style="color: #0000ff;">&gt;&lt;/</span><span style="color: #800000;">span</span><span style="color: #0000ff;">&gt;&lt;/</span><span style="color: #800000;">a</span><span style="color: #0000ff;">&gt;
            &lt;/</span><span style="color: #800000;">div</span><span style="color: #0000ff;">&gt;
        &lt;/</span><span style="color: #800000;">div</span><span style="color: #0000ff;">&gt;
    &lt;/</span><span style="color: #800000;">li</span><span style="color: #0000ff;">&gt;
&lt;/</span><span style="color: #800000;">ol</span><span style="color: #0000ff;">&gt;</span></code></pre>
</div>
<p>Start SharePoint Designer 2010 and open the site containing the <strong>Specials</strong> list. In the <strong>Site Objects </strong>pane, click <strong>Site Pages.</strong> In the ribbon, click <strong>Web Part Page </strong>and then select the first page layout (i.e. the layout with exactly one Web Part zone) to create a new page (<strong>Untitled_1.aspx</strong>).</p>
<p>Click the new page to view the settings for the page. In the <strong>Customization </strong>section, click <strong>Edit file</strong>.</p>
<p>In the page editor, in the <strong>Insert </strong>tab on the ribbon, click <strong>Data View </strong>and then click <strong>Specials </strong>to insert a new Web Part. Select the List View Web Part, click the <strong>Design </strong>tab on the ribbon, click <strong>Customize XSLT</strong>, and then click <strong>Customize Entire View</strong>.</p>
<p>Switch to the <strong>Code </strong>view for the page and locate the <code><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">WebPartPages</span><span style="color: #0000ff;">:</span><span style="color: #800000;">XsltListViewWebPart</span><span style="color: #0000ff;">&gt;</span></code> element. Right-click the <code><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">xsl</span><span style="color: #0000ff;">:</span><span style="color: #800000;">stylehsheet</span><span style="color: #0000ff;">&gt;</span></code> element within the <code><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">xsl</span><span style="color: #0000ff;">&gt;</span></code> element for the Web Part, click <strong>Select Tag</strong>, and then delete the default XSL stylesheet.</p>
<p>Note that the <strong>XsltListViewWebPart </strong>provides a <code><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">dsQueryResponse</span><span style="color: #0000ff;">&gt;</span></code> XML document containing the items from underlying the SharePoint list. Each list item is represented as a row:</p>
<div class="codeBlock">
<pre><code><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">dsQueryResponse</span><span style="color: #ff0000;"> ViewStyleID</span><span style="color: #0000ff;">=""</span><span style="color: #ff0000;"> BaseViewID</span><span style="color: #0000ff;">="1"</span><span style="color: #ff0000;"> TemplateType</span><span style="color: #0000ff;">="100"</span><span style="color: #ff0000;"> RowLimit</span><span style="color: #0000ff;">="30"&gt;
  &lt;</span><span style="color: #800000;">Rows</span><span style="color: #0000ff;">&gt;
    &lt;</span><span style="color: #800000;">Row</span><span style="color: #ff0000;"> ID</span><span style="color: #0000ff;">="1"
     </span><span style="color: #ff0000;"> PermMask</span><span style="color: #0000ff;">="0x7fffffffffffffff"</span><span style="color: #ff0000;">
      Title</span><span style="color: #0000ff;">="Fisherman&amp;amp;#39;s Brew"</span><span style="color: #ff0000;">
      UnitPrice</span><span style="color: #0000ff;">="$9.98"</span><span style="color: #ff0000;"> UnitPrice.</span><span style="color: #0000ff;">="9.98000000000000"</span><span style="color: #ff0000;">
      UnitOfMeasure</span><span style="color: #0000ff;">="/ lb."</span><span style="color: #ff0000;">
      Price</span><span style="color: #0000ff;">="9.98 / lb."</span><span style="color: #ff0000;">
      PublishingRollupImage</span><span style="color: #0000ff;">="&amp;lt;img alt=&amp;quot;&amp;quot; src=&amp;quot;/PublishingImages/boat.jpg&amp;quot; style=&amp;quot;border:px solid&amp;quot; /&amp;gt;" /&gt;
    &lt;</span><span style="color: #800000;">Row</span><span style="color: #ff0000;"> ID</span><span style="color: #0000ff;">="2"</span><span style="color: #ff0000;">
      PermMask</span><span style="color: #0000ff;">="0x7fffffffffffffff"</span><span style="color: #ff0000;">
      Title</span><span style="color: #0000ff;">="Boathouse Bold"</span><span style="color: #ff0000;">
      UnitPrice</span><span style="color: #0000ff;">="$12.50"</span>
      ... <span style="color: #0000ff;">/&gt;
    &lt;</span><span style="color: #800000;">Row</span><span style="color: #ff0000;"> ID</span><span style="color: #0000ff;">="3"</span><span style="color: #ff0000;">
      PermMask</span><span style="color: #0000ff;">="0x7fffffffffffffff"</span><span style="color: #ff0000;">
      Title</span><span style="color: #0000ff;">="Deadly Decaf"</span>
      ... <span style="color: #0000ff;">/&gt;
  &lt;/</span><span style="color: #800000;">Rows</span><span style="color: #0000ff;">&gt;
&lt;/</span><span style="color: #800000;">dsQueryResponse</span><span style="color: #0000ff;">&gt;</span></code></pre>
</div>
<blockquote class="note">
<div class="noteTitle"><strong>Tip</strong></div>
<div>To view the "raw" XML document, insert the <a href="https://blogs.msdn.com/b/jjameson/archive/2009/03/25/xslt-identity-transform.aspx">XSLT identity transform</a> into the <strong>&lt;xsl&gt;</strong> element of the <strong>XsltListViewWebPart</strong>, save the page, and then browse to the page in Internet Explorer.</div>
</blockquote>
<p>At this point, I recommend using Visual Studio to create two files (e.g. Specials.xml and Specials.xslt) for development purposes. Paste the data that you want to transform into the XML file (i.e. the <code><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">dsQueryResponse</span><span style="color: #0000ff;">&gt;</span></code> document). Then iterate on the XSLT file until you get the final HTML that you are looking for.</p>
<p>Since we know that we want to generate HTML from the XML document, start by specifying the corresponding output element and a template that simply emits a heading:</p>
<div class="codeBlock">
<pre><code><span style="color: #0000ff;">&lt;?</span><span style="color: #a31515;">xml</span><span style="color: #ff0000;"> version</span><span style="color: #0000ff;">=</span>"<span style="color: #0000ff;">1.0</span>"<span style="color: #ff0000;"> encoding</span><span style="color: #0000ff;">=</span>"<span style="color: #0000ff;">utf-8</span>"<span style="color: #0000ff;">?&gt;
&lt;</span><span style="color: #2b91af;">xsl:stylesheet</span><span style="color: #ff0000;"> version</span><span style="color: #0000ff;">=</span>"<span style="color: #0000ff;">1.0</span>"
<span style="color: #ff0000;">  xmlns:xsl</span><span style="color: #0000ff;">=</span>"<span style="color: #0000ff;">https://www.w3.org/1999/XSL/Transform</span>"
<span style="color: #ff0000;">  xmlns:msxsl</span><span style="color: #0000ff;">=</span>"<span style="color: #0000ff;">urn:schemas-microsoft-com:xslt</span>"
<span style="color: #ff0000;">  exclude-result-prefixes</span><span style="color: #0000ff;">=</span>"<span style="color: #0000ff;">msxsl</span>"<span style="color: #0000ff;">&gt;

  &lt;</span><span style="color: #2b91af;">xsl:output</span><span style="color: #ff0000;"> method</span><span style="color: #0000ff;">=</span>'<span style="color: #0000ff;">html</span>'<span style="color: #ff0000;"> indent</span><span style="color: #0000ff;">=</span>'<span style="color: #0000ff;">yes</span>'<span style="color: #0000ff;">/&gt;

  &lt;</span><span style="color: #2b91af;">xsl:template</span><span style="color: #ff0000;"> match</span><span style="color: #0000ff;">=</span>'<span style="color: #0000ff;">dsQueryResponse</span>'<span style="color: #0000ff;">&gt;
    &lt;</span><span style="color: #a31515;">h2</span><span style="color: #0000ff;">&gt;</span>This Week's Specials<span style="color: #0000ff;">&lt;/</span><span style="color: #a31515;">h2</span><span style="color: #0000ff;">&gt;
  &lt;/</span><span style="color: #2b91af;">xsl:template</span><span style="color: #0000ff;">&gt;
&lt;/</span><span style="color: #2b91af;">xsl:stylesheet</span><span style="color: #0000ff;">&gt;</span></code></pre>
</div>
<p>Next, enhance the <strong>dsQueryResponse</strong> template to output an ordered list. Add a second XSL template to render a list item element containing the value of the <strong>Title </strong>field for each "row" of data (in other words, for each SharePoint list item):</p>
<div class="codeBlock">
<pre><code><span style="color: #0000ff;">&lt;?</span><span style="color: #a31515;">xml</span><span style="color: #ff0000;"> version</span><span style="color: #0000ff;">=</span>"<span style="color: #0000ff;">1.0</span>"<span style="color: #ff0000;"> encoding</span><span style="color: #0000ff;">=</span>"<span style="color: #0000ff;">utf-8</span>"<span style="color: #0000ff;">?&gt;
&lt;</span><span style="color: #2b91af;">xsl:stylesheet</span><span style="color: #ff0000;"> version</span><span style="color: #0000ff;">=</span>"<span style="color: #0000ff;">1.0</span>"
<span style="color: #ff0000;">  xmlns:xsl</span><span style="color: #0000ff;">=</span>"<span style="color: #0000ff;">https://www.w3.org/1999/XSL/Transform</span>"
<span style="color: #ff0000;">  xmlns:msxsl</span><span style="color: #0000ff;">=</span>"<span style="color: #0000ff;">urn:schemas-microsoft-com:xslt</span>"
<span style="color: #ff0000;">  exclude-result-prefixes</span><span style="color: #0000ff;">=</span>"<span style="color: #0000ff;">msxsl</span>"<span style="color: #0000ff;">&gt;

  &lt;</span><span style="color: #2b91af;">xsl:output</span><span style="color: #ff0000;"> method</span><span style="color: #0000ff;">=</span>'<span style="color: #0000ff;">html</span>'<span style="color: #ff0000;"> indent</span><span style="color: #0000ff;">=</span>'<span style="color: #0000ff;">yes</span>'<span style="color: #0000ff;">/&gt;

  &lt;</span><span style="color: #2b91af;">xsl:template</span><span style="color: #ff0000;"> match</span><span style="color: #0000ff;">=</span>'<span style="color: #0000ff;">dsQueryResponse</span>'<span style="color: #0000ff;">&gt;
    &lt;</span><span style="color: #a31515;">h2</span><span style="color: #0000ff;">&gt;</span>This Week's Specials<span style="color: #0000ff;">&lt;/</span><span style="color: #a31515;">h2</span><span style="color: #0000ff;">&gt;
    &lt;</span><span style="color: #a31515;">ol</span><span style="color: #ff0000;"> class</span><span style="color: #0000ff;">=</span>'<span style="color: #0000ff;">specials group</span>'<span style="color: #0000ff;">&gt;
      &lt;</span><span style="color: #2b91af;">xsl:apply-templates</span><span style="color: #ff0000;"> select</span><span style="color: #0000ff;">=</span>'<span style="color: #0000ff;">Rows/Row</span>'<span style="color: #0000ff;">/&gt;
    &lt;/</span><span style="color: #a31515;">ol</span><span style="color: #0000ff;">&gt;
  &lt;/</span><span style="color: #2b91af;">xsl:template</span><span style="color: #0000ff;">&gt;

  &lt;</span><span style="color: #2b91af;">xsl:template</span><span style="color: #ff0000;"> match</span><span style="color: #0000ff;">=</span>'<span style="color: #0000ff;">Row</span>'<span style="color: #0000ff;">&gt;
    &lt;</span><span style="color: #a31515;">li</span><span style="color: #0000ff;">&gt;
      &lt;</span><span style="color: #2b91af;">xsl:value-of</span><span style="color: #ff0000;"> select</span><span style="color: #0000ff;">=</span>'<span style="color: #0000ff;">@Title</span>'<span style="color: #0000ff;">/&gt;
    &lt;/</span><span style="color: #a31515;">li</span><span style="color: #0000ff;">&gt;
  &lt;/</span><span style="color: #2b91af;">xsl:template</span><span style="color: #0000ff;">&gt;
&lt;/</span><span style="color: #2b91af;">xsl:stylesheet</span><span style="color: #0000ff;">&gt;</span></code></pre>
</div>
<p>At this point, the HTML rendered by the <strong>XsltListViewWebPart</strong> is:</p>
<div class="sample">
<h2>This Week&rsquo;s Specials</h2>
<ol class="specials group">
<li>Fisherman&amp;#39;s Brew</li>
<li>Boathouse Bold</li>
<li>Deadly Decaf</li>
</ol></div>
<p>As you can see, we need to tweak the XSLT a little bit in order to render special characters as expected (such as an encoded apostrophe). This is simply a matter of adding the <code><span style="color: #ff0000;">disable-output-escaping</span></code> attribute:</p>
<div class="codeBlock">
<pre><code><span style="color: #0000ff;">&lt;?</span><span style="color: #a31515;">xml</span><span style="color: #ff0000;"> version</span><span style="color: #0000ff;">=</span>"<span style="color: #0000ff;">1.0</span>"<span style="color: #ff0000;"> encoding</span><span style="color: #0000ff;">=</span>"<span style="color: #0000ff;">utf-8</span>"<span style="color: #0000ff;">?&gt;
&lt;</span><span style="color: #2b91af;">xsl:stylesheet</span><span style="color: #ff0000;"> version</span><span style="color: #0000ff;">=</span>"<span style="color: #0000ff;">1.0</span>"
<span style="color: #ff0000;">  xmlns:xsl</span><span style="color: #0000ff;">=</span>"<span style="color: #0000ff;">https://www.w3.org/1999/XSL/Transform</span>"
<span style="color: #ff0000;">  xmlns:msxsl</span><span style="color: #0000ff;">=</span>"<span style="color: #0000ff;">urn:schemas-microsoft-com:xslt</span>"
<span style="color: #ff0000;">  exclude-result-prefixes</span><span style="color: #0000ff;">=</span>"<span style="color: #0000ff;">msxsl</span>"<span style="color: #0000ff;">&gt;

  &lt;</span><span style="color: #2b91af;">xsl:output</span><span style="color: #ff0000;"> method</span><span style="color: #0000ff;">=</span>'<span style="color: #0000ff;">html</span>'<span style="color: #ff0000;"> indent</span><span style="color: #0000ff;">=</span>'<span style="color: #0000ff;">yes</span>'<span style="color: #0000ff;">/&gt;

  &lt;</span><span style="color: #2b91af;">xsl:template</span><span style="color: #ff0000;"> match</span><span style="color: #0000ff;">=</span>'<span style="color: #0000ff;">dsQueryResponse</span>'<span style="color: #0000ff;">&gt;
    &lt;</span><span style="color: #a31515;">h2</span><span style="color: #0000ff;">&gt;</span>This Week's Specials<span style="color: #0000ff;">&lt;/</span><span style="color: #a31515;">h2</span><span style="color: #0000ff;">&gt;
    &lt;</span><span style="color: #a31515;">ol</span><span style="color: #ff0000;"> class</span><span style="color: #0000ff;">=</span>'<span style="color: #0000ff;">specials group</span>'<span style="color: #0000ff;">&gt;
      &lt;</span><span style="color: #2b91af;">xsl:apply-templates</span><span style="color: #ff0000;"> select</span><span style="color: #0000ff;">=</span>'<span style="color: #0000ff;">Rows/Row</span>'<span style="color: #0000ff;">/&gt;
    &lt;/</span><span style="color: #a31515;">ol</span><span style="color: #0000ff;">&gt;
  &lt;/</span><span style="color: #2b91af;">xsl:template</span><span style="color: #0000ff;">&gt;

  &lt;</span><span style="color: #2b91af;">xsl:template</span><span style="color: #ff0000;"> match</span><span style="color: #0000ff;">=</span>'<span style="color: #0000ff;">Row</span>'<span style="color: #0000ff;">&gt;
    &lt;</span><span style="color: #a31515;">li</span><span style="color: #0000ff;">&gt;
      &lt;</span><span style="color: #2b91af;">xsl:value-of</span><span style="color: #ff0000;"> select</span><span style="color: #0000ff;">=</span>'<span style="color: #0000ff;">@Title</span>'<span style="color: #ff0000;"> disable-output-escaping</span><span style="color: #0000ff;">=</span>'<span style="color: #0000ff;">yes</span>'<span style="color: #0000ff;">/&gt;
    &lt;/</span><span style="color: #a31515;">li</span><span style="color: #0000ff;">&gt;
  &lt;/</span><span style="color: #2b91af;">xsl:template</span><span style="color: #0000ff;">&gt;
&lt;/</span><span style="color: #2b91af;">xsl:stylesheet</span><span style="color: #0000ff;">&gt;</span></code></pre>
</div>
<p>Now that we have the basic implementation working, let's replace the content of the <code><span style="color: #0000ff;">&lt;</span><span style="color: #a31515;">li</span><span style="color: #0000ff;">&gt;</span></code> element with the actual markup from the static HTML, and replace the various pieces of content with corresponding placeholders:</p>
<div class="codeBlock">
<pre><code><span style="color: #0000ff;">&lt;?</span><span style="color: #a31515;">xml</span><span style="color: #ff0000;"> version</span><span style="color: #0000ff;">=</span>"<span style="color: #0000ff;">1.0</span>"<span style="color: #ff0000;"> encoding</span><span style="color: #0000ff;">=</span>"<span style="color: #0000ff;">utf-8</span>"<span style="color: #0000ff;">?&gt;
&lt;</span><span style="color: #2b91af;">xsl:stylesheet</span><span style="color: #ff0000;"> version</span><span style="color: #0000ff;">=</span>"<span style="color: #0000ff;">1.0</span>"
<span style="color: #ff0000;">  xmlns:xsl</span><span style="color: #0000ff;">=</span>"<span style="color: #0000ff;">https://www.w3.org/1999/XSL/Transform</span>"
<span style="color: #ff0000;">  xmlns:msxsl</span><span style="color: #0000ff;">=</span>"<span style="color: #0000ff;">urn:schemas-microsoft-com:xslt</span>"
<span style="color: #ff0000;">  exclude-result-prefixes</span><span style="color: #0000ff;">=</span>"<span style="color: #0000ff;">msxsl</span>"<span style="color: #0000ff;">&gt;

  &lt;</span><span style="color: #2b91af;">xsl:output</span><span style="color: #ff0000;"> method</span><span style="color: #0000ff;">=</span>'<span style="color: #0000ff;">html</span>'<span style="color: #ff0000;"> indent</span><span style="color: #0000ff;">=</span>'<span style="color: #0000ff;">yes</span>'<span style="color: #0000ff;">/&gt;

  &lt;</span><span style="color: #2b91af;">xsl:template</span><span style="color: #ff0000;"> match</span><span style="color: #0000ff;">=</span>'<span style="color: #0000ff;">dsQueryResponse</span>'<span style="color: #0000ff;">&gt;
    &lt;</span><span style="color: #a31515;">h2</span><span style="color: #0000ff;">&gt;</span>This Week's Specials<span style="color: #0000ff;">&lt;/</span><span style="color: #a31515;">h2</span><span style="color: #0000ff;">&gt;
    &lt;</span><span style="color: #a31515;">ol</span><span style="color: #ff0000;"> class</span><span style="color: #0000ff;">=</span>'<span style="color: #0000ff;">specials group</span>'<span style="color: #0000ff;">&gt;
      &lt;</span><span style="color: #2b91af;">xsl:apply-templates</span><span style="color: #ff0000;"> select</span><span style="color: #0000ff;">=</span>'<span style="color: #0000ff;">Rows/Row</span>'<span style="color: #0000ff;">/&gt;
    &lt;/</span><span style="color: #a31515;">ol</span><span style="color: #0000ff;">&gt;
  &lt;/</span><span style="color: #2b91af;">xsl:template</span><span style="color: #0000ff;">&gt;

  &lt;</span><span style="color: #2b91af;">xsl:template</span><span style="color: #ff0000;"> match</span><span style="color: #0000ff;">=</span>'<span style="color: #0000ff;">Row</span>'<span style="color: #0000ff;">&gt;
    &lt;</span><span style="color: #a31515;">li</span><span style="color: #ff0000;"> class</span><span style="color: #0000ff;">=</span>'<span style="color: #0000ff;">group</span>'<span style="color: #0000ff;">&gt;
      &lt;</span><span style="color: #a31515;">div</span><span style="color: #ff0000;"> class</span><span style="color: #0000ff;">=</span>'<span style="color: #0000ff;">special</span>'<span style="color: #0000ff;">&gt;
        &lt;</span><span style="color: #a31515;">div</span><span style="color: #ff0000;"> class</span><span style="color: #0000ff;">=</span>'<span style="color: #0000ff;">special-img</span>'<span style="color: #0000ff;">&gt;
          &lt;</span><span style="color: #a31515;">a</span><span style="color: #ff0000;"> href</span><span style="color: #0000ff;">=</span>'<span style="color: #0000ff;">#</span>'<span style="color: #0000ff;">&gt;
            &lt;</span><span style="color: #2b91af;">xsl:value-of</span><span style="color: #ff0000;"> select</span><span style="color: #0000ff;">=</span>'<span style="color: #0000ff;">@PublishingRollupImage</span>'
<span style="color: #ff0000;">                disable-output-escaping</span><span style="color: #0000ff;"> =</span>'<span style="color: #0000ff;">yes</span>'<span style="color: #0000ff;">/&gt;
            &lt;</span><span style="color: #2b91af;">xsl:text</span><span style="color: #0000ff;">&gt; &lt;/</span><span style="color: #2b91af;">xsl:text</span><span style="color: #0000ff;">&gt;
            &lt;</span><span style="color: #a31515;">span</span><span style="color: #0000ff;">&gt;
              &lt;</span><span style="color: #a31515;">strong</span><span style="color: #0000ff;">&gt;
                &lt;</span><span style="color: #2b91af;">xsl:value-of</span><span style="color: #ff0000;"> select</span><span style="color: #0000ff;">=</span>'<span style="color: #0000ff;">@Title</span>'<span style="color: #ff0000;"> disable-output-escaping</span><span style="color: #0000ff;"> =</span>'<span style="color: #0000ff;">yes</span>'<span style="color: #0000ff;">/&gt;
              &lt;/</span><span style="color: #a31515;">strong</span><span style="color: #0000ff;">&gt;
              &lt;</span><span style="color: #2b91af;">xsl:text</span><span style="color: #0000ff;">&gt; &lt;/</span><span style="color: #2b91af;">xsl:text</span><span style="color: #0000ff;">&gt;
              &lt;</span><span style="color: #a31515;">em</span><span style="color: #0000ff;">&gt;
                &lt;</span><span style="color: #2b91af;">xsl:value-of</span><span style="color: #ff0000;"> select</span><span style="color: #0000ff;">=</span>'<span style="color: #0000ff;">@Price</span>'<span style="color: #0000ff;">/&gt;
              &lt;/</span><span style="color: #a31515;">em</span><span style="color: #0000ff;">&gt;
            &lt;/</span><span style="color: #a31515;">span</span><span style="color: #0000ff;">&gt;
          &lt;/</span><span style="color: #a31515;">a</span><span style="color: #0000ff;">&gt;
        &lt;/</span><span style="color: #a31515;">div</span><span style="color: #0000ff;">&gt;
      &lt;/</span><span style="color: #a31515;">div</span><span style="color: #0000ff;">&gt;
    &lt;/</span><span style="color: #a31515;">li</span><span style="color: #0000ff;">&gt;
  &lt;/</span><span style="color: #2b91af;">xsl:template</span><span style="color: #0000ff;">&gt;
&lt;/</span><span style="color: #2b91af;">xsl:stylesheet</span><span style="color: #0000ff;">&gt;</span></code></pre>
</div>
<p>At this point, our HTML is very close to what we want it to be:</p>
<div class="sample">
<h2>This Week&rsquo;s Specials</h2>
<ol class="specials group">
<li class="group">
<div class="special">
<div class="special-img"><a href="#"><img src="https://blogs.msdn.com/PublishingImages/boat.jpg" /> <span><strong>Fisherman's Brew</strong> <em>$9.98 / lb.</em></span> </a></div>
</div>
</li>
<li class="group">
<div class="special">
<div class="special-img"><a href="#"><img src="https://blogs.msdn.com/PublishingImages/ropes.jpg" /> <span><strong>Boathouse Bold</strong> <em>$12.50 / lb.</em></span></a></div>
</div>
</li>
<li class="group">
<div class="special">
<div class="special-img"><a href="#"><img src="https://blogs.msdn.com/PublishingImages/fame.jpg" /> <span><strong>Deadly Decaf</strong> <em>$7.49 / lb.</em></span></a></div>
</div>
</li>
</ol></div>
<p>Obviously the image URLs are invalid (outside the context of the SharePoint site -- due to server-relative URLs), but that's okay...the images should appear as soon as we view this within the actual SharePoint site.</p>
<p>However, there is still one minor discrepancy between the dynamic HTML generated using the above XSLT and the original static HTML created by Dan. The problem is that the third list item needs to have an additional CSS class name specified (in other words, for the last item in the list we need to render <code><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">li</span><span style="color: #ff0000;"> class</span><span style="color: #0000ff;">="group third"&gt;</span></code> instead of <code><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">li</span><span style="color: #ff0000;"> class</span><span style="color: #0000ff;">="group"&gt;</span></code>).</p>
<p>Fortunately, this is very easy to achieve using a little more XSLT. Here is the final version of the XSL stylesheet that I developed to render items in the <strong>Specials </strong>list to match the original HTML specified by the Web designer (Dan):</p>
<div class="codeBlock">
<pre><code><span style="color: #0000ff;">&lt;</span><span style="color: #2b91af;">xsl:stylesheet</span><span style="color: #ff0000;"> version</span><span style="color: #0000ff;">=</span>'<span style="color: #0000ff;">1.0</span>'
<span style="color: #ff0000;">    xmlns:xsl</span><span style="color: #0000ff;">=</span>'<span style="color: #0000ff;">https://www.w3.org/1999/XSL/Transform</span>'
<span style="color: #ff0000;">    xmlns:msxsl</span><span style="color: #0000ff;">=</span>'<span style="color: #0000ff;">urn:schemas-microsoft-com:xslt</span>'
<span style="color: #ff0000;">    xmlns:ddwrt2</span><span style="color: #0000ff;">=</span>'<span style="color: #0000ff;">urn:frontpage:internal</span>'
<span style="color: #ff0000;">    exclude-result-prefixes</span><span style="color: #0000ff;">=</span>'<span style="color: #0000ff;">msxsl</span>'<span style="color: #0000ff;">&gt;
  &lt;</span><span style="color: #2b91af;">xsl:output</span><span style="color: #ff0000;"> method</span><span style="color: #0000ff;">=</span>'<span style="color: #0000ff;">html</span>'<span style="color: #ff0000;"> indent</span><span style="color: #0000ff;">=</span>'<span style="color: #0000ff;">yes</span>'<span style="color: #0000ff;">/&gt;
  &lt;</span><span style="color: #2b91af;">xsl:template</span><span style="color: #ff0000;"> match</span><span style="color: #0000ff;">=</span>'<span style="color: #0000ff;">dsQueryResponse</span>'<span style="color: #0000ff;">&gt;
    &lt;</span><span style="color: #a31515;">h2</span><span style="color: #0000ff;">&gt;</span>This Week's Specials<span style="color: #0000ff;">&lt;/</span><span style="color: #a31515;">h2</span><span style="color: #0000ff;">&gt;
    &lt;</span><span style="color: #a31515;">ol</span><span style="color: #ff0000;"> class</span><span style="color: #0000ff;">=</span>'<span style="color: #0000ff;">specials group</span>'<span style="color: #0000ff;">&gt;
      &lt;</span><span style="color: #2b91af;">xsl:apply-templates</span><span style="color: #ff0000;"> select</span><span style="color: #0000ff;">=</span>'<span style="color: #0000ff;">Rows/Row</span>'<span style="color: #0000ff;">/&gt;
    &lt;/</span><span style="color: #a31515;">ol</span><span style="color: #0000ff;">&gt;
  &lt;/</span><span style="color: #2b91af;">xsl:template</span><span style="color: #0000ff;">&gt;

  &lt;</span><span style="color: #2b91af;">xsl:template</span><span style="color: #ff0000;"> match</span><span style="color: #0000ff;">=</span>'<span style="color: #0000ff;">Row</span>'<span style="color: #0000ff;">&gt;
    &lt;</span><span style="color: #a31515;">li</span><span style="color: #0000ff;">&gt;
      &lt;</span><span style="color: #2b91af;">xsl:choose</span><span style="color: #0000ff;">&gt;
        &lt;</span><span style="color: #2b91af;">xsl:when</span><span style="color: #ff0000;"> test</span><span style="color: #0000ff;">=</span>'<span style="color: #0000ff;">position() = 3</span>'<span style="color: #0000ff;">&gt;
          &lt;</span><span style="color: #2b91af;">xsl:attribute</span><span style="color: #ff0000;"> name</span><span style="color: #0000ff;">=</span>'<span style="color: #0000ff;">class</span>'<span style="color: #0000ff;">&gt;</span>group third<span style="color: #0000ff;">&lt;/</span><span style="color: #2b91af;">xsl:attribute</span><span style="color: #0000ff;">&gt;
        &lt;/</span><span style="color: #2b91af;">xsl:when</span><span style="color: #0000ff;">&gt;
        &lt;</span><span style="color: #2b91af;">xsl:otherwise</span><span style="color: #0000ff;">&gt;
          &lt;</span><span style="color: #2b91af;">xsl:attribute</span><span style="color: #ff0000;"> name</span><span style="color: #0000ff;">=</span>'<span style="color: #0000ff;">class</span>'<span style="color: #0000ff;">&gt;</span>group<span style="color: #0000ff;">&lt;/</span><span style="color: #2b91af;">xsl:attribute</span><span style="color: #0000ff;">&gt;
        &lt;/</span><span style="color: #2b91af;">xsl:otherwise</span><span style="color: #0000ff;">&gt;
      &lt;/</span><span style="color: #2b91af;">xsl:choose</span><span style="color: #0000ff;">&gt;
      &lt;</span><span style="color: #a31515;">div</span><span style="color: #ff0000;"> class</span><span style="color: #0000ff;">=</span>'<span style="color: #0000ff;">special</span>'<span style="color: #0000ff;">&gt;
        &lt;</span><span style="color: #a31515;">div</span><span style="color: #ff0000;"> class</span><span style="color: #0000ff;">=</span>'<span style="color: #0000ff;">special-img</span>'<span style="color: #0000ff;">&gt;
          &lt;</span><span style="color: #a31515;">a</span><span style="color: #ff0000;"> href</span><span style="color: #0000ff;">=</span>'<span style="color: #0000ff;">#</span>'<span style="color: #0000ff;">&gt;
            &lt;</span><span style="color: #2b91af;">xsl:value-of</span><span style="color: #ff0000;"> select</span><span style="color: #0000ff;">=</span>'<span style="color: #0000ff;">@PublishingRollupImage</span>'
<span style="color: #ff0000;">                disable-output-escaping</span><span style="color: #0000ff;"> =</span>'<span style="color: #0000ff;">yes</span>'<span style="color: #0000ff;">/&gt;
            &lt;</span><span style="color: #2b91af;">xsl:text</span><span style="color: #0000ff;">&gt; &lt;/</span><span style="color: #2b91af;">xsl:text</span><span style="color: #0000ff;">&gt;
            &lt;</span><span style="color: #a31515;">span</span><span style="color: #0000ff;">&gt;
              &lt;</span><span style="color: #a31515;">strong</span><span style="color: #0000ff;">&gt;
                &lt;</span><span style="color: #2b91af;">xsl:value-of</span><span style="color: #ff0000;"> select</span><span style="color: #0000ff;">=</span>'<span style="color: #0000ff;">@Title</span>'<span style="color: #ff0000;"> disable-output-escaping</span><span style="color: #0000ff;"> =</span>'<span style="color: #0000ff;">yes</span>'<span style="color: #0000ff;">/&gt;
              &lt;/</span><span style="color: #a31515;">strong</span><span style="color: #0000ff;">&gt;
              &lt;</span><span style="color: #2b91af;">xsl:text</span><span style="color: #0000ff;">&gt; &lt;/</span><span style="color: #2b91af;">xsl:text</span><span style="color: #0000ff;">&gt;
              &lt;</span><span style="color: #a31515;">em</span><span style="color: #0000ff;">&gt;
                &lt;</span><span style="color: #2b91af;">xsl:value-of</span><span style="color: #ff0000;"> select</span><span style="color: #0000ff;">=</span>'<span style="color: #0000ff;">@Price</span>'<span style="color: #0000ff;">/&gt;
              &lt;/</span><span style="color: #a31515;">em</span><span style="color: #0000ff;">&gt;
            &lt;/</span><span style="color: #a31515;">span</span><span style="color: #0000ff;">&gt;
          &lt;/</span><span style="color: #a31515;">a</span><span style="color: #0000ff;">&gt;
        &lt;/</span><span style="color: #a31515;">div</span><span style="color: #0000ff;">&gt;
      &lt;/</span><span style="color: #a31515;">div</span><span style="color: #0000ff;">&gt;
    &lt;/</span><span style="color: #a31515;">li</span><span style="color: #0000ff;">&gt;
  &lt;/</span><span style="color: #2b91af;">xsl:template</span><span style="color: #0000ff;">&gt;
&lt;/</span><span style="color: #2b91af;">xsl:stylesheet</span><span style="color: #0000ff;">&gt;</span></code></pre>
</div>
<p>Copy this into the the <code><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">xsl</span><span style="color: #0000ff;">&gt;</span></code> element for the Web Part (using SharePoint Designer) and save the page. Then view the page in Internet Explorer to verify that it renders the expected HTML.</p>
<p>Once you have a test page rendering the SharePoint list items using the HTML you want (rather than the out-of-the-box table-based layout), you can export the Web Part to a .webpart file and subsequently import it onto the home page (at which point, the custom Tugboat CSS rules will be applied and the content should exactly match the static HTML previously used to render <strong>This Week's Specials</strong>).</p>
<p>There's still quite a bit I want to cover on this topic, but I need to move on to my "day job". In my next post, I'll show you how to programmatically configure the <strong>Specials </strong>list (yep, you guessed it...upon feature activation), add some sample items to the list, and replace the static HTML content on the home page with an instance of the <strong>XsltListViewWebPart</strong> configured with the custom XSLT.</p>
<p>There are also some "bugs" in this new solution. For example, what happens if there are more than three items in the <strong>Specials </strong>list? Don't worry, this is easy to fix -- thanks to the power of SharePoint.</p>
<p>Stay tuned...</p>
<blockquote class="note update">
<div class="noteTitle"><strong>Update (2011-05-02)</strong></div>
<div>The <a href="https://blogs.msdn.com/b/jjameson/archive/2011/05/02/web-standards-design-with-sharepoint-part-6.aspx">next post in this series</a> is now available.</div>
</blockquote>]]></content:encoded>
    </item>
    <item>
      <title>Script to Reset WSUS for SysPrep'ed Image</title>
      <link>https://docs.microsoft.com/archive/blogs/jjameson/script-to-reset-wsus-for-syspreped-image</link>
      <pubDate>Mon, 25 Apr 2011 05:03:15 GMT</pubDate>
      <dc:creator><![CDATA[Jeremy Jameson - MSFT]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/jjameson/2011/04/25/script-to-reset-wsus-for-syspreped-image/</guid>
      <description><![CDATA[Here's a useful script for those, like me, that use SysPrep'ed images to create new virtual machines...]]></description>
      <content:encoded><![CDATA[<p>Here's a useful script for those, like me, that <a href="https://blogs.msdn.com/b/jjameson/archive/2009/08/13/using-sysprep-ed-vhds-for-new-hyper-v-virtual-machines.aspx">use SysPrep'ed images to create new virtual machines</a> and also leverage Windows Server Update Services (WSUS) to keep machines up-to-date with the latest patches.</p>
<h3>Reset WSUS for SysPrep Image.cmd</h3>
<div class="codeBlock">
<pre><code>net stop wuauserv

reg.exe delete HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate /v PingID /f
reg.exe delete HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate /v AccountDomainSid /f
reg.exe delete HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate /v SusClientId /f
reg.exe delete HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate /v SusClientIDValidation /f

net start wuauserv

wuauclt.exe /resetauthorization /detectnow</code></pre>
</div>
<p>I keep a copy of this script in my <a href="https://blogs.msdn.com/b/jjameson/archive/2007/03/22/backedup-and-notbackedup.aspx">Toolbox</a> and run it after creating a new VM from a SysPrep'ed image.</p>
<p>If you are not sure why this is helpful, refer to the following KB article:</p>
<div class="reference"><cite>A Windows 2000-based, Windows Server 2003-based, or Windows XP-based computer that was set up by using a Windows 2000, Windows Server 2003, or Windows XP image does not appear in the WSUS console</cite>
<div class="referenceLink"><a href="https://support.microsoft.com/kb/903262/en-us">https://support.microsoft.com/kb/903262/en-us</a></div>
</div>]]></content:encoded>
    </item>
    <item>
      <title>Reusable Content in SharePoint Publishing HTML Fields, Part 3</title>
      <link>https://docs.microsoft.com/archive/blogs/jjameson/reusable-content-in-sharepoint-publishing-html-fields-part-3</link>
      <pubDate>Thu, 14 Apr 2011 06:56:00 GMT</pubDate>
      <dc:creator><![CDATA[Jeremy Jameson - MSFT]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/jjameson/2011/04/14/reusable-content-in-sharepoint-publishing-html-fields-part-3/</guid>
      <description><![CDATA[In part 2 of this series, I explained how to programmatically add a new Reusable Content list item...]]></description>
      <content:encoded><![CDATA[<p>In <a href="https://blogs.msdn.com/b/jjameson/archive/2011/04/13/reusable-content-in-sharepoint-publishing-html-fields-part-2.aspx">part 2 of this series</a>, I explained how to programmatically add a new <strong>Reusable Content </strong>list item and subsequently add it to a Publishing HTML field on a page. I also provided a complete sample for SharePoint 2010 that demonstrates how this can be accomplished with minimal effort (thanks to some nitfy helper classes).</p>
<p>However, depending on your specific business requirements, <em>creating</em> a SharePoint page that leverages reusable content may only be half the battle. What if you need to get that content out of SharePoint?</p>
<p>For example, in <a href="https://blogs.msdn.com/b/jjameson/archive/2011/04/08/reusable-content-in-sharepoint-publishing-html-fields-part-1.aspx">part 1 of this series</a>, I mentioned how I previously built a custom "document publishing" system based on the WCM features in SharePoint. In the first sprint of that effort, one of the core scenarios was exporting a SharePoint site (typically containing about a hundred pages of content) to a PDF file. We call this the "Export to PDF" feature. In the current sprint, I've been working on an enhancement to export the documents to an external system (so clients can view a copy of the content stored in that other system). We call this the "Export to {external system}" feature. [I've substituted the name of the actual system with a placeholder to protect the innocent ;-) ]</p>
<p>The "Export to PDF" feature was originally built with very little code. [You may recall that I previously stated this solution had to be delivered to Production in four weeks.]</p>
<p>Essentially, I wrote a little bit of code to "aggregate" all of the pages in a specific SharePoint site into one large HTML document. The solution then uses <a href="https://www.princexml.com/">Prince</a> (in combination with some custom cascading style sheets I created) to convert the HTML file into a commercial-quality PDF file.</p>
<p>The "Export to PDF" process typically takes between 10-15 seconds (which seems very reasonable given that some of the resulting PDF documents can exceed 100 printed pages). Consequently, I implemented this using a "long running page" in SharePoint (you know, one of those application pages with the "spinning wheel" that says something like "Please wait while...").</p>
<p>The most important aspect of the "Export to PDF" feature -- with regards to reusable content -- is that it runs within the context of a SharePoint HTTP request. In other words, when <strong>SPContext.Current </strong>is not null. During that original sprint, I discovered that it is actually quite trivial to "expand" the reusable content placeholders in Publishing HTML fields <em>when </em><strong><em>SPContext.Current </em></strong><em>is not null</em>. On the other hand, when <strong>SPContext.Current </strong><em>is </em>null, it takes a fair amount of custom code to retrieve the same content. More on that in a moment.</p>
<p>Let's start with the simplest scenario first...</p>
<h3>"Expanding" reusable content placeholders when SPContext.Current is not null</h3>
<p>When you need to get the HTML content from a SharePoint page and SPContext.Current is not null (i.e. your code is running in the context of a SharePoint HTTP request), then you can simply use the <a href="https://msdn.microsoft.com/en-us/library/microsoft.sharepoint.publishing.fields.htmlfield.getfieldvalueashtml.aspx">HtmlField.GetFieldValueAsHtml</a> method, as shown below:</p>
<div class="codeBlock">
<pre><span style="font-size: 9pt">    </span><code><span style="color: #2b91af">HtmlField</span> pageContentField =
        (<span style="color: #2b91af">HtmlField</span>) page.Fields[<span style="color: #2b91af">FieldId</span>.PublishingPageContent];

    <span style="color: #0000ff">string</span> pageContent = pageContentField.GetFieldValueAsHtml(
        page.ListItem[<span style="color: #2b91af">FieldId</span>.PublishingPageContent]);</code></pre>
</div>
<p>If you look at this method with Reflector, you'll see that most of the work is actually done by the <strong><a href="https://msdn.microsoft.com/en-us/library/microsoft.sharepoint.publishing.internal.webcontrols.htmleditorinternal(v=office.12).aspx">HtmlEditorInternal</a> </strong>class.</p>
<p>If SPContext.Current is null, then a NullReferenceException is thrown in the <strong><a href="https://msdn.microsoft.com/en-us/library/microsoft.sharepoint.publishing.internal.webcontrols.htmleditorinternal_members(v=office.12).aspx">HtmlEditorInternal.ConvertStorageFormatToViewFormat</a></strong> method (due to an attempt to reference SPContext.Current.Site).</p>
<p>When developing the original "Export to PDF" solution, I implemented a hack to insert a warning message in the underlying HTML when SPContext.Current is null. [Note that I tend to write as much code as a I can outside the context of a Web request -- for example, by writing unit tests that invoke the core services underlying the presentation layer.]</p>
<p>The original code looked like this:</p>
<div class="codeBlock">
<pre><code><span style="color: #0000ff">                if</span> (pageContent.Contains(<span style="color: #a31515">"__publishingReusableFragment"</span>) == <span style="color: #0000ff">true</span>)
                {
                    <span style="color: #008000">// HACK: We need to "expand" the reusable content in order
                    // to obtain the complete HTML page content. Unfortunately,
                    // SharePoint does not currently provide an easy way to do
                    // this outside the context of a Web request. Therefore
                    // just emit a warning message in the content when
                    // SPContext.Current is null (since attempting to replace
                    // the reusable content fragments ourselves would require
                    // a substantial amount of custom code).</span><span style="color: #0000ff">
                    if</span> (SPContext.Current != <span style="color: #0000ff">null</span>)
                    {
                        <span style="color: #2b91af">HtmlField</span> pageContentField =
                            (<span style="color: #2b91af">HtmlField</span>)page.Fields[<span style="color: #2b91af">FieldId</span>.PublishingPageContent];

                        pageContent = pageContentField.GetFieldValueAsHtml(
                            pageContent);
                    }
                    <span style="color: #0000ff">else</span>
                    {
                        writer.WriteLine(
<span style="color: #a31515">@"&lt;p&gt;&lt;strong&gt;Warning:&lt;/strong&gt; The page content contains reusable content, which
    is not included when SPContext.Current is null. To obtain the full content
    -- including all reusable content -- submit the request within the context
    of a SharePoint site.&lt;/p&gt;"</span>);
                    }
                }</code></pre>
</div>
<p>This worked fine for the original release because we only needed to support the "Export to PDF" feature in the "long running page" that I mentioned before. When running my unit tests, the resulting PDF contained the warning message inserted by the code above, but that was just fine with me.</p>
<p>However, as is often the case, the business requirements changed over time and we needed to evolve the solution accordingly.</p>
<h3>"Expanding" reusable content placeholders when SPContext.Current is null</h3>
<p>As I mentioned before, in addition to the original "Export to PDF" feature, we now need the ability to export the documents to an external system. The problem is that, unlike the process of exporting to PDF, exporting to an external system isn't going to run in a matter of a few seconds. Rather, I estimated that it could take several minutes to export a ~100 page document from SharePoint to the external system. [Keep in mind that this is going over the Internet and we also need to export inline images and referenced files from the SharePoint site to the external system (in addition to the actual HTML content).]</p>
<p>Due to the expected latency of the export process, I decided to implement it using a custom SharePoint workflow. This would allow it to run asynchronously instead of within the context of a "long running page" (which would be problematic if the user closed the browser while the export was still running).</p>
<p>The problem with running the export in a workflow, however, is that SPContext.Current is null -- and unlike, my simple hack in the intial sprint, a warning message simply wasn't going to cut it in this case ;-)</p>
<p>Consequently, I implemented a custom method in the <strong>SharePointHtmlFieldHelper </strong>class for getting the "expanded" HTML content from a Publishing HTML field:</p>
<div class="codeBlock">
<pre><code><span style="color: #0000ff">        public static string</span> GetFieldValueAsHtml(
            <span style="color: #2b91af">SPWeb</span> web,
            <span style="color: #0000ff">object</span> value)
        {
            <span style="color: #0000ff">if</span> (web == <span style="color: #0000ff">null</span>)
            {
                <span style="color: #0000ff">throw new</span><span style="color: #2b91af"> ArgumentNullException</span>(<span style="color: #a31515">"web"</span>);
            }

            <span style="color: #0000ff">string</span> str = value <span style="color: #0000ff">as string</span>;

            <span style="color: #0000ff">if</span> (<span style="color: #0000ff">string</span>.IsNullOrEmpty(str) == <span style="color: #0000ff">true</span>)
            {
                <span style="color: #0000ff">return string</span>.Empty;
            }

            <span style="color: #0000ff">bool</span> canCacheResults = <span style="color: #0000ff">true</span>;

            <span style="color: #0000ff">return</span> ConvertStorageFormatToViewFormat(
                web,
                str,
                <span style="color: #0000ff">out</span> canCacheResults);
        }</code></pre>
</div>
<p>If the method signatures look a little goofy, it's because I tried to keep them as similar as possible to the out-of-the-box SharePoint classes. [My hope -- no matter how small the probability -- is that the SharePoint code will be eventually be improved to handle the scenario where SPContext.Current is null. Let's just say that I'm not expecting that to happen, but that doesn't mean I can't hope for more robust code from the SharePoint team.]</p>
<p>The reason why I require an SPWeb object is so I can subsequently read the corresponding list items from the <strong>Reusable Content </strong>list. [Technically speaking, I really only need an SPSite object, but I chose an SPWeb in order to provide additional detail in cases where "malformed" content is detected (e.g. the number of placeholders specified in the HTML does not match the number of items in the reusable content "header").]</p>
<p>If you look at the code (see attachment on previous post), then you'll see that I'm essentially just following the original implementation in <strong>HtmlEditorInternal </strong>except that I don't rely on SPContext.Current and instead use the specified SPWeb parameter to access the <strong>Reusable Content </strong>list.</p>
<blockquote class="note important">
<div class="noteTitle"><strong>Important</strong></div>
<div>Also note that, unlike the original <strong>HtmlEditorInternal</strong> implementation, I don't leverage any caching when fetching <strong>Reusable Content </strong>list items. Consequently, you should be wary of using this implementation in very high volume scenarios. For the purposes of my current project, performance has proved to be more than adequate.</div>
</blockquote>
<p>I should point out, however, that I still prefer to use the out-of-the-box SharePoint code whenever possible, which is why I provide a wrapper method in the <strong>SharePointPublishingHelper</strong> class that determines whether or not it is "safe" to use the <strong>HtmlField.GetFieldValueAsHtml </strong>method:</p>
<div class="codeBlock">
<pre><code><span style="color: #808080">        /// &lt;summary&gt;
        ///</span><span style="color: #008000"> Returns the value of the "Page Content" field for the specified page</span><span style="color: #808080">
        ///</span><span style="color: #008000"> in HTML format. If the field contains any "reusable content"</span><span style="color: #808080">
        ///</span><span style="color: #008000"> placeholders, they are expanded in order to return the HTML just as</span><span style="color: #808080">
        ///</span><span style="color: #008000"> it would appear directly on the page.</span><span style="color: #808080">
        /// &lt;/summary&gt;
        /// &lt;param name="page"&gt;</span><span style="color: #008000">The page to get the content for.</span><span style="color: #808080">&lt;/param&gt;
        /// &lt;returns&gt;</span><span style="color: #008000">The HTML content for the "Page Content" field.</span><span style="color: #808080">&lt;/returns&gt;</span><span style="color: #0000ff">
        public static string</span> GetPageContentAsHtml(
            <span style="color: #2b91af">PublishingPage</span> page)
        {
            <span style="color: #0000ff">if</span> (page == <span style="color: #0000ff">null</span>)
            {
                <span style="color: #0000ff">throw new</span><span style="color: #2b91af"> ArgumentNullException</span>(<span style="color: #a31515">"page"</span>);
            }

            <span style="color: #0000ff">string</span> pageContent = <span style="color: #0000ff">null</span>;

            <span style="color: #0000ff">if</span> (<span style="color: #2b91af">SPContext</span>.Current != <span style="color: #0000ff">null</span>)
            {
                <span style="color: #008000">// In this case, the out-of-the-box SharePoint functionality
                // works, so use it (rather than our own hack)</span><span style="color: #2b91af">
                HtmlField</span> pageContentField =
                    (<span style="color: #2b91af">HtmlField</span>) page.Fields[<span style="color: #2b91af">FieldId</span>.PublishingPageContent];

                pageContent = pageContentField.GetFieldValueAsHtml(
                    page.ListItem[<span style="color: #2b91af">FieldId</span>.PublishingPageContent]);

            }
            <span style="color: #0000ff">else</span>
            {
                <span style="color: #008000">// HACK: In this case, the out-of-the-box SharePoint
                // functionality throws a NullReferenceException, so get the
                // HTML content using custom code</span>
                pageContent = <span style="color: #2b91af">SharePointHtmlFieldHelper</span>.GetFieldValueAsHtml(
                     page.PublishingWeb.Web,
                     page.ListItem[<span style="color: #2b91af">FieldId</span>.PublishingPageContent]);
            }

            <span style="color: #0000ff">return</span> pageContent;
        }</code></pre>
</div>
<p>If you download the sample solution from my previous post, you can run all the various unit tests to verify things are working as expected, including the following unit test, which demonstrates why you can't use the <strong>HtmlField.GetFieldValueAsHtml </strong>method when SPContext.Current is null:</p>
<div class="codeBlock">
<pre><code><span style="color: #808080">        /// &lt;summary&gt;
        ///</span><span style="color: #008000"> This test simply demonstrates why the out-of-the-box</span><span style="color: #808080">
        /// &lt;see cref="Microsoft.SharePoint.Publishing.Fields.HtmlField.GetFieldValueAsHtml"/&gt;
        ///</span><span style="color: #008000"> method cannot be used to "expand" reusable content when</span><span style="color: #808080">
        ///</span><span style="color: #008000"> SPContext.Current is null.</span><span style="color: #808080">
        /// &lt;/summary&gt;</span>
        [System.Diagnostics.CodeAnalysis.<span style="color: #2b91af">SuppressMessage</span>(
            <span style="color: #a31515">"Microsoft.Naming"</span>,
            <span style="color: #a31515">"CA1707:IdentifiersShouldNotContainUnderscores"</span>)]
        [<span style="color: #2b91af">TestMethod</span>()]
        [<span style="color: #2b91af">ExpectedException</span>(<span style="color: #0000ff">typeof</span>(<span style="color: #2b91af">NullReferenceException</span>))]
        <span style="color: #0000ff">public void</span> HtmlField_GetFieldValueAsHtml_FailsWhenSPContextCurrentIsNull()
        {
            <span style="color: #0000ff">string</span> fabrikamDemoUrl = GetFabrikamDemoUrl();

            <span style="color: #0000ff">const string</span> webUrl = <span style="color: #a31515">"/"</span>;
            <span style="color: #0000ff">const string</span> pageUrl = <span style="color: #a31515">"Pages/ReusableContentSample.aspx"</span>;

            <span style="color: #008000">// The following HTML should not appear in the "storage" format but
            // should appear in the "view" format (in other words, when the
            // reusable content is "expanded")</span><span style="color: #0000ff">
            const string</span> reusableContentFragment =
                <span style="color: #a31515">"&lt;h2&gt;Statement of Non-Discrimination and Affirmative Action&lt;/h2&gt;"</span>;

            <span style="color: #0000ff">using</span> (<span style="color: #2b91af">SPSite</span> site = <span style="color: #0000ff">new</span><span style="color: #2b91af"> SPSite</span>(fabrikamDemoUrl))
            {
                <span style="color: #0000ff">using</span> (<span style="color: #2b91af">SPWeb</span> web = site.OpenWeb(webUrl))
                {
                    <span style="color: #2b91af">PublishingPage</span> page =
                        <span style="color: #2b91af">SharePointPublishingHelper</span>.FindPublishingPage(
                            web,
                            pageUrl);

                    <span style="color: #0000ff">string</span> pageContent = (<span style="color: #0000ff">string</span>)page.ListItem[
                        <span style="color: #2b91af">FieldId</span>.PublishingPageContent];

                    <span style="color: #2b91af">Assert</span>.IsTrue(pageContent.Contains(
                        <span style="color: #a31515">"__publishingReusableFragment"</span>));

                    <span style="color: #2b91af">Assert</span>.IsFalse(pageContent.Contains(
                        reusableContentFragment));

                    <span style="color: #008000">// Now demonstrate that HtmlField.GetFieldValueAsHtml
                    // throws NullReferenceException when SPContext.Current is
                    // null...</span><span style="color: #2b91af">
                    Assert</span>.IsNull(<span style="color: #2b91af">SPContext</span>.Current);

                    <span style="color: #2b91af">HtmlField</span> pageContentField =
                        (<span style="color: #2b91af">HtmlField</span>)page.Fields[<span style="color: #2b91af">FieldId</span>.PublishingPageContent];

                    pageContentField.GetFieldValueAsHtml(
                        pageContent);
                }
            }
        }</code></pre>
</div>
<p>If this unit test ever starts failing, I'll know there's a good chance that I can scrap my custom code and go with the out-of-the-box SharePoint code instead ;-)</p>
<blockquote class="note">
<div class="noteTitle"><strong>Note</strong></div>
<div>You must be running Visual Studio 2010 Service Pack 1 in order to run my SharePoint 2010 unit tests (in order to avoid bugs in 64-bit environments like the one I described in a <a href="https://blogs.msdn.com/b/jjameson/archive/2009/10/08/web-application-at-could-not-be-found-error-on-moss-2007-x64.aspx">post from a couple of years ago</a>). </div>
</blockquote>]]></content:encoded>
    </item>
    <item>
      <title>Reusable Content in SharePoint Publishing HTML Fields, Part 2</title>
      <link>https://docs.microsoft.com/archive/blogs/jjameson/reusable-content-in-sharepoint-publishing-html-fields-part-2</link>
      <pubDate>Wed, 13 Apr 2011 19:06:00 GMT</pubDate>
      <dc:creator><![CDATA[Jeremy Jameson - MSFT]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/jjameson/2011/04/13/reusable-content-in-sharepoint-publishing-html-fields-part-2/</guid>
      <description><![CDATA[In my previous post, I introduced a scenario for using the "Reusable Content" feature in Microsoft...]]></description>
      <content:encoded><![CDATA[<p>In <a href="https://blogs.msdn.com/b/jjameson/archive/2011/04/08/reusable-content-in-sharepoint-publishing-html-fields-part-1.aspx">my previous post</a>, I introduced a scenario for using the "Reusable Content" feature in Microsoft Office SharePoint Server (MOSS) 2007 and SharePoint Server 2010. In this post, I show you how to programmatically add <strong>Reusable Content </strong>list items (which is very helpful when deploying to multiple environments, such as DEV, TEST, and PROD) as well as how to insert reusable content into a Publishing HTML field on a page (e.g. the <strong>Page Content </strong>field). I also provide a complete code sample for SharePoint 2010 that demonstrates the key concepts discussed in this series.</p>
<h3>Adding a new Reusable Content list item</h3>
<p>Since reusable content in SharePoint is enabled by the Publishing features, I chose to enhance the <strong>SharePointPublishingHelper</strong> class -- which I introduced in a <a href="https://blogs.msdn.com/b/jjameson/archive/2009/10/09/introducing-the-sharepointpublishinghelper-class.aspx">previous post</a> -- to provide a new method for ensuring some piece of reusable content has been configured.</p>
<p>In case you are not familiar with the "ensure" logic that I tend to implement in various SharePoint helper classes, in essence the code performs any necessary actions to achieve some condition (for example, ensure a specific page exists -- by creating a new page if one doesn't already exist). The key design principle in the "ensure" logic is to avoid trampling any manual changes that may have been made to SharePoint by content authors and administrators.</p>
<p>When it comes to adding a piece of reusable content, this "ensure" logic means that we only want to configure the <strong>Reusable Content </strong>list item if it doesn't already exist. In other words, we don't want to overwrite the content if the list item already exists, because it may have been updated by a content author or administrator since it was originally created (presumably upon activation of some feature). [There are scenarios where you <em>do</em> want to overwrite the existing site configuration, but those are beyond the scope of this post.]</p>
<p>For example, consider the following method of <strong>SharePointPublishingHelper</strong>:</p>
<div class="codeBlock">
<pre><code><span style="color: #0000ff">        public static</span><span style="color: #2b91af"> SPListItem</span> EnsureReusableContentItem(
            <span style="color: #2b91af">SPSite</span> site,
            <span style="color: #0000ff">string</span> title,
            <span style="color: #0000ff">bool</span> automaticUpdate,
            <span style="color: #0000ff">string</span> reusableHtml)</code></pre>
</div>
<p>Imagine that you run the following code upon activation of a feature:</p>
<div class="codeBlock">
<pre><code><span style="color: #2b91af">            SPListItem</span> reusableContent =
                <span style="color: #2b91af">SharePointPublishingHelper</span>.EnsureReusableContentItem(
                    web.Site,
                    <span style="color: #a31515">"Copyright"</span>,
                    <span style="color: #0000ff">true</span>,
                    <span style="color: #a31515">"Copyright&copy; 2009 Contoso Corporation - All Rights Reserved"</span>);</code></pre>
</div>
<p>If the specified <strong>Reusable Content </strong>list item already exists (found by matching on the <strong>Title </strong>field), then the existing list item is returned. Otherwise, a new list item is added (with the specified field values) and subsequently returned. Note that we don't want to overwrite the content in the list item because someone may have updated the copyright content (e.g. "Copyright&copy; 2011 Fabrikam Technologies - All Rights Reserved").</p>
<p>Also note that the <strong>Reusable Content </strong>list is configured for approval by default, and -- as I mentioned in <a href="https://blogs.msdn.com/b/jjameson/archive/2011/04/08/reusable-content-in-sharepoint-publishing-html-fields-part-1.aspx">part 1 of this series</a> -- "bad things" happen when a page includes a reference to reusable content that has not been approved. Consequently, the <strong>EnsureReusableContentItem </strong>method also takes care of approving the list item (if it does not have at least one approved version):</p>
<div class="codeBlock">
<pre><code><span style="color: #0000ff">        public static</span><span style="color: #2b91af"> SPListItem</span> EnsureReusableContentItem(
            <span style="color: #2b91af">SPSite</span> site,
            <span style="color: #0000ff">string</span> title,
            <span style="color: #0000ff">bool</span> automaticUpdate,
            <span style="color: #0000ff">string</span> reusableHtml,
            <span style="color: #0000ff">string</span> comments,
            <span style="color: #0000ff">string</span> contentCategory)
        {
            <span style="color: #0000ff">if</span> (site == <span style="color: #0000ff">null</span>)
            {
                <span style="color: #0000ff">throw new</span><span style="color: #2b91af"> ArgumentNullException</span>(<span style="color: #a31515">"site"</span>);
            }

            <span style="color: #0000ff">if</span> (title == <span style="color: #0000ff">null</span>)
            {
                <span style="color: #0000ff">throw new</span><span style="color: #2b91af"> ArgumentNullException</span>(<span style="color: #a31515">"title"</span>);
            }

            title = title.Trim();
            <span style="color: #0000ff">if</span> (<span style="color: #0000ff">string</span>.IsNullOrEmpty(title) == <span style="color: #0000ff">true</span>)
            {
                <span style="color: #0000ff">throw new</span><span style="color: #2b91af"> ArgumentException</span>(
                    <span style="color: #a31515">"The title must be specified."</span>,
                    <span style="color: #a31515">"title"</span>);
            }

            <span style="color: #0000ff">if</span> (reusableHtml == <span style="color: #0000ff">null</span>)
            {
                <span style="color: #0000ff">throw new</span><span style="color: #2b91af"> ArgumentNullException</span>(<span style="color: #a31515">"reusableHtml"</span>);
            }

            reusableHtml = reusableHtml.Trim();
            <span style="color: #0000ff">if</span> (<span style="color: #0000ff">string</span>.IsNullOrEmpty(reusableHtml) == <span style="color: #0000ff">true</span>)
            {
                <span style="color: #0000ff">throw new</span><span style="color: #2b91af"> ArgumentException</span>(
                    <span style="color: #a31515">"The reusable HTML must be specified."</span>,
                    <span style="color: #a31515">"reusableHtml"</span>);
            }

            <span style="color: #008000">// Note: comments and contentCategory may be null</span><span style="color: #2b91af">
            
            SPLogger</span>.Log(
                <span style="color: #2b91af">LogCategory</span>.Configuration,
                <span style="color: #2b91af">TraceSeverity</span>.Medium,
                <span style="color: #a31515">"Configuring reusable content item ({0}) on site ({1})..."</span>,
                title,
                site.Url);

            <span style="color: #0000ff">if</span> (<span style="color: #0000ff">string</span>.IsNullOrEmpty(contentCategory) == <span style="color: #0000ff">true</span>)
            {
                <span style="color: #2b91af">SPLogger</span>.Log(
                    <span style="color: #2b91af">LogCategory</span>.Configuration,
                    <span style="color: #2b91af">TraceSeverity</span>.Medium,
                    <span style="color: #a31515">"The content category was not specified, using default"</span>
                        + <span style="color: #a31515">" value (None)"</span>,
                    title);

                contentCategory = <span style="color: #a31515">"None"</span>;
            }

            <span style="color: #0000ff">const string</span> rootFolderUrl = <span style="color: #a31515">"ReusableContent"</span>;

            <span style="color: #2b91af">SPList</span> reusableContentList = <span style="color: #2b91af">SharePointListHelper</span>.FindListByRootFolderUrl(
                site.RootWeb.Lists,
                rootFolderUrl);

            <span style="color: #0000ff">if</span> (reusableContentList == <span style="color: #0000ff">null</span>)
            {
                <span style="color: #0000ff">string</span> message = <span style="color: #0000ff">string</span>.Format(
                    <span style="color: #2b91af">CultureInfo</span>.CurrentCulture,
                    <span style="color: #a31515">"The list ({0}) could not be found on the site ({1})."</span>,
                    rootFolderUrl,
                    site.Url);

                <span style="color: #0000ff">throw new</span><span style="color: #2b91af"> InvalidOperationException</span>(message);
            }

            EnsureReusableContentCategoryExists(reusableContentList, contentCategory);

            <span style="color: #0000ff">string</span> camlQuery =
                <span style="color: #a31515">"&lt;Where&gt;&lt;Eq&gt;&lt;FieldRef Name='Title'/&gt;&lt;Value Type='Text'&gt;"</span>
                    + title + <span style="color: #a31515">"&lt;/Value&gt;&lt;/Eq&gt;&lt;/Where&gt;"</span>;

            <span style="color: #2b91af">SPListItem</span> listItem = <span style="color: #2b91af">SharePointListHelper</span>.FindUniqueListItem(
                reusableContentList,
                camlQuery);

            <span style="color: #0000ff">if</span> (listItem == <span style="color: #0000ff">null</span>)
            {
                <span style="color: #2b91af">SPLogger</span>.Log(
                    <span style="color: #2b91af">LogCategory</span>.Configuration,
                    <span style="color: #2b91af">TraceSeverity</span>.Medium,
                    <span style="color: #a31515">"Adding reusable content item ({0}) to site ({1})..."</span>,
                    title,
                    site.Url);

                listItem = reusableContentList.Items.Add();
                listItem[<span style="color: #2b91af">SPBuiltInFieldId</span>.Title] = title;
                listItem[<span style="color: #2b91af">FieldId</span>.AutomaticUpdate] = automaticUpdate;
                listItem[<span style="color: #2b91af">FieldId</span>.ReusableHtml] = reusableHtml;
                listItem[<span style="color: #2b91af">SPBuiltInFieldId</span>.Comments] = comments;
                listItem[<span style="color: #2b91af">FieldId</span>.ReusableTextType] = contentCategory;

                listItem.Update();

                <span style="color: #2b91af">SPLogger</span>.LogEvent(
                    <span style="color: #2b91af">LogCategory</span>.Configuration,
                    <span style="color: #2b91af">EventSeverity</span>.Information,
                    <span style="color: #a31515">"Successfully added reusable content item ({0}) to site"</span>
                        + <span style="color: #a31515">" ({1})."</span>,
                    title,
                    site.Url);
            }

            <span style="color: #0000ff">if</span> (listItem.HasPublishedVersion == <span style="color: #0000ff">false</span>)
            {
                <span style="color: #2b91af">SPLogger</span>.Log(
                    <span style="color: #2b91af">LogCategory</span>.Configuration,
                    <span style="color: #2b91af">TraceSeverity</span>.Medium,
                    <span style="color: #a31515">"The reusable content item ({0}) does not have a published"</span>
                        + <span style="color: #a31515">" version. Approving list item ({1}/{2})..."</span>,
                    title,
                    listItem.Web.Url,
                    listItem.Url);

                listItem.ModerationInformation.Status =
                    <span style="color: #2b91af">SPModerationStatusType</span>.Approved;

                listItem.Update();

                <span style="color: #2b91af">SPLogger</span>.LogEvent(
                    <span style="color: #2b91af">LogCategory</span>.Configuration,
                    <span style="color: #2b91af">EventSeverity</span>.Information,
                    <span style="color: #a31515">"Successfully approved reusable content item ({0})"</span>
                        + <span style="color: #a31515">" ({1}/{2})."</span>,
                    title,
                    listItem.Web.Url,
                    listItem.Url);
            }
            <span style="color: #0000ff">else</span>
            {
                <span style="color: #2b91af">SPLogger</span>.Log(
                    <span style="color: #2b91af">LogCategory</span>.Configuration,
                    <span style="color: #2b91af">TraceSeverity</span>.Medium,
                    <span style="color: #a31515">"The reusable content item ({0}) already has a"</span>
                        + <span style="color: #a31515">" published version and may have been customized, so"</span>
                        + <span style="color: #a31515">" no changes will be made to the list item"</span>
                        + <span style="color: #a31515">" ({1}/{2})."</span>,
                    title,
                    listItem.Web.Url,
                    listItem.Url);
            }

            <span style="color: #0000ff">return</span> listItem;
        }</code></pre>
</div>
<p>The method is rather long, but keep in mind that roughly half of the code above is error checking and logging. This is intended to be "Production-quality" code, not just a minimal code sample.</p>
<h3>Adding reusable content to a page</h3>
<p>Once you have the <strong>Reusable Content </strong>list item created (either manually or programmatically using code like that shown above), the next task is to add the content to a page. Since I'm assuming you already know how to do that using the out-of-the-box page editing features in SharePoint, let's see how this can be achieved through code.</p>
<p>First it is important to understand how reusable content is implemented in SharePoint (the foundation is the same in MOSS 2007 and SharePoint 2010). There are two key concepts to grasp:</p>
<ol>
<li>The "storage format" of the content in a Publishing HTML field uses a special &lt;div&gt; element as a "header" and corresponding &lt;span&gt; elements to designate the placeholders where reusable content is to be inserted (note that I'm only talking about reusable content that is specified to automatically update).</li>
<li>The "view format" of the content is generated by reading the "header" and subsequently replacing the corresponding placeholders that follow. Hence I tend to refer to this as the "expanded" HTML. In <em>most</em> cases,&nbsp; you can get the expanded HTML using out-of-the-box SharePoint functionality. [In part 3 of this series, I'll discuss when you can't get it that way and explain the not-so-elegant workaround that I came up with.]</li>
</ol>
<p>Here is a sample of the HTML content in "storage format":</p>
<div class="codeBlock">
<pre><code><span style="color: #0000ff">    &lt;</span><span style="color: #800000">div</span><span style="color: #ff0000"> id</span><span style="color: #0000ff">="__publishingReusableFragmentIdSection"&gt;
        &lt;</span><span style="color: #800000">a</span><span style="color: #ff0000"> href</span><span style="color: #0000ff">="/ReusableContent/1_.000"&gt;</span>a<span style="color: #0000ff">&lt;/</span><span style="color: #800000">a</span><span style="color: #0000ff">&gt;
        &lt;</span><span style="color: #800000">a</span><span style="color: #ff0000"> href</span><span style="color: #0000ff">="/ReusableContent/3_.000"&gt;</span>a<span style="color: #0000ff">&lt;/</span><span style="color: #800000">a</span><span style="color: #0000ff">&gt;
    &lt;/</span><span style="color: #800000">div</span><span style="color: #0000ff">&gt;
    &lt;</span><span style="color: #800000">p</span><span style="color: #0000ff">&gt;</span>
        Here is some reusable content...<span style="color: #0000ff">&lt;/</span><span style="color: #800000">p</span><span style="color: #0000ff">&gt;
    &lt;</span><span style="color: #800000">p</span><span style="color: #0000ff">&gt;
        &lt;</span><span style="color: #800000">span</span><span style="color: #ff0000"> id</span><span style="color: #0000ff">="__publishingReusableFragment"&gt;&lt;/</span><span style="color: #800000">span</span><span style="color: #0000ff">&gt;
    &lt;/</span><span style="color: #800000">p</span><span style="color: #0000ff">&gt;
    &lt;</span><span style="color: #800000">p</span><span style="color: #0000ff">&gt;</span>
        ...and here is some more:<span style="color: #0000ff">&lt;/</span><span style="color: #800000">p</span><span style="color: #0000ff">&gt;
    &lt;</span><span style="color: #800000">p</span><span style="color: #0000ff">&gt;
        &lt;</span><span style="color: #800000">span</span><span style="color: #ff0000"> id</span><span style="color: #0000ff">="__publishingReusableFragment"&gt;&lt;/</span><span style="color: #800000">span</span><span style="color: #0000ff">&gt;
    &lt;/</span><span style="color: #800000">p</span><span style="color: #0000ff">&gt;</span></code></pre>
</div>
<p>The corresponding "view format" is shown below:</p>
<div class="codeBlock">
<pre><code><span style="color: #0000ff">    &lt;</span><span style="color: #800000">p</span><span style="color: #0000ff">&gt;</span>
        Here is some reusable content...<span style="color: #0000ff">&lt;/</span><span style="color: #800000">p</span><span style="color: #0000ff">&gt;
    &lt;</span><span style="color: #800000">p</span><span style="color: #0000ff">&gt;
        &lt;</span><span style="color: #800000">span</span><span style="color: #ff0000"> class</span><span style="color: #0000ff">="ms-rtestate-read  ms-reusableTextView"</span><span style="color: #ff0000">
            contenteditable</span><span style="color: #0000ff">="false"</span><span style="color: #ff0000"> id</span><span style="color: #0000ff">="__publishingReusableFragment"</span><span style="color: #ff0000">
            fragmentid</span><span style="color: #0000ff">="/ReusableContent/1_.000"&gt;</span>
            Copyright&copy; 2009 Contoso Corporation - All Rights
            Reserved<span style="color: #0000ff">&lt;/</span><span style="color: #800000">span</span><span style="color: #0000ff">&gt;
    &lt;/</span><span style="color: #800000">p</span><span style="color: #0000ff">&gt;
    &lt;</span><span style="color: #800000">p</span><span style="color: #0000ff">&gt;</span>
        ...and here is some more:<span style="color: #0000ff">&lt;/</span><span style="color: #800000">p</span><span style="color: #0000ff">&gt;
    &lt;</span><span style="color: #800000">p</span><span style="color: #0000ff">&gt;
        &lt;</span><span style="color: #800000">span</span><span style="color: #ff0000"> class</span><span style="color: #0000ff">="ms-rtestate-read  ms-reusableTextView"</span><span style="color: #ff0000">
            contenteditable</span><span style="color: #0000ff">="false"</span><span style="color: #ff0000"> id</span><span style="color: #0000ff">="__publishingReusableFragment"</span><span style="color: #ff0000">
            fragmentid</span><span style="color: #0000ff">="/ReusableContent/3_.000"&gt;
            &lt;</span><span style="color: #800000">em</span><span style="color: #0000ff">&gt;</span><span style="color: #ff0000">&amp;quot;</span>Example quotation<span style="color: #ff0000">&amp;quot;</span><span style="color: #0000ff">&lt;/</span><span style="color: #800000">em</span><span style="color: #0000ff">&gt;
        &lt;/</span><span style="color: #800000">span</span><span style="color: #0000ff">&gt;
    &lt;/</span><span style="color: #800000">p</span><span style="color: #0000ff">&gt;</span></code></pre>
</div>
<blockquote class="note">
<div class="noteTitle"><strong>Note</strong></div>
<div>I have no idea why the out-of-the-box <strong>Byline</strong> and <strong>Quote</strong> reusable content items in SharePoint 2010 specify <strong>Automatic Update</strong> = <strong>Yes</strong>. I can see the reasoning for enabling automatic update of the default <strong>Copyright </strong>item, but these other two baffle me.</div>
</blockquote>
<p>Look again at the sample "storage format" HTML above. Notice how the reusable content placeholders (i.e. the &lt;span&gt; elements) do not specify which <strong>Reusable Content </strong>list item to render. Rather, the list item is determined based on the position of the placeholder (relative to other placeholders) -- which is matched to the corresponding item specified in the "header" (by index).</p>
<p>In other words, if you were to swap the order of the &lt;a&gt; elements in the "header"...</p>
<div class="codeBlock">
<pre><code><span style="color: #0000ff">    &lt;</span><span style="color: #800000">div</span><span style="color: #ff0000"> id</span><span style="color: #0000ff">="__publishingReusableFragmentIdSection"&gt;
        &lt;</span><span style="color: #800000">a</span><span style="color: #ff0000"> href</span><span style="color: #0000ff">="/ReusableContent/3_.000"&gt;</span>a<span style="color: #0000ff">&lt;/</span><span style="color: #800000">a</span><span style="color: #0000ff">&gt;
        &lt;</span><span style="color: #800000">a</span><span style="color: #ff0000"> href</span><span style="color: #0000ff">="/ReusableContent/1_.000"&gt;</span>a<span style="color: #0000ff">&lt;/</span><span style="color: #800000">a</span><span style="color: #0000ff">&gt;
    &lt;/</span><span style="color: #800000">div</span><span style="color: #0000ff">&gt;
    ...</span></code></pre>
</div>
<p>...then the order of the reusable content in the corresponding "view format" would be reversed, as shown below:</p>
<div class="codeBlock">
<pre><code><span style="color: #0000ff">    &lt;</span><span style="color: #800000">p</span><span style="color: #0000ff">&gt;</span>
        Here is some reusable content...<span style="color: #0000ff">&lt;/</span><span style="color: #800000">p</span><span style="color: #0000ff">&gt;
    &lt;</span><span style="color: #800000">p</span><span style="color: #0000ff">&gt;
        &lt;</span><span style="color: #800000">span</span><span style="color: #ff0000"> class</span><span style="color: #0000ff">="ms-rtestate-read  ms-reusableTextView"</span><span style="color: #ff0000">
            contenteditable</span><span style="color: #0000ff">="false"</span><span style="color: #ff0000"> id</span><span style="color: #0000ff">="Span1"</span><span style="color: #ff0000">
            fragmentid</span><span style="color: #0000ff">="/ReusableContent/3_.000"&gt;
            &lt;</span><span style="color: #800000">em</span><span style="color: #0000ff">&gt;</span><span style="color: #ff0000">&amp;quot;</span>Example quotation<span style="color: #ff0000">&amp;quot;</span><span style="color: #0000ff">&lt;/</span><span style="color: #800000">em</span><span style="color: #0000ff">&gt;
        &lt;/</span><span style="color: #800000">span</span><span style="color: #0000ff">&gt;
    &lt;/</span><span style="color: #800000">p</span><span style="color: #0000ff">&gt;
    &lt;</span><span style="color: #800000">p</span><span style="color: #0000ff">&gt;</span>
        ...and here is some more:<span style="color: #0000ff">&lt;/</span><span style="color: #800000">p</span><span style="color: #0000ff">&gt;
    &lt;</span><span style="color: #800000">p</span><span style="color: #0000ff">&gt;
        &lt;</span><span style="color: #800000">span</span><span style="color: #ff0000"> class</span><span style="color: #0000ff">="ms-rtestate-read  ms-reusableTextView"</span><span style="color: #ff0000">
            contenteditable</span><span style="color: #0000ff">="false"</span><span style="color: #ff0000"> id</span><span style="color: #0000ff">="__publishingReusableFragment"</span><span style="color: #ff0000">
            fragmentid</span><span style="color: #0000ff">="/ReusableContent/1_.000"&gt;</span>
            Copyright&copy; 2009 Contoso Corporation - All Rights
            Reserved<span style="color: #0000ff">&lt;/</span><span style="color: #800000">span</span><span style="color: #0000ff">&gt;
    &lt;/</span><span style="color: #800000">p</span><span style="color: #0000ff">&gt;</span></code></pre>
</div>
<p>This actually makes the code for inserting reusable content into Publishing HTML fields significantly more complex than it would be if the "storage format" specified something like the following instead:</p>
<div class="codeBlock">
<pre><code><span style="color: #0000ff">    &lt;</span><span style="color: #800000">div</span><span style="color: #ff0000"> id</span><span style="color: #0000ff">="__publishingReusableFragmentIdSection" /&gt;
    &lt;</span><span style="color: #800000">p</span><span style="color: #0000ff">&gt;</span>
        Here is some reusable content...<span style="color: #0000ff">&lt;/</span><span style="color: #800000">p</span><span style="color: #0000ff">&gt;
    &lt;</span><span style="color: #800000">p</span><span style="color: #0000ff">&gt;
        &lt;</span><span style="color: #800000">span</span><span style="color: #ff0000"> id</span><span style="color: #0000ff">="__publishingReusableFragment"&gt;
            &lt;</span><span style="color: #800000">a</span><span style="color: #ff0000"> href</span><span style="color: #0000ff">="/ReusableContent/1_.000"&gt;</span>a<span style="color: #0000ff">&lt;/</span><span style="color: #800000">a</span><span style="color: #0000ff">&gt;
        &lt;/</span><span style="color: #800000">span</span><span style="color: #0000ff">&gt;
    &lt;/</span><span style="color: #800000">p</span><span style="color: #0000ff">&gt;
    &lt;</span><span style="color: #800000">p</span><span style="color: #0000ff">&gt;</span>
        ...and here is some more:<span style="color: #0000ff">&lt;/</span><span style="color: #800000">p</span><span style="color: #0000ff">&gt;
    &lt;</span><span style="color: #800000">p</span><span style="color: #0000ff">&gt;
        &lt;</span><span style="color: #800000">span</span><span style="color: #ff0000"> id</span><span style="color: #0000ff">="__publishingReusableFragment"&gt;
            &lt;</span><span style="color: #800000">a</span><span style="color: #ff0000"> href</span><span style="color: #0000ff">="/ReusableContent/3_.000"&gt;</span>a<span style="color: #0000ff">&lt;/</span><span style="color: #800000">a</span><span style="color: #0000ff">&gt;
        &lt;/</span><span style="color: #800000">span</span><span style="color: #0000ff">&gt;
    &lt;/</span><span style="color: #800000">p</span><span style="color: #0000ff">&gt;</span></code></pre>
</div>
<p>Rather than simply listing the code for inserting reusable content into a page (which you can easily access in the attached sample solution), start by reviewing some of the unit tests that I created when developing the <strong>InsertReusableContentIntoHtmlField </strong>method:</p>
<div class="codeBlock">
<pre><code><span style="color: #808080">        /// &lt;summary&gt;
        ///</span><span style="color: #008000"> Basic test for appending reusable content to an HTML field.</span><span style="color: #808080">
        /// &lt;/summary&gt;</span>
        [<span style="color: #2b91af">TestMethod</span>()]
        <span style="color: #0000ff">public void</span> InsertReusableContentIntoHtmlField001()
        {
            <span style="color: #0000ff">const string</span> reusableContentListItemUrl = <span style="color: #a31515">"/ReusableContent/1_.000"</span>;
            <span style="color: #0000ff">const string</span> htmlFieldContent = <span style="color: #0000ff">null</span>;

            <span style="color: #0000ff">const string</span> expected =
                <span style="color: #a31515">"&lt;div id=\"__publishingReusableFragmentIdSection\"&gt;"</span>
                        + <span style="color: #a31515">"&lt;a href=\"/ReusableContent/1_.000\"&gt;a&lt;/a&gt;"</span>
                    + <span style="color: #a31515">"&lt;/div&gt;"</span>
                    + <span style="color: #a31515">"&lt;span id=\"__publishingReusableFragment\"&gt;&lt;/span&gt;"</span>;

            <span style="color: #0000ff">string</span> actual =
                <span style="color: #2b91af">SharePointHtmlFieldHelper</span>.InsertReusableContentIntoHtmlField(
                    reusableContentListItemUrl,
                    htmlFieldContent);

            <span style="color: #2b91af">Assert</span>.AreEqual(expected, actual);
        }

        <span style="color: #808080">/// &lt;summary&gt;
        ///</span><span style="color: #008000"> Basic test for appending reusable content to an HTML field which</span><span style="color: #808080">
        ///</span><span style="color: #008000"> already contains another piece of reusable content.</span><span style="color: #808080">
        /// &lt;/summary&gt;</span>
        [<span style="color: #2b91af">TestMethod</span>()]
        <span style="color: #0000ff">public void</span> InsertReusableContentIntoHtmlField002()
        {
            <span style="color: #0000ff">const string</span> reusableContentListItemUrl = <span style="color: #a31515">"/ReusableContent/2_.000"</span>;
            <span style="color: #0000ff">const string</span> htmlFieldContent =
                <span style="color: #a31515">"&lt;div id=\"__publishingReusableFragmentIdSection\"&gt;"</span>
                        + <span style="color: #a31515">"&lt;a href=\"/ReusableContent/1_.000\"&gt;a&lt;/a&gt;"</span>
                    + <span style="color: #a31515">"&lt;/div&gt;"</span>
                    + <span style="color: #a31515">"&lt;span id=\"__publishingReusableFragment\"&gt;&lt;/span&gt;"</span>;

            <span style="color: #0000ff">const string</span> expected =
                <span style="color: #a31515">"&lt;div id=\"__publishingReusableFragmentIdSection\"&gt;"</span>
                        + <span style="color: #a31515">"&lt;a href=\"/ReusableContent/1_.000\"&gt;a&lt;/a&gt;"</span>
                        + <span style="color: #a31515">"&lt;a href=\"/ReusableContent/2_.000\"&gt;a&lt;/a&gt;"</span>
                    + <span style="color: #a31515">"&lt;/div&gt;"</span>
                    + <span style="color: #a31515">"&lt;span id=\"__publishingReusableFragment\"&gt;&lt;/span&gt;"</span>
                    + <span style="color: #a31515">"&lt;span id=\"__publishingReusableFragment\"&gt;&lt;/span&gt;"</span>;

            <span style="color: #0000ff">string</span> actual =
                <span style="color: #2b91af">SharePointHtmlFieldHelper</span>.InsertReusableContentIntoHtmlField(
                    reusableContentListItemUrl,
                    htmlFieldContent);

            <span style="color: #2b91af">Assert</span>.AreEqual(expected, actual);
        }

        <span style="color: #808080">/// &lt;summary&gt;
        ///</span><span style="color: #008000"> Basic test for inserting reusable content into an HTML field at a</span><span style="color: #808080">
        ///</span><span style="color: #008000"> specific location.</span><span style="color: #808080">
        /// &lt;/summary&gt;</span>
        [<span style="color: #2b91af">TestMethod</span>()]
        <span style="color: #0000ff">public void</span> InsertReusableContentIntoHtmlField003()
        {
            <span style="color: #0000ff">const string</span> placeholder = <span style="color: #a31515">"{TODO: Insert reusable content here}"</span>;

            <span style="color: #0000ff">const string</span> reusableContentListItemUrl = <span style="color: #a31515">"/ReusableContent/1_.000"</span>;
            <span style="color: #0000ff">const string</span> htmlFieldContent = <span style="color: #a31515">"&lt;p&gt;"</span> + placeholder + <span style="color: #a31515">"&lt;/p&gt;"</span>;

            <span style="color: #0000ff">const string</span> expected =
                <span style="color: #a31515">"&lt;div id=\"__publishingReusableFragmentIdSection\"&gt;"</span>
                        + <span style="color: #a31515">"&lt;a href=\"/ReusableContent/1_.000\"&gt;a&lt;/a&gt;"</span>
                    + <span style="color: #a31515">"&lt;/div&gt;"</span>
                    + <span style="color: #a31515">"&lt;p&gt;&lt;span id=\"__publishingReusableFragment\"&gt;&lt;/span&gt;&lt;/p&gt;"</span>;

            <span style="color: #0000ff">string</span> actual =
                <span style="color: #2b91af">SharePointHtmlFieldHelper</span>.InsertReusableContentIntoHtmlField(
                    reusableContentListItemUrl,
                    htmlFieldContent,
                    placeholder);

            <span style="color: #2b91af">Assert</span>.AreEqual(expected, actual);
        }

        <span style="color: #808080">/// &lt;summary&gt;
        ///</span><span style="color: #008000"> Basic test for inserting reusable content into an HTML field which</span><span style="color: #808080">
        ///</span><span style="color: #008000"> already contains other pieces of reusable content.</span><span style="color: #808080">
        /// &lt;/summary&gt;</span>
        [<span style="color: #2b91af">TestMethod</span>()]
        <span style="color: #0000ff">public void</span> InsertReusableContentIntoHtmlField004()
        {
            <span style="color: #0000ff">const string</span> placeholder = <span style="color: #a31515">"{TODO: Insert reusable content here}"</span>;

            <span style="color: #0000ff">const string</span> reusableContentListItemUrl = <span style="color: #a31515">"/ReusableContent/3_.000"</span>;
            <span style="color: #0000ff">const string</span> htmlFieldContent =
                <span style="color: #a31515">"&lt;div id=\"__publishingReusableFragmentIdSection\"&gt;"</span>
                        + <span style="color: #a31515">"&lt;a href=\"/ReusableContent/1_.000\"&gt;a&lt;/a&gt;"</span>
                        + <span style="color: #a31515">"&lt;a href=\"/ReusableContent/2_.000\"&gt;a&lt;/a&gt;"</span>
                    + <span style="color: #a31515">"&lt;/div&gt;"</span>
                    + <span style="color: #a31515">"&lt;div id='reusableContent1'&gt;"</span>
                        + <span style="color: #a31515">"&lt;span id=\"__publishingReusableFragment\"&gt;&lt;/span&gt;"</span>
                    + <span style="color: #a31515">"&lt;/div&gt;"</span>
                    + <span style="color: #a31515">"&lt;div id='reusableContent3'&gt;"</span> + placeholder + <span style="color: #a31515">"&lt;/div&gt;"</span>
                    + <span style="color: #a31515">"&lt;div id='reusableContent2'&gt;"</span>
                        + <span style="color: #a31515">"&lt;span id=\"__publishingReusableFragment\"&gt;&lt;/span&gt;"</span>
                    + <span style="color: #a31515">"&lt;/div&gt;"</span>;

            <span style="color: #0000ff">const string</span> expected =
                <span style="color: #a31515">"&lt;div id=\"__publishingReusableFragmentIdSection\"&gt;"</span>
                        + <span style="color: #a31515">"&lt;a href=\"/ReusableContent/1_.000\"&gt;a&lt;/a&gt;"</span>
                        + <span style="color: #a31515">"&lt;a href=\"/ReusableContent/3_.000\"&gt;a&lt;/a&gt;"</span>
                        + <span style="color: #a31515">"&lt;a href=\"/ReusableContent/2_.000\"&gt;a&lt;/a&gt;"</span>
                    + <span style="color: #a31515">"&lt;/div&gt;"</span>
                    + <span style="color: #a31515">"&lt;div id='reusableContent1'&gt;"</span>
                        + <span style="color: #a31515">"&lt;span id=\"__publishingReusableFragment\"&gt;&lt;/span&gt;"</span>
                    + <span style="color: #a31515">"&lt;/div&gt;"</span>
                    + <span style="color: #a31515">"&lt;div id='reusableContent3'&gt;"</span>
                        + <span style="color: #a31515">"&lt;span id=\"__publishingReusableFragment\"&gt;&lt;/span&gt;"</span>
                    + <span style="color: #a31515">"&lt;/div&gt;"</span>
                    + <span style="color: #a31515">"&lt;div id='reusableContent2'&gt;"</span>
                        + <span style="color: #a31515">"&lt;span id=\"__publishingReusableFragment\"&gt;&lt;/span&gt;"</span>
                    + <span style="color: #a31515">"&lt;/div&gt;"</span>;

            <span style="color: #0000ff">string</span> actual =
                <span style="color: #2b91af">SharePointHtmlFieldHelper</span>.InsertReusableContentIntoHtmlField(
                    reusableContentListItemUrl,
                    htmlFieldContent,
                    placeholder);

            <span style="color: #2b91af">Assert</span>.AreEqual(expected, actual);
        }</code></pre>
</div>
<blockquote class="note">
<div class="noteTitle"><strong>Note</strong></div>
<div>Initially, I added the code for inserting reusable content into a page to the <strong>SharePointPublishingHelper </strong>class. However, I ended up refactoring this code into the new <strong>SharePointHtmlFieldHelper </strong>class. I'll discuss this new class in more detail in part 3 of this series.</div>
</blockquote>
<h3>Sample "Reusable Content" solution for SharePoint Server 2010</h3>
<p>I've attached a complete Visual Studio 2010 solution so you can see just how easy it is to automatically create reusable content and add it to a page using the provided helper classes (for example, upon activation of a feature).</p>
<p>If you've deployed any of my other sample SharePoint solutions, you'll find this one just as easy.</p>
<p>Here are the instructions to deploy the sample to your own SharePoint environment. First, download the attachment and unzip the files. Then you simply need to create a few domain users and run a handful of PowerShell scripts, as described below.</p>
<h4>To deploy the sample solution to SharePoint 2010:</h4>
<ol>
<li>Create three service accounts for the Fabrikam Demo site: 
<ul>
<li><strong>{DOMAIN}\svc-web-fabrikam-dev</strong> - used as the application pool identity for the new "Fabrikam Demo" site</li>
<li><strong>{DOMAIN}\svc-sp-psr-dev</strong> - object cache user account providing Full Read access to Web applications (<a href="https://technet.microsoft.com/en-us/library/ff758656.aspx">https://technet.microsoft.com/en-us/library/ff758656.aspx</a>)</li>
<li><strong>{DOMAIN}\svc-sp-psu-dev</strong> - object cache user account providing Full Control access to Web applications</li>
</ul>
</li>
<li>On the <strong>Start</strong> menu, click <strong>All Programs</strong>, click <strong>Microsoft SharePoint 2010 Products</strong>, right-click <strong>SharePoint 2010 Management Shell</strong>, and then click <strong>Run as administrator</strong>. If prompted by User Account Control to allow the program to make changes to the computer, click <strong>Yes</strong>.</li>
<li>From the Windows PowerShell command prompt, change to the directory containing the deployment scripts (e.g. C:\NotBackedUp\Fabrikam\Demo\Dev\SharePointReusableContent\Source\DeploymentFiles\Scripts), and run the following commands:<br />
<div class="consoleBlock">$env:FABRIKAM_DEMO_URL = "<a href="https://tugboatcoffee-local">https://fabrikam-local</a>"<br />$env:FABRIKAM_DEMO_BUILD_CONFIGURATION = "Debug"<br />&amp; '.\Add Event Log Sources.ps1'<br />&amp; '.\Create Web Application.ps1'<br />&amp; '.\Configure Object Cache User Accounts.ps1'<br />&amp; '.\Create Site Collections.ps1'<br />&amp; '.\Enable Anonymous Access.ps1'<br />&amp; '.\Add Solutions.ps1'<br />&amp; '.\Deploy Solutions.ps1'<br />&amp; '.\Activate Features.ps1'</div>
</li>
</ol>
<blockquote class="note">
<div class="noteTitle"><strong>Note</strong></div>
<div>Technically, you don't have to set the environment variables (and use the "-dev" accounts). However, I recommend this in order to bypass SharePoint timer jobs when deploying the WSPs.</div>
</blockquote>
<p>At this point you should be able to modify your hosts file accordingly and browse to either <a href="https://www-local.tugboatcoffee.com">https://www-local.fabrikam.com</a> (to view the site as an anonymous user) or <a href="https://tugboatcoffee-local">https://fabrikam-local</a> (to view the site as an administrator).</p>
<p>You can then click the <strong>Reusable Content Sample </strong>link on the home page of the site to view the sample page. Once you have verified the page renders as expected, modify the corresponding item in the <strong>Reusable Content </strong>list and verify the page is "automatically updated" accordingly. [Now that you understand how reusable content works in SharePoint, you know the page isn't "automatically updated" at all. Rather, the dynamically generated "view format" renders the updated content.]</p>
<p>In <a href="https://blogs.msdn.com/b/jjameson/archive/2011/04/14/reusable-content-in-sharepoint-publishing-html-fields-part-3.aspx">part 3 of this series</a>, I'll discuss various ways of accessing the "expanded" HTML content (a.k.a. the "view format.")</p>
<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Components.PostAttachments/00/10/15/35/60/Fabrikam%20Demo%20-%20SharePointReusableContent.zip" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-components-postattachments/00-10-15-35-60/Fabrikam-Demo-_2D00_-SharePointReusableContent.zip">Fabrikam Demo - SharePointReusableContent.zip</a></p>]]></content:encoded>
    </item>
  </channel>
</rss>