protected override void ExecuteCmdlet()
{
SelectedWeb.EnsureProperty(w => w.Url);
ProvisioningTemplate provisioningTemplate;
FileConnectorBase fileConnector;
if (MyInvocation.BoundParameters.ContainsKey("Path"))
{
bool templateFromFileSystem = !Path.ToLower().StartsWith("http");
string templateFileName = System.IO.Path.GetFileName(Path);
if (templateFromFileSystem)
{
if (!System.IO.Path.IsPathRooted(Path))
{
Path = System.IO.Path.Combine(SessionState.Path.CurrentFileSystemLocation.Path, Path);
}
if (!string.IsNullOrEmpty(ResourceFolder))
{
if (!System.IO.Path.IsPathRooted(ResourceFolder))
{
ResourceFolder = System.IO.Path.Combine(SessionState.Path.CurrentFileSystemLocation.Path,
ResourceFolder);
}
}
var fileInfo = new FileInfo(Path);
fileConnector = new FileSystemConnector(fileInfo.DirectoryName, "");
}
else
{
Uri fileUri = new Uri(Path);
var webUrl = Microsoft.SharePoint.Client.Web.WebUrlFromFolderUrlDirect(ClientContext, fileUri);
var templateContext = ClientContext.Clone(webUrl.ToString());
var library = Path.ToLower().Replace(templateContext.Url.ToLower(), "").TrimStart('/');
var idx = library.IndexOf("/", StringComparison.Ordinal);
library = library.Substring(0, idx);
// This syntax creates a SharePoint connector regardless we have the -InputInstance argument or not
fileConnector = new SharePointConnector(templateContext, templateContext.Url, library);
}
// If we don't have the -InputInstance parameter, we load the template from the source connector
Stream stream = fileConnector.GetFileStream(templateFileName);
var isOpenOfficeFile = IsOpenOfficeFile(stream);
XMLTemplateProvider provider;
if (isOpenOfficeFile)
{
provider = new XMLOpenXMLTemplateProvider(new OpenXMLConnector(templateFileName, fileConnector));
templateFileName = templateFileName.Substring(0, templateFileName.LastIndexOf(".", StringComparison.Ordinal)) + ".xml";
}
else
{
if (templateFromFileSystem)
{
provider = new XMLFileSystemTemplateProvider(fileConnector.Parameters[FileConnectorBase.CONNECTIONSTRING] + "", "");
}
else
{
throw new NotSupportedException("Only .pnp package files are supported from a SharePoint library");
}
}
provisioningTemplate = provider.GetTemplate(templateFileName, TemplateProviderExtensions);
if (provisioningTemplate == null)
{
// If we don't have the template, raise an error and exit
WriteError(new ErrorRecord(new Exception("The -Path parameter targets an invalid repository or template object."), "WRONG_PATH", ErrorCategory.SyntaxError, null));
return;
}
if (isOpenOfficeFile)
{
provisioningTemplate.Connector = provider.Connector;
}
else
{
if (ResourceFolder != null)
{
var fileSystemConnector = new FileSystemConnector(ResourceFolder, "");
provisioningTemplate.Connector = fileSystemConnector;
}
else
{
provisioningTemplate.Connector = provider.Connector;
}
}
}
else
{
if (MyInvocation.BoundParameters.ContainsKey("GalleryTemplateId"))
{
provisioningTemplate = GalleryHelper.GetTemplate(GalleryTemplateId);
}
else
{
provisioningTemplate = InputInstance;
}
if (ResourceFolder != null)
{
var fileSystemConnector = new FileSystemConnector(ResourceFolder, "");
provisioningTemplate.Connector = fileSystemConnector;
}
else
{
if (Path != null)
{
if (!System.IO.Path.IsPathRooted(Path))
{
Path = System.IO.Path.Combine(SessionState.Path.CurrentFileSystemLocation.Path, Path);
}
}
else
{
Path = SessionState.Path.CurrentFileSystemLocation.Path;
}
var fileInfo = new FileInfo(Path);
fileConnector = new FileSystemConnector(fileInfo.DirectoryName, "");
provisioningTemplate.Connector = fileConnector;
}
}
if (Parameters != null)
{
foreach (var parameter in Parameters.Keys)
{
if (provisioningTemplate.Parameters.ContainsKey(parameter.ToString()))
{
provisioningTemplate.Parameters[parameter.ToString()] = Parameters[parameter].ToString();
}
else
{
provisioningTemplate.Parameters.Add(parameter.ToString(), Parameters[parameter].ToString());
}
}
}
var applyingInformation = new ProvisioningTemplateApplyingInformation();
if (MyInvocation.BoundParameters.ContainsKey("Handlers"))
{
applyingInformation.HandlersToProcess = Handlers;
}
if (MyInvocation.BoundParameters.ContainsKey("ExcludeHandlers"))
{
foreach (var handler in (Handlers[])Enum.GetValues(typeof(Handlers)))
{
if (!ExcludeHandlers.Has(handler) && handler != Handlers.All)
{
Handlers = Handlers | handler;
}
}
applyingInformation.HandlersToProcess = Handlers;
}
if (ExtensibilityHandlers != null)
{
applyingInformation.ExtensibilityHandlers = ExtensibilityHandlers.ToList();
}
applyingInformation.ProgressDelegate = (message, step, total) =>
{
WriteProgress(new ProgressRecord(0, $"Applying template to {SelectedWeb.Url}", message) { PercentComplete = (100 / total) * step });
};
applyingInformation.MessagesDelegate = (message, type) =>
{
if (type == ProvisioningMessageType.Warning)
{
WriteWarning(message);
}
};
applyingInformation.OverwriteSystemPropertyBagValues = OverwriteSystemPropertyBagValues;
SelectedWeb.ApplyProvisioningTemplate(provisioningTemplate, applyingInformation);
WriteProgress(new ProgressRecord(0, $"Applying template to {SelectedWeb.Url}", " ") { RecordType = ProgressRecordType.Completed });
}