Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
426 views
in Technique[技术] by (71.8m points)

c# - Open sitecore media library programmatically from asp.net button click

I have been trying to figure out if it is possible to open the Sitecore media library browser programmatically from ASP.NET code-behind on a server-side button click. By searching the net, I found an example which explained how to open a media library browser from a Sitecore thumbnail click. I tried this approach on ASP.NET server-side button click but it did not work failing with object reference not set to an instance of an object.

Can anyone please help me if you know how to open the Sitecore media browser in a modal dialog via a server side button click?

My code:

protected void btnShowMediaPopup_Click(object sender, EventArgs e)
{
    Database masterDb = Factory.GetDatabase("master");
    UrlString url = new UrlString(UIUtil.GetUri("control:Sitecore.Shell.Applications.Media.MediaBrowser"));

    Item folderItem = masterDb.GetItem("/sitecore/media library/Images");
    url["ro"] = folderItem.Uri.ToString();

    SheerResponse.ShowModalDialog(url.ToString(), true);   
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I found a solution. Steps as follows:

a. Created config file "MediaBrowser.config" and added into folder App_Config/Include (configuration xml below):

<configuration xmlns:patch= "http://www.sitecore.net/xmlconfig/">
  <sitecore>
      <commands>
          <command name="example:MediaBrowser" type="SitecoreTraining.HelperClasses.MediaBrowser,SitecoreTraining" />
      </commands>
  </sitecore>
</configuration>

b. In my ascx control markup added the following to open the media browser:

<A class="scChromeCommand" title="Open Media Browser" onclick="javascript:Sitecore.PageModes.ChromeManager.postRequest('example:MediaBrowser(id=<%# Sitecore.Context.Item.ID.ToString() %>)',null,false)" href="#"><IMG alt="Open Media Browser" src="http://localhost:2438/temp/IconCache/applications/16x16/photo_scenery.png" width="16" height="16" /></A>

c. Created a MediaBrowser class that inherits the sitecore Command class with the following main methods used:

public override void Execute(CommandContext context) {

        Item item = context.Items.Length == 0 ? Context.Item : context.Items[0];
        contextItem = item;

        var parameters = new NameValueCollection();

        wizardPipeline = Context.ClientPage.Start(this, "Run", parameters);
    }

protected virtual void Run(ClientPipelineArgs args) {

        Database  masterDb = Factory .GetDatabase("master");      
        if  (args.IsPostBack)     
        {         
            var itemID = args.Result;   
        }     
        else      
        {         
            UrlString  url = new  UrlString (UIUtil .GetUri("control:Sitecore.Shell.Applications.Media.MediaBrowser" ));  

            Item  folderItem = masterDb.GetItem("/sitecore/media library/Images" );         
            url["ro" ] = folderItem.Uri.ToString();                  
            SheerResponse.ShowModalDialog(url.ToString(), true );         
            args.WaitForPostBack(true );     
        }
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...