Surendra Sharma

Surendra Sharma

Search This Blog

Showing posts with label Programming. Show all posts
Showing posts with label Programming. Show all posts

Sunday, August 13, 2017

How to render Brightcove video programmatically using sitecore item




Brightcove and Sitecore using CSharp
Brightcove and Sitecore using CSharp


It’s very easy to render Brightcove video through HTML as

<iframe src="//players.brightcove.net/4234/default_default/index.html?videoId=1111" allowfullscreen webkitallowfullscreen mozallowfullscreen></iframe>

But do you know how to render it through programmatically?

Let’s suppose you are getting video item id and player id through Sitecore in your MVC view as

var playerHtml = string.Empty;
<div>
@{
    playerHtml = HelperClass.Instance().GetVideoPlayerMarkup(Model.VideoItemGuidId, Model.PlayerGuidId, 170, 300);
}
<script language="JavaScript" type="text/javascript" src="http://admin.brightcove.com/js/BrightcoveExperiences.js"></script>
@Html.Raw(playerHtml)
<script type="text/javascript">
    brightcove.createExperiences();
</script>
</div>

This view calling GetVideoPlayerMarkup() method which accept 4 parameters – video item id, player item id, video player’s height and width.

You need to include below namespace for using this method

using Sitecore.MediaFramework.Pipelines.MediaGenerateMarkup;
using Sitecore.MediaFramework.Players;

Here is a C# code for this method as

public virtual string GetVideoPlayerMarkup(ID videoItemId, ID playerItemId, int height, int width)
{
    var playerHtml = string.Empty;
    PlayerProperties playerProperties = new PlayerProperties()
    {
        ItemId = videoItemId,
        PlayerId = playerItemId,
        Height = height,
        Width = width
    };
    MediaGenerateMarkupArgs args = new MediaGenerateMarkupArgs()
    {
        MarkupType = MarkupType.Html,
        Properties = playerProperties
    };
    MediaGenerateMarkupPipeline.Run(args);
    if (!args.Aborted)
    {
        playerHtml = args.Result.Html;
    }
    return playerHtml;
}

That’s it. Now when you render this view you will get the Brightcove video on the browser.

I hope you like this Sitecore-Brightcove integration through code. Stay tuned for more Sitecore related articles.

Till that happy Sitecoring :)

Please leave your comments or share this code if it’s useful for you.

Saturday, September 12, 2015

How to implement 301 redirect with IIS URL rewrite module in ASP.NET



If Its common scenario that, if you created items first and later on creating workflow then you have to manually apply those workflow on template’s standard values and content items.

If your client website is already running and you have developed it again with same or different technology.

Once the coding and testing of this new website is complete. What next?

Deploy on server. Make it LIVE.

So there may be some old URL or pages which should be redirected to new website pages.
Good example is if somebody bookmarked the old URL then instead of page not found it should be redirected to new pages.

In simple terms it’s called 301 redirect.

How to achieve 301 redirect without writing code in ASP.NET?

You must have URL Rewrite module in IIS. If you have not then download and install from http://www.iis.net/downloads/microsoft/url-rewrite

Now make entries in web.config between <system.webServer> … </system.webServer> tag by specifying old URL pages mapping with new pages as below.

Suppose you old page URL is http://www.example.com/old.php and you want to redirect it to new page as http://www.example.com/new.aspx. Finally your config file entry looks like



<rewrite>
  <rules>

      <rule name="Rewrite Rule1" stopProcessing="true">
          <match url="world/new-zealand.php" />
          <action type="Redirect" url="world/east/country1.aspx" />
      </rule>
      <rule name="Rewrite Rule2" stopProcessing="true">
          <match url="old.php" />
          <action type="Redirect" url="new.aspx" />
      </rule>
      <rule name="Rewrite Rule3" stopProcessing="true">
          <match url="oldfile.pdf" />
          <action type="Redirect" url="newfile.pdf" />
      </rule>
      <rule name="Rewrite Rule4" stopProcessing="true">
          <match url="policy.pdf" />
          <action type="Redirect" url="/" />
      </rule>
      <rule name="Rewrite Rule5" stopProcessing="true">
          <match url="world/bahamas.php" />
          <action type="Redirect" url="bahamasnew.aspx" />
      </rule>
  </rules>
</rewrite>  

Here we are redirecting from request for any php page, pdf file to index page, new aspx pages.
You can specify any numbers of pages here. But this is good for limited pages. My suggestion is that its limit should be upto 50 pages.

If you have more than 50 pages then write the custom redirecting code.

You can do all these activities from IIS as well and IIS will make entry in web.config for you.

Please leave your comments or share this tip if it’s useful for you.