none
Detect current device channel from code behind

    Question

  • From MSDN article about device channels:

    Also, device channels can set a JavaScript variable called effectiveDeviceChannel that contains the current channel alias. This variable can be used to show which channel is currently being used.
    Is there similar variable, that could be used in code behind (in C#), to determine which channel is currently used?
    Tuesday, July 23, 2013 2:53 PM

Answers

  • Hi,

    What I've found using Reflector, is that all the DeviceChannel APIs are internal (under Microsoft.SharePoint.Publishing.Mobile). However, you can use 'HttpContext.Current.Items["MobileChannel"]' to get Desired Device Channel. You can get the effective device channel from 'HttpContext.Current.Items["EffectiveMobileChannel"]'. The only issue you might have that these variable loaded on demand and while you are accessing these variables, they might not be initialized.


    Thanks,
    Sohel Rana
    http://ranaictiu-technicalblog.blogspot.com

    • Marked as answer by Tschareck Monday, February 10, 2014 9:53 AM
    Monday, February 3, 2014 2:32 AM

All replies

  • You can use DeviceChannelPanel in your web part...

    For e.g. the following in a visual web part will render only to a channel that is targetted for tablets.

    <Publishing:DeviceChannelPanel runat="server" ID="pnlTab" IncludedChannels="Tab">
        You can see this tab devices.
    </Publishing:DeviceChannelPanel>

    From code behind, you can create a device channel panel dynamically and add contents to it.

    e.g.

                LiteralControl l = new LiteralControl();
                l.Text += "Sample content for tablets";
                DeviceChannelPanel dp = new DeviceChannelPanel();
                dp.Controls.Add(l);
                
                dp.IncludedChannels = "Tab";
                
                MyPanel.Controls.Add(dp);

    • Proposed as answer by sreejukg Sunday, February 2, 2014 1:14 PM
    • Unproposed as answer by Tschareck Sunday, February 2, 2014 1:18 PM
    Sunday, February 2, 2014 1:13 PM
  • Hi,

    What I've found using Reflector, is that all the DeviceChannel APIs are internal (under Microsoft.SharePoint.Publishing.Mobile). However, you can use 'HttpContext.Current.Items["MobileChannel"]' to get Desired Device Channel. You can get the effective device channel from 'HttpContext.Current.Items["EffectiveMobileChannel"]'. The only issue you might have that these variable loaded on demand and while you are accessing these variables, they might not be initialized.


    Thanks,
    Sohel Rana
    http://ranaictiu-technicalblog.blogspot.com

    • Marked as answer by Tschareck Monday, February 10, 2014 9:53 AM
    Monday, February 3, 2014 2:32 AM