I have a project for a module, that deploys to a Sitecore site in IIS. So, my root project folder is C:\Dev\ContentExportTool\CET
(where the .sln
lives) and the site is at C:\inetpub\wwwroot\CET.dev.local
.
I have an admin page that generates a package of my module with all of the necessary files and Sitecore items. However, the paths that it generates in the package are including the full file path from the project directory, which causes the files to be placed in the wrong place when the package is installed:
- In my project (in the CET folder) I have
/sitecore/shell/example.aspx
- In my admin page, the code to add this file to the package is
fileSource.Entries.Add(MainUtil.MapPath("C:\\Dev\\ContentExportTool\\CET\\sitecore\\shell\\example.aspx"));
- When I unzip the package file that's created, the files is at under
/files/Dev/ContentExportTool/CET/sitecore/shell/example.aspx
instead of/sitecore/shell/example.aspx
- When I install the package in Sitecore, that full folder structure is added to the website root instead of
example.aspx
getting added under/sitecore/shell
My package tool worked fine in an older solution where I included the module code in a Sitecore project where the codebase root was also the IIS website root (e.g. a basic solution where I only needed to run "Build" and not deploy files to another location); I expect this is because the site running the package code had the same root path as the files. But in trying to modernize with a new Sitecore 10 solution where the code is separate from the website, the paths are no longer generated correctly.
How do I change the destination path of the files in the package so that I get /files/sitecore/shell/example.aspx
?
Here is the full code:
var packageProject = new PackageProject()
{
Metadata =
{
PackageName = "Module",
Author = "Me",
Version = txtVersion.Value
}
};
// files
var fileSource = new ExplicitFileSource();
fileSource.Name = "Files";
fileSource.Entries.Add(MainUtil.MapPath("C:\\Dev\\ContentExportTool\\CET\\sitecore\\shell\\example.aspx"));
packageProject.Sources.Add(fileSource);
packageProject.SaveProject = true;
var fileName = packageProject.Metadata.PackageName + ".zip";
var filePath = contentExportUtil.FullPackageProjectPath(fileName);
using (var writer = new PackageWriter(filePath))
{
Sitecore.Context.SetActiveSite("shell");
writer.Initialize(Installer.CreateInstallationContext());
PackageGenerator.GeneratePackage(packageProject, writer);
Sitecore.Context.SetActiveSite("website");
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", string.Format("attachment;filename={0}", fileName));
Response.ContentType = "application/zip";
byte[] data = new WebClient().DownloadData(filePath);
Response.BinaryWrite(data);
Response.Flush();
Response.End();
}