5

I created in VS2015 a new App. VS will create automatically an app project and an MVC web project. Inside my MVC web project I have an template xml file as follow:

<?xml version="1.0"?>
<pnp:Provisioning xmlns:pnp="http://schemas.dev.office.com/PnP/2016/05/ProvisioningSchema">
  <pnp:Preferences Generator="OfficeDevPnP.Core, Version=2.7.1609.3, Culture=neutral, PublicKeyToken=3751622786b357c2" />
  <pnp:Templates ID="CONTAINER-TEMPLATE-819C564282654030A12C73A642964170">
    <pnp:ProvisioningTemplate ID="TEMPLATE-819C564282654030A12C73A642964170" Version="1" BaseSiteTemplate="BLANKINTERNET#0">
      <pnp:WebSettings NoCrawl="false"  WelcomePage="Paginas/Home.aspx" SiteLogo="" AlternateCSS="" MasterPageUrl="{masterpagecatalog}/BM.Intern.Werkruimte/Masterpage.Intern.Werkruimte.master" CustomMasterPageUrl="{masterpagecatalog}/BM.Intern.Werkruimte/Masterpage.Intern.Werkruimte.master" />
      <pnp:RegionalSettings AdjustHijriDays="0" AlternateCalendarType="None" CalendarType="Gregorian" Collation="25" FirstDayOfWeek="Monday" FirstWeekOfYear="0" LocaleId="1043" ShowWeeks="false" Time24="true" TimeZone="3" WorkDayEndHour="5:00PM" WorkDays="62" WorkDayStartHour="8:00AM" />
      <pnp:SupportedUILanguages>
        <pnp:SupportedUILanguage LCID="1043" />
      </pnp:SupportedUILanguages>
      <pnp:Files>
        <pnp:File Src="Home.aspx" Folder="{site}/Paginas" Overwrite="true">
          <pnp:Properties>
            <pnp:Property Key="ContentTypeId" Value="{contenttypeid:Welkomstpagina}" />
            <pnp:Property Key="Title" Value="Home" />
            <pnp:Property Key="PublishingPageLayout" Value="{masterpagecatalog}/BlankWebPartPage.aspx, Lege pagina met webonderdelen" />
            <pnp:Property Key="PublishingIsFurlPage" Value="False" />
            <pnp:Property Key="RobotsNoIndex" Value="False" />
            <pnp:Property Key="PublishingPageContent" Value="&lt;h1&gt;​​Welkom op de Periode!&lt;{site}h1&gt;" />
          </pnp:Properties>
        </pnp:File>
      </pnp:Files>
      <pnp:Navigation>
        <pnp:GlobalNavigation />
        <pnp:CurrentNavigation />
      </pnp:Navigation>
    </pnp:ProvisioningTemplate>
  </pnp:Templates>
</pnp:Provisioning>

In the same folder as where I have stored my template xml file I have also a blanco publishing page named Home.aspx. This is the content of Home.aspx:

<%@ Page Inherits="Microsoft.SharePoint.Publishing.TemplateRedirectionPage,Microsoft.SharePoint.Publishing,Version=15.0.0.0,Culture=neutral,PublicKeyToken=71e9bce111e9429c" %> 
<%@ Reference VirtualPath="~TemplatePageUrl" %> 
<%@ Reference VirtualPath="~masterurl/custom.master" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<html xmlns:mso="urn:schemas-microsoft-com:office:office" xmlns:msdt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882">
    <head>
        <!--[if gte mso 9]><SharePoint:CTFieldRefs runat=server Prefix="mso:" FieldList="FileLeafRef,Comments,PublishingStartDate,PublishingExpirationDate,PublishingContactEmail,PublishingContactName,PublishingContactPicture,PublishingPageLayout,PublishingVariationGroupID,PublishingVariationRelationshipLinkFieldID,PublishingRollupImage,Audience,PublishingIsFurlPage,PublishingPageImage,PublishingPageContent,SummaryLinks,SummaryLinks2,SeoBrowserTitle,SeoMetaDescription,SeoKeywords,RobotsNoIndex"><xml>
        <mso:CustomDocumentProperties>
        <mso:PublishingContact msdt:dt="string">1</mso:PublishingContact>
        <mso:PublishingIsFurlPage msdt:dt="string">0</mso:PublishingIsFurlPage>
        <mso:display_urn_x003a_schemas-microsoft-com_x003a_office_x003a_office_x0023_PublishingContact msdt:dt="string">Bart Donninger</mso:display_urn_x003a_schemas-microsoft-com_x003a_office_x003a_office_x0023_PublishingContact>
        <mso:PublishingContactPicture msdt:dt="string"></mso:PublishingContactPicture>
        <mso:RobotsNoIndex msdt:dt="string">0</mso:RobotsNoIndex>
        <mso:ContentTypeId msdt:dt="string">0x010100C568DB52D9D0A14D9B2FDCC96666E9F2007948130EC3DB064584E219954237AF390064DEA0F50FC8C147B0B6EA0636C4A7D400B00B6B5176D4C343A182CF31E3D2BE00</mso:ContentTypeId>
        <mso:Comments msdt:dt="string"></mso:Comments>
        <mso:PublishingContactEmail msdt:dt="string"></mso:PublishingContactEmail>
        <mso:RequiresRouting msdt:dt="string">False</mso:RequiresRouting>
        </mso:CustomDocumentProperties>
        </xml></SharePoint:CTFieldRefs><![endif]-->
        <title>Home</title>
   </head>
</html>

Here is the pnp CSOM code to do the ProvisioningTemplate:

private bool ApplyProvisioningTemplate(Web newWeb)
        {
            XMLTemplateProvider provider = new XMLFileSystemTemplateProvider(String.Format(@"{0}\Templates\", AppDomain.CurrentDomain.BaseDirectory), "");

            if (provider != null)
            {
                var template = provider.GetTemplate("periodeTemplate.xml");
                template.Connector = provider.Connector;

                if (template != null)
                {
                    newWeb.ApplyProvisioningTemplate(template);

                    return true;
                }
            }

            return false;
        }

I can do a build on the solution. But when I try to publish the MVC web project I get build errors:

The file '/Templates/~TemplatePageUrl' does not exist.

The file '/Templates/~masterurl/custom.master' does not exist.

Why do I got this build errors when I do a publish? When I do a build I dont get them. And what is the best practice to create a new publishing page by TemplateProvisioning?

2

It is not possible to use the SharePoint token ~TemplatePageUrl in the virtualPath.

I fixed it by my self. I renamed the Home.aspx to Home.txt. Now I can do a publish. After the publish I use gulp to create a deployment package. Inside gulp js code I rename the Home.txt back to Home.aspx. Here is my gulp js code:

gulp.task('fixPublishingPages', function () {
    gulp.src(outputFolderWeb + 'Templates/Pages/*.txt')
      .pipe(rename(function (path) {
          path.extname = ".aspx"
      }))
      .pipe(gulp.dest(outputFolderWeb + 'Templates/Pages'));
    del(outputFolderWeb + 'Templates/Pages/*.txt', { force: true });
});
2

I found another solution which is better than my previous solution. In your Visual Studio you have publish profiles. In your publish profile you can go to settings. Disable the option: Precompile during publishing. Now you can publish your MVC web project without errors about the SharePoint token in the VirtualPath.

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.