0

I added Custom display template by using visual studio, Our custom display template is added but js files are not created. If I Unpublished the file then I publish again means it's creating , I tired

<Module Name="_catalogs" Url="_catalogs/masterpage/Display Templates/Search">
    <File Path="OOBWebparts\Search\DisplayTemplate\Search\Item_Link.html" Url="Item_Link.html" Type="GhostableInLibrary" />
  </Module>
  <Module Name="_catalogs" Url="_catalogs/masterpage/Display Templates/PlaybookSearch">
    <File Path="OOBWebparts\Search\DisplayTemplate\PlaybookSearch\Searchfilter.html" Url="Searchfilter.html" Type="GhostableInLibrary" />
  </Module>
  <Module Name="_catalogs" Url="_catalogs/masterpage/Display Templates/Search">
    <File Path="OOBWebparts\Team\DisplayTemplate\Item_Person_FPS.HTML" Url="Item_Person_FPS.HTML" Type="GhostableInLibrary"/>
  </Module>

Please suggest me to automate this process?

  • can you post some code which you have used to add display template ? – Gautam Sheth Nov 17 '16 at 4:58
  • Hi I Updated the question @GautamSheth – Suriya Nov 17 '16 at 5:09
2

Write your module code as below. Important thing to note is to keep the level as Draft:

<Module Name="_catalogs" Url="_catalogs/masterpage/Display Templates/Search">
    <File Path="OOBWebparts\Search\DisplayTemplate\Search\Item_Link.html" Level="Draft" Url="Item_Link.html" Type="GhostableInLibrary" />
  </Module>
  <Module Name="_catalogs" Url="_catalogs/masterpage/Display Templates/PlaybookSearch">
    <File Path="OOBWebparts\Search\DisplayTemplate\PlaybookSearch\Searchfilter.html" Level="Draft" Url="Searchfilter.html" Type="GhostableInLibrary" />
  </Module>
  <Module Name="_catalogs" Url="_catalogs/masterpage/Display Templates/Search">
    <File Path="OOBWebparts\Team\DisplayTemplate\Item_Person_FPS.HTML" Url="Item_Person_FPS.HTML" Level="Draft"  Type="GhostableInLibrary"/>
  </Module>

Now in the feature that deploys the display template, you need to checkout and publish the display template which will generate the js file. So for that you need to write code in the feature activated event as below:

private string[] displayTemplateNames = { "Item_Link.html","Searchfilter.html","Item_Person_FPS.HTML" };

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
    try
    {
        using (SPSite site = properties.Feature.Parent as SPSite)
        {

            using (SPWeb web = site.RootWeb)
            {
                //get the master pages gallery
                var gallery = web.GetCatalog(SPListTemplateType.MasterPageCatalog);

                foreach (var displayTemplate in displayTemplateNames)
                {
                    var url = SPUtility.ConcatUrls(gallery.RootFolder.Url, "Display Templates/Search");
                    url = SPUtility.ConcatUrls(url, displayTemplate);
                    //get the file
                    var fileOrFolder = web.GetFile(url);

                    if (fileOrFolder != null && fileOrFolder is SPFile)
                    {
                        var file = (SPFile)fileOrFolder;
                        if (file.Exists)
                        {
                            //determine if the gallery requires files to be checked out before editing
                            //if so, check this one out
                            if (gallery.ForceCheckout)
                                file.CheckOut();

                            //make a simple change
                            file.Item["Title"] = file.Item.Title;
                            file.Item.Update();

                            //if check out required, check it in
                            if (gallery.ForceCheckout)
                                file.CheckIn(string.Empty);
                            //if the gallery has minor versioning enabled, publish a major
                            if (gallery.EnableMinorVersions)
                                file.Publish(string.Empty);
                            //if the gallery required approval, approve it
                            if (gallery.EnableModeration)
                                file.Approve(string.Empty);
                        }
                    }
                }
            }
        }
    }
    catch (Exception ex)
    {
        //deactivate the feature
        var site = (SPSite)properties.Feature.Parent;
        site.Features.Remove(properties.Feature.DefinitionId);

        SPUtility.TransferToErrorPage(ex.Message);
    }
}

It's important to set the level to DRAFT (in the module) because if you don't the js won't get created. And then with the event receiver you get it published and functional.

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.