File Upload Sitecore dialog returning null Args.Result in PostBack (Sitecore 8.2, C#)












0















I have a custom Sitecore ribbon button to import data from an uploaded file. The import dialogue is working, but when it goes into the postback, args.HasResult is returning as 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);
}
}
}
}


I have a file UploadFile.cs that is handling the dialog:



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. nnPress OK to continue, and wait until the import confirmation popup appears.");


base.OK_Click();
}
}


The upload is succeeding, It's setting SheerResponse.SetDialogValue(FilePath), but when I get back into the postback of ImportTrials, args.HasResult is false and args.Result is null










share|improve this question



























    0















    I have a custom Sitecore ribbon button to import data from an uploaded file. The import dialogue is working, but when it goes into the postback, args.HasResult is returning as 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);
    }
    }
    }
    }


    I have a file UploadFile.cs that is handling the dialog:



    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. nnPress OK to continue, and wait until the import confirmation popup appears.");


    base.OK_Click();
    }
    }


    The upload is succeeding, It's setting SheerResponse.SetDialogValue(FilePath), but when I get back into the postback of ImportTrials, args.HasResult is false and args.Result is null










    share|improve this question

























      0












      0








      0








      I have a custom Sitecore ribbon button to import data from an uploaded file. The import dialogue is working, but when it goes into the postback, args.HasResult is returning as 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);
      }
      }
      }
      }


      I have a file UploadFile.cs that is handling the dialog:



      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. nnPress OK to continue, and wait until the import confirmation popup appears.");


      base.OK_Click();
      }
      }


      The upload is succeeding, It's setting SheerResponse.SetDialogValue(FilePath), but when I get back into the postback of ImportTrials, args.HasResult is false and args.Result is null










      share|improve this question














      I have a custom Sitecore ribbon button to import data from an uploaded file. The import dialogue is working, but when it goes into the postback, args.HasResult is returning as 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);
      }
      }
      }
      }


      I have a file UploadFile.cs that is handling the dialog:



      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. nnPress OK to continue, and wait until the import confirmation popup appears.");


      base.OK_Click();
      }
      }


      The upload is succeeding, It's setting SheerResponse.SetDialogValue(FilePath), but when I get back into the postback of ImportTrials, args.HasResult is false and args.Result is null







      c# file-upload sitecore postback sitecore8.2






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 20 '18 at 19:24









      Erica Stockwell-AlpertErica Stockwell-Alpert

      1,41532464




      1,41532464
























          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53400140%2ffile-upload-sitecore-dialog-returning-null-args-result-in-postback-sitecore-8-2%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53400140%2ffile-upload-sitecore-dialog-returning-null-args-result-in-postback-sitecore-8-2%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          If I really need a card on my start hand, how many mulligans make sense? [duplicate]

          Alcedinidae

          Can an atomic nucleus contain both particles and antiparticles? [duplicate]