У меня есть пользовательская кнопка ленты Sitecore для импорта данных из загруженного файла.Диалог импорта работает, но когда он переходит в обратную передачу, args.HasResult возвращается как false
public abstract class ImportCommand : Command
{
protected const string XamlControlPage = "/sitecore/shell/~/xaml/Sitecore.Shell.Applications.Dialogs.Bayer.UploadFile.aspx";
private const string MasterDb = "master";
protected ISitecoreService SitecoreService;
protected IItemService ItemService;
/// <summary>
/// Executes the command in the specified context.
/// </summary>
/// <param name="context">The context.</param>
public override void Execute(CommandContext context)
{
Initialize();
Assert.ArgumentNotNull(context, "context");
if (context.Items.Length != 1)
return;
Item obj = context.Items[0];
var parameters = new NameValueCollection();
parameters["uri"] = obj.Uri.ToString();
parameters["id"] = obj.ID.ToString();
Context.ClientPage.Start(this, "RunPipeline", parameters);
}
protected void Initialize()
{
var db = Sitecore.Configuration.Factory.GetDatabase("master");
SitecoreService = new SitecoreService(db);
ItemService = new ItemService(SitecoreService);
}
protected void RunPipeline(ClientPipelineArgs args)
{
if (!args.IsPostBack)
{
var url = new UrlString(UIUtil.GetUri(XamlControlPage));
SheerResponse.ShowModalDialog(url.ToString(), "580", "300", string.Empty, true);
args.WaitForPostBack(true);
return;
}
if (!args.HasResult)
return;
try
{
var itemId = new Guid(args.Parameters["id"]);
using (new BulkUpdateContext())
{
DoStuff(itemId, args.Result);
}
SheerResponse.Alert("Import Successful. Press OK to continue.");
}
catch (Exception ex)
{
SheerResponse.ShowError(ex);
}
finally
{
// Try deleting the file
try
{
File.Delete(args.Result);
}
catch (Exception e)
{
Sitecore.Diagnostics.Log.Warn(string.Format("Unable to delete file: {0}", args.Result), e, this);
}
}
}
}
У меня есть файл UploadFile.cs, который обрабатывает диалог:
public class UploadFile : DialogPage
{
protected FileUpload fileUpload;
protected Literal ltrlResult;
protected bool IsFirstLoad
{
get { return ViewState["Upload.IsFirst"] as string != "false"; }
set { ViewState["Upload.IsFirst"] = !value ? "false" : null; }
}
protected string FilePath
{
get { return ViewState["Upload.FilePath"] as string; }
set { ViewState["Upload.FilePath"] = value; }
}
protected override void OnLoad(EventArgs e)
{
if (IsFirstLoad)
{
ltrlResult.Text = "First upload a file, then click OK.";
IsFirstLoad = false;
}
else
{
ltrlResult.Text = string.Empty;
var fileOK = false;
var path = HttpContext.Current.Server.MapPath("~/Upload/");
if (fileUpload.HasFile)
{
var fileExtension = Path.GetExtension(fileUpload.FileName).ToLower();
string[] allowedExtensions = {".csv"};
for (int i = 0; i < allowedExtensions.Length; i++)
{
if (fileExtension == allowedExtensions[i])
{
fileOK = true;
}
}
}
if (fileOK)
{
try
{
var filePath = path + fileUpload.FileName;
fileUpload.PostedFile.SaveAs(filePath);
FilePath = filePath;
ltrlResult.Text = "File uploaded successfully. Press OK to continue.";
}
catch (Exception ex)
{
ltrlResult.Text = "File could not be uploaded.";
Sitecore.Diagnostics.Log.Error("File could not be uploaded.", ex, typeof (UploadFile));
}
}
else
{
ltrlResult.Text = "Cannot accept files of this type.";
}
}
base.OnLoad(e);
}
protected override void OK_Click()
{
if (string.IsNullOrEmpty(FilePath))
{
SheerResponse.ShowError("You must first upload a file.", string.Empty);
return;
}
SheerResponse.SetDialogValue(FilePath);
SheerResponse.Alert(
"The import process is starting. This may take a few minutes. \n\nPress OK to continue, and wait until the import confirmation popup appears.");
base.OK_Click();
}
}
Загрузка успешно завершена, она устанавливает SheerResponse.SetDialogValue(FilePath)
, но когда я возвращаюсь к обратной передаче ImportTrials, args.HasResult имеет значение false и args.Result имеет значение null